Intro to Java Web-Notes_Part49

Intro to Java Web-Notes_Part49 -...

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: text.setText("Clickedl"); text.setBackground(Color.R3D); Note that older versions of Java (1.4.x and earlier) required that constants for predefined color names be defined in lowercase letters (e.g., Color. red), but with Java 1.5 you should write the color name in uppercase letters (C010r.RED). Here is a description "w of the above program. Two Event Handlers in the Same Program GUI programs often are responding to events generated by two or more components. The DemoWindow program, below, shows a program that responds to action events from two different buttons. One button is labeld Red and the other Yellow. The background color of a text field changes to red or yellow, depending on which button was clicked. In this program each button has its own event handler. It also possible to write the program so that the buttons share one event handler (that technique will be covered later). import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DemoWindow { 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 red : new JButton("Red"); JButton yellow : new JButton("Yellow"); JTextField txt = new JTextField("some text"); public MyFrame(String s) { super(s); setLayout(new FlowLayout()); add(red); add(txt); add(yellow); red.addActionListener(new RedHandler()); yellow.addActionListener(new YellowHandler()); } class RedHandler implements ActionLis:ener { public void actionPerformed(ActionEvent event) { txt.setBackground(Color.RED); } } class YellowHandler implements Actionnistener { public void actionPerformed(ActionEvent event) { txt.setBackground(Color.YELLOW); Here is a description '*' of the above program. figmg - About Us Copyright © 2006 by Kiowok, Ann Arbor, Michigan, USA ...
View Full Document

Page1 / 2

Intro to Java Web-Notes_Part49 -...

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