Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout.

Similar presentations


Presentation on theme: "GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout."— Presentation transcript:

1 GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout Managers Event Handlers

2 Terms GIU component container coordinates graphics context

3 Types of interfaces Console applications Using print and Scanner A simple GUI Using JoptionPane A complete GUI using a Frame Can be created using AWT Can be create using Swing

4 The swing versus the AWT AWT part of JDK v1 Mapped to platform specific components Heavyweight Swing introduced in V2 Platform independent components Lightweight

5 Components JButton tnOK = new JButton(“OK”); JLabel tnOK = new JLabel(“OK”); JTextField tfNAME = new JTextField(“Enter Name”); JCheckBox cbOK = new JTextBox(“OK”); JComboBox cmb = new JComboBox(new String[] {“Red”,”Green”,Blue”});

6 The Class chart JComponent AbstractButton  Jbutton  JToggleButton JCheckBox JTextComponent JTextField JLabel JPanel … many others …

7 Classes Container Classes JFrame JDialog JApplet JPanel Helper Classes Graphics, Color, Font, FontMetrics LayoutManager

8 Frames Public class myframe { public static void main(String[] args) { JFrame yframe = new JFrame(“MyFrame”); myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_C LOSE); }

9 Frames with components (limited example) Import javax.swing.*; Public class myframe { public static void main(String[] args) { JFrame yframe = new JFrame(“MyFrame”); java.awt.Container mycnt = frame.getContentPane(); Jbutton btOK = new JButton(“OK”); mycnt.add(btOK); myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_CLOSE); }

10 Getting the screen size Dimension ScrS = Toolkit.getDefaultToolkit().getScreenSize(); int wid = screensize.width; int hgt = screensize.height; This can be used to center or dynamically size frame. Frame.setlocation(x,y); Frame.setwidth(wid); Frame.sethight(hgt);

11 Layout Managers Use to determine how components will be positioned on the frame or panel. To create and apply a layout manager to a component. LayoutManager Lm = new FlowLayout(); Frame.setLayout(LM);

12 Type of layout managers Flow Grid Border Others …. Properties.setHGap(10);.setVGap(10);.setAlignment(FlowLayout.RIGHT);

13 Creating a subclass of Frame Import javax.swing.*; Import java.awt.*; Public class theframe extends Jframe { Public theframe() { Container mycnt = getContentPane(); mtcnt.setLayout(new FlowLayout()); Jbutton btOK = new JButton(“OK”); mycnt.add(btOK); } public static void main(String[] args) { theframe myframe = new theframe(); myframe.settitle(“Frame subclass”); myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_CLOSE); }

14 The color Class Color c = new Color(100,100,100) The color constructor expects: Color(R,G,B) Red, Green, Blue 0-255 btnOK.setForeground( c );

15 The Font class Font ( name, Style, size) Font F = new Font(“Serif”, Font.BOLD + Font.ITALIC, 12); btnOK.setFont(F);

16 Drawing shapes drawline drawrect fillrect drawarc fillarc drawstring draw3drect drawroundedrect drawpolygon

17 Making a window based application import javax.awt.*; import javax.swing.*; public class MyWindow extends JFrame { public MyWindow() { super(“My Window”); setSize(400,200); setVisible(true); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.fillRect(20,20,100,100); } public static void main( String args[]) { MyWindow app1=new MyWindow(); app1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }

18 Event Handlers Event-driven programming A change in the state of a GUI component triggers a call to an event handler method Event source is the object that the user interacts with to trigger the event. Event handler is an object with an event handler method designed to handle a specific event type.

19 Implementing Event Handlers 1 – Code a class for the event handler. The class must implement the correct listener. This can be the same class file as out application runs in. 2 – Create an event handler object from the class. 3 – Register the handler object with the GUI component.

20 Event Handlers – Step 1 Create a class to implement the Handler public class MyProgram extends JFrame implements ActionListener { … /** This method will be invoked when a button is clicked */ public void actionPerformed(ActionEvent e) { System.out.println("The " + e.getActionCommand() + " button is " + "clicked at\n " + new java.util.Date(e.getWhen())); } … }

21 Event Handlers – Step 2 Create event handler object //ButtonListener btListener = new ButtonListener(); This step may be skipped if we are using the Program class as the event object.

22 Event Handlers – Step 3 Register the event handler object with the source object. In this example a button. … private JButton jbtOk = new JButton("OK"); jbtOk.addActionListener(this); …

23 Example import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestActionEvent extends JFrame implements ActionListener { // Create two buttons private JButton jbtOk = new JButton("OK"); private JButton jbtCancel = new JButton("Cancel"); public TestActionEvent() { // Set the window title setTitle("TestActionEvent"); // Set FlowLayout manager to arrange the components inside the frame getContentPane().setLayout(new FlowLayout()); // Add buttons to the frame getContentPane().add(jbtOk); getContentPane().add(jbtCancel); // Create a listener object //ButtonListener btListener = new ButtonListener(); // Register listeners jbtOk.addActionListener(this); jbtCancel.addActionListener(this); } /** Main method */ public static void main(String[] args) { TestActionEvent frame = new TestActionEvent(); frame.setDefaultCloseOperation(JFrame.E XIT_ON_CLOSE); frame.setSize(100, 80); frame.setVisible(true); } /** This method will be invoked when a button is clicked */ public void actionPerformed(ActionEvent e) { System.out.println("The " + e.getActionCommand() + " button is " + "clicked at\n " + new java.util.Date(e.getWhen())); }

24 Getting and Setting the value of a TextField Getting a value from a Textfield String s; s= TheTextField.getText(); Setting the value in a TextField TheTextField.setText(“A value”);

25 Summary AWT and Swing API Containers – JFrame Labels, TextFields and Buttons Layout Managers Graphics commands Handling Events Layout Managers


Download ppt "GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout."

Similar presentations


Ads by Google