1 / 16

GUI Development

Command line Input: Scanner class Output: System.out.println () GUI Components Input: Text Fields, Mouse Clicks Output: Labels, Dialog Boxes Comparison: Command line versus GUIs Command line: Easy to learn, good for debugging Disadvantages: Inappropriate for production systems.

Download Presentation

GUI Development

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. Command line • Input: Scanner class • Output: System.out.println() • GUI Components • Input: Text Fields, Mouse Clicks • Output: Labels, Dialog Boxes • Comparison: Command line versus GUIs • Command line: Easy to learn, good for debugging • Disadvantages: Inappropriate for production systems GUI Development

  2. AWT (Abstract Window Toolkit) • Simple components, short learning curve • Operating system calls create components (heavy weight) • Single buffering • SWING • Complex components, lots of features, steep learning curve • Draws components locally (lightweight, consistent cross platform look & feel) • Double buffering for increased responsiveness • Extends AWT, often overriding methods • Note: JavaFX and Androids don’t use Swing for mobile devices Java Gui development

  3. SWING class hierarchy Heavyweight Lightweight

  4. AWT class hierarchy

  5. These could help Game Components • JApplet • JPanel • JButton • JLabel • LayoutManager • BorderLayout • BoxLayout • JOptionPane • JTextField • JSlider We’ll definitely use these Parent classes • Component • Container • JComponent • Window • Frame • Applet

  6. creating a Japplet Creating a JFrame • Extend; code the init method • Get the content pane • Create Components • Add components to the content pane • If draggable, set title and possibly the icon • Set the size • Override Japplet methods as needed • Instantiate or extend • Get the content pane • Create Components • Add components to the content pane • Set title, possibly icon • Set the size • Set location in window • Set close operation • Set visible Japplet and JFrame

  7. Definition: A sub-panel that holds components • Creating a JPanel • Instantiate (new JPanel()) • Create and add components (add(new JButton("X")); • If desired • Set background and foreground colors • Set a border (setBorder method) • Set size, preferred size, minimum size, maximum size • Override the paintComponent method if necessaryNote: when overriding paintCompnent, you should call super.paintComponent(g); jpanel

  8. Definition: An object that controls how components are drawn in a panel • Manager classes • Flow, Border, Box, Grid, GridBag, Overlay, and others • Default if none specified: Flow • Purpose: Abstracts component positioning so appearance will correctly adapt to different platforms. Layout managers

  9. public class ShowBorder extends JFrame { public static void main(String[] args) { JFrame f = new ShowBorder(); Container c = f.getContentPane(); c.setLayout(new BorderLayout(5,10)); c.add(new JButton("East"), BorderLayout.EAST); c.add(new JButton("West"), BorderLayout.WEST); c.add(new JButton("North"), BorderLayout.NORTH); c.add(new JButton("South"), BorderLayout.SOUTH); c.add(new JButton("Center"), BorderLayout.CENTER); f.setTitle("Show Border Layout"); f.setSize(new Dimension(400, 200)); f.setLocationRelativeTo(null); // Center on screen f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } Borderlayout Note: setImageIcon can replace the Java coffee cup

  10. Box Layout public class ShowBox extends JFrame { public static void main(String[] args){ JFrame f = new ShowBox(); Container c = f.getContentPane(); c.setLayout(new BoxLayout(c, BoxLayout.X_AXIS)); c.add(new JButton("First")); c.add(Box.createHorizontalStrut(10)); c.add(new JButton("Second")); c.add(Box.createHorizontalGlue()); c.add(new JButton(“Third")); f.setTitle("Show Box Layout"); f.setSize(new Dimension(400, 75)); f.setLocationRelativeTo(null); // Center on screen f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true);} } Note: Box Layouts can be either horizontal or vertical)

  11. Instantiate: JButton button = new JButton(“Reset"); • Background Color: button.setBackground(new Color(0,0x99,0xFF)); • Foreground Color: button.setForeground(Color.WHITE); • Border: button.setBorder (BorderFactory.createEtchedBorder(EtchedBorder.RAISED)); • Tool tip: button.setToolTipText("This is a tool tip"); • Change Font: button.setFont(new Font("Arial", Font.BOLD, 25)); • Set size of button: button.setSize(BUTTON_SIZE); • Set preferred size: button.setPreferredSize(BUTTON_SIZE); • Set the minimum size: button.setMinimumSize(BUTTON_SIZE); • Set the maximum size: button.setMaximumSize(BUTTON_SIZE); • Listen for button clicks: button.addActionListener(this); jbutton • Notes: • 1) The setText method can change the button label • Buttons can also be created with icons (ImageIcon object) • Listener methods respond to user interactions (week 5 slides)

  12. label = new JLabel("Points: " + 0); label.setBackground (new Color(0x33, 0x66, 0xcc)); label.setForeground(Color.WHITE); label.setOpaque(true); // The default is false. label.setBorder(BorderFactory.createEtchedBorder (EtchedBorder.RAISED)); label.setPreferredSize(new Dimension(100,30)); jlabel To change a label: label: .setText ("Points: " + 20); To get a label’s text: String text = label.getText();

  13. Purposes: • Display messages in a production system • Create dialog boxes for gathering user input • JOptionPane class structure: a group of static methods providing a variety of options • Example: • JOptionPane.showMessageDialog (this, "this is a message for the user"); • Note: The first argument of showMessageDialog is a GUI component that the message will always appear on top. A null argument is legal, but then the message could go behind the component. joptionpane

  14. JSlider slider = new JSlider(JSlider.VERTICAL); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setMajorTickSpacing(10); slider.setMinorTickSpacing(1); slider.addChangeListener( new ChangeListener() { public void stateChanged (ChangeEvent e) { // *** Insert listener code here ** } }); jslider Note: Horizontal Jsliders are also possible Note: background/foreground color, border, font methods exist

  15. JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JTextField field = new JTextField("Initial data"); field.setPreferredSize(new Dimension(200,30)); field.setMaximumSize(field.getPreferredSize()); JLabel label = new JLabel("Enter Something"); panel.add(label); panel.add(Box.createHorizontalStrut(10)); panel.add(field); field.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Add listener logic here } }); JTextField

  16. JCheckBox, JRadioButton, JTextArea, JComboBox, JScrollBar, JTable, JTree, Many others • All swing components follow the same general principles for instantiating, sizing, setting colors/fonts, adding listeners, etc. as was described on the previous slides Other common Swing components

More Related