Intro to Java Web-Notes_Part49

Intro to Java Web-Notes_Part49 -...

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

View Full Document Right Arrow Icon
Image of page 1

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

View Full Document Right Arrow Icon
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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern