Download presentation
Presentation is loading. Please wait.
1
Java GUI
2
GUI Layout Basic Layout: Flow layout Border layout Grid layout
Box layout Layout Process: Create frame (Jframe) or panel (JPanel) Layout the panel or frame Create the components Place the components on the container You may create more complex GUI by placing panel(s) on the Frame Size and display the Frame
3
javax.swing.JPanel It’s an intermediate-level container
Bi-dimensional area within a Frame You can have multiple JPanels in same Frame Has internal layout default is FlowLayout: all content in a row JPanel can be a handy place to implement listeners for contained components
4
How to set a layout At instantiation time; Separately
JPanel jPan = new JPanel(new BorderLayout()); Separately JPanel jPan = New JPanel(); jPan.setLayout(new BorderLayout());
5
FlowLayout Example import javax.swing.*; import java.awt.*;
JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); 4 & 5. panel.add(new JButton("one")); 4 & 5. panel.add(new JButton("two")); 4 & 5. panel.add(new JButton("three")); 4 & 5. panel.add(new JButton("four")); 4 & 5. panel.add(new JButton("five")); frame.getContentPane().add(panel); frame.setSize(100,200); frame.setvisible(true); frame + panel =
6
BorderLayout Layouts NORTH EAST WEST SOUTH CENTER
7
BorderLayout Layouts import javax.swing.*; import java.awt.*;
JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(new JButton("one"),BorderLayout.NORTH); panel.add(new JButton("two"),BorderLayout.SOUTH); panel.add(new JButton("three"),BorderLayout.EAST); panel.add(new JButton("four"), BorderLayout.WEST); panel.add(new JButton("five"), BorderLayout.CENTER); frame.getContentPane().add(panel); frame.setSize(250,300); frame.setvisible(true);
8
Grid Layout JFrame frame = new JFrame(); JPanel panel = new JPanel();
… JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3,2)); panel.add(new JButton("one")); panel.add(new JButton("two")); panel.add(new JButton("three")); panel.add(new JButton("four")); panel.add(new JButton("five")); frame.add(panel); frame.setSize(100,200); frame.setvisible(true); GridLayout(3,2,5,7);
9
Event Handling event type 1 Each component has a predefined set of events it can generate. Each Built in event has a corresponding built in Listener interface. no Listener Event ignored some component Event generated event type 2 Listeners are written by you and implement the appropriate interface. A class which implements the Listener interface describes what to do when the event occurs. Same event generated by different components may result in different actions. Each Listener is registered with the component its supposed to work with. There it is notified of events generated by that component only. Same Listener Interface event type 2 some component event type 3
10
Code Structure for Event Handling
class Event_Listener_Class implements some_Listener_interface{ public … method(…){ //code here says what to do if button is pressed } public class GUI{ //code to produce the GUI //Here we will 1. Create a Event_Listener_Object 2. Register it with a component – so when the component generates some event, the listener is informed. }
11
Event Handling When an action is performed (like a button is pressed) an Event Object is created AND A method is called This method is found inside a “Listener” class/object In order for the correct method to be called, the litener object must be register to the component.
12
ButtonsTest.java Generic Listener
class ButtonPanel extends JPanel implements ActionListener { private JButton yellowButton; private JButton blueButton; private JButton redButton; // constructor public ButtonPanel() { yellowButton = new JButton("Yellow"); blueButton = new JButton("Blue"); redButton = new JButton("Red"); add(yellowButton); add(blueButton); add(redButton); yellowButton.addActionListener(this); blueButton. addActionListener(this); redButton. addActionListener(this); } Generic Listener
13
ButtonsTest.java (Cont.)
public void actionPerformed(ActionEvent evt){ Object source = evt.getSource(); // process event Color color = getBackground(); if (source == yellowButton) color = Color.yellow; else if (source == blueButton) color = Color.blue; else if (source == redButton) color = Color.red; setBackground(color); repaint(); } Generic callback in ActionListener
14
class ButtonFrame extends JFrame
{ public ButtonFrame() { setTitle("ButtonTest"); setSize(300, 200); setVisible (true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); add(new ButtonPanel()); } }
15
Anonymous Inner Classes
Digression Nested Class – on the fly Need an object of a particular type exactly once, as a parameter The example is an anonymous subclass of WindowAdapter Overriding windowClosing()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.