16
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

Command line Input: Scanner class Output: System.out.println() GUI Components Input: Text Fields, Mouse Clicks Output: Labels, Dialog Boxes

Embed Size (px)

Citation preview

Page 1: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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

Page 2: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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

Page 3: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

Heavyweight

Lightweight

Page 4: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes
Page 5: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

JApplet JPanel JButton JLabel LayoutManager

BorderLayout BoxLayout

JOptionPane JTextField JSlider

These could help

We’ll definitely use these

Parent classes Component Container JComponent Window Frame Applet

Page 6: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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

Page 7: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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 necessary

Note: when overriding paintCompnent, you should call super.paintComponent(g);

Page 8: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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.

Page 9: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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);

} }Note: setImageIcon can replace the Java coffee cup

Page 10: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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)

Page 11: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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);

Notes: 1) The setText method can change the button label2)Buttons can also be created with icons (ImageIcon object)3)Listener methods respond to user interactions (week 5 slides)

Page 12: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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));

To change a label: label: .setText ("Points: " + 20);To get a label’s text: String text = label.getText();

Page 13: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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.

Page 14: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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 ** }

});

Note: Horizontal Jsliders are also possibleNote: background/foreground color, border, font methods exist

Page 15: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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 } });

Page 16: Command line  Input: Scanner class  Output: System.out.println()  GUI Components  Input: Text Fields, Mouse Clicks  Output: Labels, Dialog Boxes

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