Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.

Similar presentations


Presentation on theme: "Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox."— Presentation transcript:

1 Java GUI

2 Graphical User Interface (GUI) a list a button a text field a label combo box checkbox

3 Graphical User Interfaces GUI’s, AWT, and Swing GUI’s, AWT, and Swing –AWT (Abstract Window Toolkit) is the original Java classes used for GUI development. –Java 2 includes an improved toolkit, named Swing, to improve on AWT. Swing components are easier to use, more portable, and provide more functionality than older AWT components. Swing components are easier to use, more portable, and provide more functionality than older AWT components.

4 Inheritance Helps Since Swing was designed as an improvement of AWT, it made sense not to start from scratch when designing the Swing components. Since Swing was designed as an improvement of AWT, it made sense not to start from scratch when designing the Swing components. To differentiate them, Swing components have names that begin with the letter “J”. To differentiate them, Swing components have names that begin with the letter “J”.

5 Graphical User Interfaces GUI’s and the Java Swing components GUI’s and the Java Swing components –JComponent is the ancestor of many Swing classes. Here are some descendants of the Jcomponent class. JButton JButton JPanel JPanel JLabel JLabel JTextArea JTextArea

6 Windows We will use the class JFrame to create GUI windows. We will use the class JFrame to create GUI windows. –javax.swing.JFrame inherits from java.awt.Frame –Every JFrame object has a “content pane” associated with it. Content panes are of type java.awt.Container Content panes are of type java.awt.Container

7 What to Import When we design GUI programs, there are three packages we will need to import: When we design GUI programs, there are three packages we will need to import: –java.awt.* –java.awt.event.* –javax.swing.* The java.awt.event package provides us with the capability to respond to user interface “events”, such as the pushing of a button. The java.awt.event package provides us with the capability to respond to user interface “events”, such as the pushing of a button.

8 Displaying a Window The most common form of a window displayed via Swing is a JFrame. The most common form of a window displayed via Swing is a JFrame. –Next, we will see several example programs that will: display JFrames display JFrames place Swing objects in frames place Swing objects in frames Exhibit event-driven programming Exhibit event-driven programming –“Listeners” will be used to respond to mouse events.

9 Example GUIone Creating a JFrame Creating a JFrame –new JFrame() Setting the size of our frame. Setting the size of our frame. –setSize(…); Displaying the frame Displaying the frame –setVisible(true)

10 FirstFrame.java import javax.swing.*; class FirstFrame extends JFrame { public FirstFrame() { super("FirstFrame"); setSize(300, 200); setVisible(true); } public static void main(String[] args) { JFrame frame = new FirstFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }

11 Putting content in a GUI Frames constitute GUI real estate Frames constitute GUI real estate Organization of space: Organization of space: –Via other containers Each container defines and manages an area within the GUI Each container defines and manages an area within the GUI –e.g. menu bars, scroll bars, tool bars, panels, etc. Containers can be nested Containers can be nested Individual components added into those Containers Individual components added into those Containers Containers may decide on their layout (will be discussed later) Containers may decide on their layout (will be discussed later)

12 LabelTest import javax.swing.*; import java.awt.*; public class LabelTest extends JFrame { public LabelTest() { setTitle("Label Test"); { setTitle("Label Test"); JLabel helloLabel = new JLabel("Hello"); JLabel helloLabel = new JLabel("Hello"); add( helloLabel); add( helloLabel); setSize(300, 200); setSize(300, 200); setVisible(true); setVisible(true); } public static void main(String[] args) public static void main(String[] args) { LabelTest t = new LabelTest(); { LabelTest t = new LabelTest(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

13 Event Handling Perform some functionalities when the button is press Perform some functionalities when the button is press Need to have a class that implements ActionListener Need to have a class that implements ActionListener The functionalities needed to perform will be put in the actionPerformed method The functionalities needed to perform will be put in the actionPerformed method Create an instance of this type of ActionListener Create an instance of this type of ActionListener Register the handler with the component Register the handler with the component

14 ButtonTest public class ButtonTest extends JFrame { public ButtonTest() { setTitle("Button Test"); { setTitle("Button Test"); JButton helloButton = new JButton("Hello"); JButton helloButton = new JButton("Hello"); add( helloButton); add( helloButton); // create an instance of inner class ButtonHandler to use for button event handling // create an instance of inner class ButtonHandler to use for button event handling ButtonHandler handler = new ButtonHandler(); ButtonHandler handler = new ButtonHandler(); //register the handler //register the handler helloButton.addActionListener( handler ); helloButton.addActionListener( handler ); setSize(300, 200); setSize(300, 200); setVisible(true); setVisible(true); } // inner class for button event handling // inner class for button event handling private class ButtonHandler implements ActionListener { private class ButtonHandler implements ActionListener { // handle button event // handle button event public void actionPerformed( ActionEvent event ) public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( null, JOptionPane.showMessageDialog( null, "You pressed: " + event.getActionCommand() ); "You pressed: " + event.getActionCommand() ); } } // end private inner class ButtonHandler } // end private inner class ButtonHandler public static void main(String[] args) public static void main(String[] args) { ButtonTest t = new ButtonTest(); { ButtonTest t = new ButtonTest(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

15 No inner class public class ButtonTest2 extends JFrame implements ActionListener { public ButtonTest2() public ButtonTest2() { setTitle("Button Test"); setTitle("Button Test"); JButton helloButton = new JButton("Hello"); JButton helloButton = new JButton("Hello"); add( helloButton); add( helloButton); helloButton.addActionListener( this ); //register the handler (the frame itself) helloButton.addActionListener( this ); //register the handler (the frame itself) setSize(300, 200); setSize(300, 200); setVisible(true); setVisible(true); } // handle button event // handle button event public void actionPerformed( ActionEvent event ) public void actionPerformed( ActionEvent event ) { JOptionPane.showMessageDialog( null, JOptionPane.showMessageDialog( null, "You pressed: " + event.getActionCommand() ); "You pressed: " + event.getActionCommand() ); } public static void main(String[] args) public static void main(String[] args) { ButtonTest2 t = new ButtonTest2(); ButtonTest2 t = new ButtonTest2(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}

16 Example JTextfield JTextfield Get Length of square in a Jtextfield and output the area Get Length of square in a Jtextfield and output the area


Download ppt "Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox."

Similar presentations


Ads by Google