Download presentation
Presentation is loading. Please wait.
Published byChrystal Taylor Modified over 8 years ago
1
GUI Programming in Java Hao Jiang Boston College April, 2009
2
Java GUI Programming GUI indicates Graphics User Interface, which contrasts with the text-only interfaces, e.g. a terminal. GUI elements include –“window”, “menu”, “button”, “text box”, “check box”… –Key and mouse interactions “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.
3
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); }
4
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); }
5
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); g2.setPaint(new Color(0, 0, 0)); g2.setFont(new Font("Helvetica", Font.BOLD, 40)); g2.drawString("Hello World", 30, 230); }
6
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); } Exception handling try {…} catch (Exception e) { … }
7
Event Handling 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.
8
Event Handling in Java Event Source Event Source Event Listener Event Listener Interface Listener Interface 1 … * >
9
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 }
10
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)); } 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); } Button Event class MyPanel extends JPanel { public MyPanel(ActionListener f) { JButton redButton = new JButton("red"); redButton.addActionListener(f); JButton blueButton = new JButton("blue"); blueButton.addActionListener(f); add(redButton); add(blueButton); }
11
Using Inner Class 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()); } class MyListener implements ActionListener { private Color color; public MyListener(Color c) { color = c; } public void actionPerformed(ActionEvent e) { setBackground(color); } Inner class
12
Using Inner Class (Cont) 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); }
13
More Events and Listeners AWT events: ActionEvent KeyEvent AdjustmentEvent MouseEvent FocusEvent MouseWheelEvent ItemEvent WindowEvent Listeners: ActionListener MouseMotionListener AdjustmentListener MouseWheelListener FocusListener WindowListener ItemListener WindowFocusListener KeyListener WindowStateListener MouseListener Adaptors WindowAdapter
14
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()); } 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")); } public static void main(String[] args) { ComFrame frame = new ComFrame(); frame.setVisible(true); }
15
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); }
16
Animation
17
GUI Using StdDraw
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.