Presentation is loading. Please wait.

Presentation is loading. Please wait.

Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox.

Similar presentations


Presentation on theme: "Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox."— Presentation transcript:

1 Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox and CheckboxGroup classes –Choice class –………………..

2 Some methods of component class comp here is a variable that refers to any component comp.getSize() –returns an object belonging to the class Dimension. It has two instance variable comp.getSize().width and comp.getSize().height comp.getParent() –Returns a value of thpe Container comp.getLocation() –Returns the location of the top-left corner of the comonent. comp.setEnabled(true) & comp.setEnabled(false) –Enable or disable the component. comp.setVisible(true) & comp.setVisible(false) comp.setBackground(color), comp.setForeground(color), comp.setFont(font)

3 The Button Class An object of class Button is a push button. Some of the Button methods: 1.Constructors: Button bttn = new Button(“test”) 2.Event: When the user clicks on a button, the button generates an event of type ActionEvent. This event is sent to any listener that has been registered with the button. 3.Listeners: An object that wants to handle events generated by buttons must implement the ActionListener interface. This interface defines just one method, "pubic void actionPerformed(ActionEvent evt)", which is called to notify the object of an action event. 4.Registration of Listeners: In order to actually receive notification of an event from a button, an ActionListener must be registered with the button. This is done with the button's addActionListener() method. For example: stopGoButton.addActionListener(buttonHandler)

4 Button’s methods (continued) 5.Event methods: When actionPerformed(evt) is called by the button, the parameter, evt, contains information about the event. This information can be retrieved by calling methods in the ActionEvent class. In particular, evt.getActionCommand() returns a String giving the command associated with the button. By default, this command is the label that is displayed on the button. 6.Component methods: There are several useful methods in the Button class. For example, stopGoButton.setLabel("Stop") changes the label displayed on the button to "Stop". And stopGoButton.setActionCommand("sgb") changes the action command associated to this button for action events.

5 The Label Class An object of type Label is just a single line of text. The text cannot be edited by the user The constructor for a Label specifies the text to be displayed: Label message = new Label(“Hello World!”); You can change the text displayed in a label by calling the label’s setText() method: message.setText(“Good World!”); Label is subclass of Component, so you can use Component’s methods such as setFont(), setBackground(), setForeground().

6 TextField and TextArea Classes TextField and TextArea are boxes where the user can type in and edit text. TextField contains a single line of editable text, while a TextArea displays multiple lines and might include scrollbars. Methods: –getText(); return a copy of the current contents –getSelectedText(); return the selected text –select(int start, int end); change the selected range –setEditable(boolean canBeEdited); specify if or not the text in the component can be edited by the user. The TextField generates an ActionEvent when user presses return while typing in the TextField. The TextField class includes an addActionListener() method. In the actionProformed() method, the evt.getActionCommand() will return a copy of the text from the TextField.

7 An exmaple for a simple Component events import java.awt.*; // Defines basic classes for GUI programming. import java.awt.event.*; // Defines classes for working with events. import java.applet.*; // Defines the applet class. public class Action04 extends Applet implements ActionListener { Font textFont, buttonFont; // declare the fonts used in this program TextField txt = new TextField("Input field"); //declare a textfield object. Label result = new Label("This is the label where to show results", Label.CENTER); //declare a Label object which will display the string”” Button squareButton = new Button("Square"), rootButton = new Button("Square root"); // Create two new buttons. “Square“ “Square root” are the text // displayed on the buttons.

8 public void init() { setBackground(Color.lightGray); /* The applet is filled with the background color before the paint method is called. The button and the message in this applet will appear on a light gray background.*/ textFont = new Font("Serif",Font.BOLD,16); // Create a font object representing a big, bold font. buttonFont = new Font("Serif",Font.BOLD,14); squareButton.addActionListener(this); add(squareButton); squareButton.setBackground(Color.blue); squareButton.setForeground(Color.white); squareButton.setFont(buttonFont); /* Set up squareButton to send an "action event" to this applet when the user clicks the button. The parameter, this, is a name for the applet object that we are creating. Add the button to the applet, so that it will appear on the screen.*/

9 rootButton.addActionListener(this); add(rootButton); rootButton.setBackground(Color.red); rootButton.setForeground(Color.yellow); add(txt); //add the textfield to the applet add(result);//add the label to the applet result.setFont(textFont); } // end init()

10 public void actionPerformed(ActionEvent evt) { /* This routine is called by the system when an "action" is performed by the user, provided that the applet has been set as a "listener" for such events. Responds to the buttons by giving the different result display in the applet.*/ int number; String command = evt.getActionCommand(); if (command.equals("Square")){ number = Integer.parseInt(txt.getText()); number *= number; result.setText("The square is :"+number); } if (command.equals("Square root")){ number = Integer.parseInt(txt.getText()); result.setText("The square root is :"+Math.sqrt(number)); } } // end class Action

11


Download ppt "Standard Components and Their Events GUI interface elements are represented by subclasses of component: –Button class –Label class –TextField class –Checkbox."

Similar presentations


Ads by Google