GUI Programming in Java Java GUI Programming An Empty ...

9 downloads 356062 Views 944KB Size Report
GUI indicates Graphics User Interface, which contrasts with the ... problems in programming GUI. ▫ For GUI programming in Java, we can use AWT (Abstract.
Java GUI Programming !! GUI indicates Graphics User Interface, which contrasts with the text-only interfaces, e.g. a terminal.

GUI Programming in Java

!! GUI elements include –! “window”, “menu”, “button”, “text box”, “check box”… –! Key and mouse interactions

Hao Jiang Boston College April, 2009

!! “Components” and “events handling” are two key problems in programming GUI. !! For GUI programming in Java, we can use AWT (Abstract Window Toolkit) and SWING.

An Empty Window import javax.swing.*; public class SimpleFrame extends JFrame { public SimpleFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { SimpleFrame frame = new SimpleFrame(); frame.setVisible(true); } }

Hello World import javax.swing.*; import java.awt.*; public class HelloFrame extends JFrame { public HelloFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new MyPanel()); } public static void main(String[] args) { HelloFrame frame = new HelloFrame(); frame.setVisible(true); } } class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("Hello World", 50, 50); } }

Drawing in Panel import java.awt.geom.*; class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; Rectangle2D rect = new Rectangle2D.Double(50, 50, 100, 100); g2.draw(rect); Rectangle2D frect = new Rectangle2D.Double(150, 50, 100, 100); g2.setPaint(new Color(255, 0, 0)); g2.fill(frect); Ellipse2D e = new Ellipse2D.Double(); e.setFrame(rect); g2.setPaint(new Color(0, 0, 255)); g2.fill(e);

Display an Image class MyPanel extends JPanel { private Image image; public MyPanel(String imageName) { try { image = ImageIO.read(new File(imageName)); } catch (Exception e) { e.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); if (image == null) return; g.drawImage(image, 0, 0, null); }

g2.setPaint(new Color(0, 0, 0)); g2.setFont(new Font("Helvetica", Font.BOLD, 40)); g2.drawString("Hello World", 30, 230); }

Exception handling try {…} catch (Exception e) {…}

}

}

Event Handling

Event Handling in Java

!! Different events need to be processed in GUI based programs. –! –! –! –! –! –!

Mouse clicked, dragged, pressed, released Window resized, moved, closed Slider bar changed Check box checked Text box input changed And many others

!! In Java, event sources pass event objects to event listeners; event listeners process the event objects and generate some actions.

Event Source

1…*

Event Listener Listener Interface

An Example ActionListener listener = new MyListener(); JButton button = new JButton(“OK”); button.addActionListener(listener); …

class MyListener implements ActionListener { public void actionPerformed(ActionEvent event) { // reaction to the button click } }

Button Event

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class ButtonFrame extends JFrame implements ActionListener { public ButtonFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new MyPanel(this)); } class MyPanel extends JPanel public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); if (cmd.equals("red")) setBackground(Color.RED); if (cmd.equals("blue")) setBackground(Color.BLUE); } public static void main(String[] args) { ButtonFrame frame = new ButtonFrame(); frame.setVisible(true); }

{ public MyPanel(ActionListener f) { JButton redButton = new JButton("red"); redButton.addActionListener(f); JButton blueButton = new JButton("blue"); blueButton.addActionListener(f); add(redButton); add(blueButton); } }

}

Using Inner Class

Using Inner Class (Cont)

import javax.swing.*; import java.awt.*; import java.awt.event.*; public class InnerFrame extends JFrame { public InnerFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new MyPanel()); }

Inner class

class MyListener implements ActionListener { private Color color; public MyListener(Color c) { color = c; } public void actionPerformed(ActionEvent e) { setBackground(color); } }

class MyPanel extends JPanel { public MyPanel() { JButton redButton = new JButton("red"); redButton.addActionListener(new MyListener(Color.RED)); JButton blueButton = new JButton("blue"); blueButton.addActionListener(new MyListener(Color.BLUE)); add(redButton); add(blueButton); } } public static void main(String[] args) { InnerFrame frame = new InnerFrame(); frame.setVisible(true); } }

More Events and Listeners AWT events: ActionEvent AdjustmentEvent FocusEvent ItemEvent Listeners: ActionListener AdjustmentListener FocusListener ItemListener KeyListener MouseListener

KeyEvent MouseEvent MouseWheelEvent WindowEvent

MouseMotionListener MouseWheelListener WindowListener WindowFocusListener WindowStateListener

Adaptors WindowAdapter

Component Layout class MyPanel extends JPanel { public MyPanel() { setLayout(new BorderLayout()); add(new JButton(""), BorderLayout.NORTH); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(4, 4)); panel.add(new JButton("7")); panel.add(new JButton("8")); panel.add(new JButton("9")); panel.add(new JButton("/")); panel.add(new JButton("4")); panel.add(new JButton("5")); panel.add(new JButton("6")); panel.add(new JButton("*")); panel.add(new JButton("1")); panel.add(new JButton("2")); panel.add(new JButton("3")); panel.add(new JButton("-")); panel.add(new JButton("0")); panel.add(new JButton(".")); panel.add(new JButton("=")); panel.add(new JButton("+")); add(panel, BorderLayout.CENTER); } }

GUI Components import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ComFrame extends JFrame { public ComFrame() { setSize(300, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(new MyPanel()); }

public static void main(String[] args) { ComFrame frame = new ComFrame(); frame.setVisible(true); } }

class MyPanel extends JPanel { public MyPanel() { JButton redButton = new JButton("red"); add(redButton); add(new JLabel("Label")); add(new JTextField("test", 5)); add(new JCheckBox("Bold")); } }

Animation

GUI Using StdDraw