160 likes | 301 Views
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.
E N D
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
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
SWING class hierarchy Heavyweight Lightweight
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
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
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
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
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
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)
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)
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();
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
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
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
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