1 / 25

Java Swing Recitation – 11/(20,21)/2008

CS 180 Department of Computer Science, Purdue University. Java Swing Recitation – 11/(20,21)/2008 . Announcements. Project 8 is out Milestone due on Dec 3 rd, 10:00 pm Final due on Dec 10 th, 10:00 pm No classes, recitations and labs next week. No study group meeting next Tuesday.

ace
Télécharger la présentation

Java Swing Recitation – 11/(20,21)/2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 180 Department of Computer Science, Purdue University Java Swing Recitation – 11/(20,21)/2008

  2. Announcements • Project 8 is out • Milestone due on Dec 3rd, 10:00 pm • Final due on Dec 10th, 10:00 pm • No classes, recitations and labs next week. • No study group meeting next Tuesday. • Consulting hours will be held only on Monday 7-10 pm.

  3. Event Driven Programming • Most GUI programs involve events and event handlers. • A GUI event is an object that represents some action such as clicking the mouse, dragging the mouse, pressing a keyboard key, clicking the close-window button on a window, etc. • When an object generates an event, it is said to fire the event.

  4. 4 Event Driven Programming • An event is an object that represents an action • Event handling is similar to exception handling • Difference – • Exceptions are created by user code or java interpreter. • Events are created by external actions, such as user interactions through a GUI

  5. Programming Example: A Simple Window • This simple program produces a window and displays some text. • JFrame: to create a window • JLabel: to create a label • getContentPane().add() : add a component such as a label to the content pane of the window • setTitle() : set the title of the window • setSize() : set the size of the window • setVisible() : Method setVisible permits the programmer to specify when GUI objects should be displayed and when they should not

  6. Example Programming Example: A Simple Window A Frame public class makeWindow { public static void main(String args[]) { JFramemyWindow=new JFrame(); //create the window myWindow.setSize(300, 200); //set the title of the window myWindow.setTitle("this is a window"); //create the label JLabelmyLabel=new JLabel("this is a label"); //add the label to the content pane of the window myWindow.getContentPane().add(myLabel); //set color of the content pane myWindow.getContentPane().setBackground(Color.CYAN); //make the window visible myWindow.setVisible(true); } } This area is the content pane

  7. 7 Layout Managers • A layout manager arranges objects within a container • After a container has been created, you can set its layout manager using the setLayout method. • For example: • Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); • FlowLayout : It simply lays out components in a single row, starting a new row if its container is not sufficiently wide.

  8. 8 Layout Managers • BorderLayout : It places components in up to five areas: top, bottom, left, right, and center. • Every content pane is initialized to use a BorderLayout. • GridLayout : It simply arranges a bunch of components in a grid of rows and columns. Components are ordered in a row-major fashion.

  9. Example - Border Layout • A BorderLayout manager can place a component into any of the five regions. • Regions which are unused give up their space to BorderLayout.CENTER. • This layout limits the GUI to five objects, these are almost always five (or fewer) JPanels. • Equivalent forms for center: content.add(label3, BorderLayout.CENTER); and content.add(label3, “Center”); and (for center ONLY) content.add(label3); Even though BorderLayout is default, it’s better to set the layout explicitly

  10. Buttons • A button is a GUI component that looks like a button and fires an event when it is clicked using a mouse. • Like a label, a button is created and added to a container. • Unlike a label, a button can fire an event and the event can cause a GUI to perform some action. • Buttons are instances of the JButtonclass.

  11. Adding Buttons • A button is created using JButtonButton_Name = new JButton(“Button_Label”); • A button is added to a container using Container_Name.add(Button_Name);

  12. Action Listeners and Action Events • For each button, the GUI needs to • register (specify) the listener object(s). • define the methods to be invoked when an event is fired. • Buttons fire action events which are handled by action listeners. • An action listener is an object of type ActionListener, and ActionListener is an interface • Note: A user defined GUI class can itself be its own listener if it implements the ActionListener interface.

  13. Action Listeners and Action Events, cont. • To make a class into an ActionListener, • add implements ActionListenerto the heading of the class definition • Define a method named actionPerformed. • register the ActionListener object with the component that will fire the event using the method addActionListener(..) • The actionPerformedMethod of the ActionListener class will be called every time the object fires an event. • This is an important step that must not be forgotten. • (A component may register with more than one listener.)

  14. Buttons and an Action Listener

  15. The actionPerformed Method • An actionListenerclass must have a method namedactionPerformedthat has one parameter of typeActionEvent. • syntax public void actionPerformed(ActionEvent e) { Code_for_Actions_Performed }

  16. Method setActionCommand • Every object that fires an action event has an associated string known as the action commandfor that component. • e.getActionCommand() returns the action command for the component that fired the event e. • The default action command for a button is its name. • Method setActionCommand(String)can be used to change the action command for the object.

  17. .Example with buttons

  18. Example with buttons

  19. The JPanel Class • A GUI can be organized hierarchically, with window-like containers inside other containers. • Components can be placed in a JPanel which can be placed in another JPanel, … which can be placed in a JFrame. • E.g, to place two components in BorderLayout.SOUTHfor example, simply place the two components in a panel and place the panel in the BorderLayout.SOUTH position. • The panel has its own layout manager.

  20. Example with JPanel Container contentPane = getContentPane (); contentPane.setBackground (Color.BLUE); contentPane.setLayout (new BorderLayout ()); JPanelbuttonPanel = new JPanel (); buttonPanel.setBackground (Color.WHITE); buttonPanel.setLayout (new FlowLayout ()); JButtonstopButton = new JButton ("Red"); stopButton.setBackground (Color.RED); stopButton.addActionListener (this); buttonPanel.add (stopButton); JButtongoButton = new JButton ("Green"); goButton.setBackground (Color.GREEN); goButton.addActionListener (this); buttonPanel.add (goButton); contentPane.add (buttonPanel, BorderLayout.SOUTH); Creating a panel with flow layout Adding a red button to the panel Adding a green button to the panel Adding panel to the frame

  21. The JPanel Class, cont. Panel with two buttons since BorderLayout limits the GUI to five objects, there are usually JPanels (five or fewer) in the pane.

  22. The Container Class • An object of a class which descends from class Container is called a container class and can have components added to it. • Examples – • JFrameis a descendent of class Container, permitting any JFrame object to hold labels, buttons, panels, and other components. • JPanelis a descendent of class Container, permitting any JPanel object to hold labels, buttons, other panels, and other components.

  23. Text I/O using JTextField • Create a text field with some initial text JTextFieldtextfield = new JTextField("Initial Text"); • Create a text field with some initial text and a default number of columns. The number of columns controls the preferred width of the component textfield= new JTextField("Initial Text", cols); Use textfield.getText() to return the written text

  24. Text I/O using JTextArea • Create a text area with some initial text JTextAreatextarea = new JTextArea("Initial Text"); • Create a text area with some initial text and a default number of rows and columns. This number of rows and columns controls the preferred width and height of the component; textarea = new JTextArea(“InitialText", rows, cols);

  25. Quiz GUI components can be added to an object of any class that descends from the _______ class.

More Related