{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Awt not most of the work of creating instance class

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: import java.awt.*; not most of the work class Gui of creating anFrame of Gui. extends instance Modify properties of Gui. { public Gui(String s) //constructor and attach it to Gui. Create a button { super(s); //construct Frame part of Gui setBackground(Color.yellow); setLayout(new FlowLayout()); Button pushButton = new Button("press me"); add(pushButton); Construct a Gui, set its size } } //class Gui and make it visible. class Ex_1 //Creates an instance of class Gui { public static void main(String args) { Gui screen = new Gui("Example 1"); screen.setSize(500,100); screen.setVisible(true); } } //class Ex_1 What does this program not do? Responding to events //Program to demonstrate action listeners and event handlers import java.awt.*; import java.awt.event.*; Attach a observer to "listen" for events class Gui extends Frame implements ActionListener, WindowListener { public Gui(String s) //constructor a simple button, on this window. Create that might occur { super(s); then add it to the Gui Frame. setBackground(Color.yellow); setLayout(new FlowLayout()); Attach an observer to listen to//listen this button. events on for events on this Window addWindowListener(this); Button pushButton = new Button("press me"); When the ActionEvent add(pushButton); labeled "press me" occurs, pushButton.addActionListener(this); //listen for Button press print (ring) the bell. } //define action for Button press public void actionPerformed(ActionEvent event) { final char bell = '\u0007'; Listen for these events that if (event.getActionCommand().equals("press can occur on a Window. me")) { System.out.print(bell); } } //define methods in WindowListener interface public void windowClosing(WindowEvent event) { System.exit(0); } public void windowClosed(WindowEvent event) {} //do nothing public void windowDeiconified(WindowEvent event){} public void windowIconified(WindowEvent event){} public void windowActivated(WindowEvent event){} public void windowDeactivated(WindowEvent event){} public void windowOpened(WindowEvent event){} } Resp...
View Full Document

{[ snackBarMessage ]}