Intro to Java Web-Notes_Part48

Intro to Java Web-Notes_Part48 - 3. Create the handler...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 3. Create the handler object and CONNECT THE HANDLER OBJECT to the visually component. Here is an application that presents a button and a text field. Every time button is pressed the text field is set to the word "Clicked". import java.awt.*; import java.awt.event.*; // l — Add the import statement import javax.swing.*; public class SampleWin8 i public static void main(String args) { MyFrame frame : new MyFrame("Alan Window"); frame.setSize(400, 300); frame.setLocation(lOO, 75); frame.setVisible(true); } class MyFrame extends JFrame { JButton btnPress; JTextField text; public MyFrame(String s) { super(s); setLayout(new FlowLayout()); btnPress : new JButton("Press"); add(btnPress); text = new JTextField(20); add(text); btnPress.addActionListener(new ButtonHandler()); // 3 - connect } // 2 — Add the event handler class. // // Note that this is defined INSIDE tie ending curly brace for // the MyFrame class. class ButtonHandler implements Actionuistener { public void actionPerformed(ActionEvent e) { text.setText("Clicked"); } Here is a description in of a program very similar to the SampleWin8 program shown above. There are many different kinds of events that occur while a Java program is running. A Java program with a GUI interface can handle mouse events (when the mouse moves, when the mouse buttons are clicked), window events (when a window is resized, when a window is moved), and so on. The SampleWin8 program, above is handing an action event. An action event occurs when: ' A JButton is clicked. ' Text is entered into a J TextField and the user presses the Enter key. Exercises 1. Write an application SampleWin9 that presents two JButtons labeled "OK" and "Clear", and a J TextField component. Write event handlers for the two buttons so that when the OK button is clicked then the JTextField is set to "Alright" and when the Clear button is clicked the JTextField is set to "Cleared". 2. Write an application SampleWinlO that presents two JButtons labeled "Left" and "Right", and two JTextField components. Write event handlers for the two buttons so that when the Left button is clicked then the text in the lefthand JTextField is set to whatever contents are currently in the righthand text field, and when the Right button is clicked the text moves from the text field displayed on the lef to the text field displayed on the right. Changing a Component's Background Color In addition to retrieving the current contents of a text field with the getT ext method String s : text.getText(); // s now references a copy of the textfield's text and setting the text field text to something new text.setText("Something neW"); // the user see's "Something new" appear in the text field you can manipulate the text field in many other ways: change the text field's background color, select part of the display text, disable user input into the box, etc. To see a full list ofpossible mainpulations, see Sun's official ,iaygvdgggmggntatjwm}, (look up J TextField, and all of the methods that JTextField inherits from the JTextComponent, J Component, Container, Component and Object classes). The program below changes the text field's background color to red when the "OK" button is clicked. Click here for a full description of the program: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoWindow3 { public static void main(String args) { MyFrame frame : new MyFrame("Clicked! Window"); frame.setSize(400, 300); frame.setLocation(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } class MyFrame extends JFrame { JButton btnOK : new JButton("OK"); JT xtFi ld t xt — n w JT xtFi ld(20); public MyFrame(String s) { super(s); setLayout(new FlowLayout()); add(btnOK); add(text); btnOK.addActionListener(new Buttoniandler()); } class ButtonHandler implements Actionnistener { public void actionPerformed(ActionEvent event) { ...
View Full Document

Page1 / 2

Intro to Java Web-Notes_Part48 - 3. Create the handler...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online