Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 233 Tutorial #17. GUI programming Objective – To understand better how to create simple interactive GUI programs.

Similar presentations


Presentation on theme: "CPSC 233 Tutorial #17. GUI programming Objective – To understand better how to create simple interactive GUI programs."— Presentation transcript:

1 CPSC 233 Tutorial #17

2 GUI programming Objective – To understand better how to create simple interactive GUI programs.

3 import javax.swing.*; import java.awt.*; public class Example { JFrame frame; JButton b; public static void main(String[] args) { Example gui = new Example(); gui.go(); } public void go() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b = new JButton("A"); b.addActionListener(); frame.getContentPane().add(BorderLayout.SOUTH, b); frame.setSize(200,100); frame.setVisible(true); } Be the compiler class BListener extends ActionListener { public void actionPerformed(ActionEvent e) { if (b.getText().equals("A")) b.setText("B"); else b.setText("A"); } import java.awt.event.*; new BListener() implements

4 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener{ public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int NUMBER_OF_DIGITS = 30; private JTextField ioField; private double result = 0.0; public Calculator() { setTitle("Simplified Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setLayout(new BorderLayout()); JPanel textPanel = new JPanel(); textPanel.setLayout(new FlowLayout()); ioField = new JTextField("Enter numbers here.", NUMBER_OF_DIGITS); ioField.setBackground(Color.WHITE); textPanel.add(ioField); add(textPanel, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.GRAY); buttonPanel.setLayout(new FlowLayout()); Display 17.19

5 JButton addButton = new JButton("+"); addButton.addActionListener(this); buttonPanel.add(addButton); JButton subtractButton = new JButton("-"); subtractButton.addActionListener(this); buttonPanel.add(subtractButton); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(this); buttonPanel.add(resetButton); add(buttonPanel, BorderLayout.CENTER); } public static void main(String[] args) { Calculator aCalculator = new Calculator(); aCalculator.setVisible(true); } public void actionPerformed(ActionEvent e) { try { assumingCorrectNumberFormats(e); } catch (NumberFormatException e2) { ioField.setText("Error: Re-enter Number."); } public void assumingCorrectNumberFormats(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("+")){ result = result + stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } else if (actionCommand.equals("-")){ result = result - stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } else if (actionCommand.equals("Reset")){ result = 0.0; ioField.setText ("0.0"); } else ioField.setText("Unexpected error."); } private static double stringToDouble(String stringObject){ return Double.parseDouble(stringObject.trim()); }

6

7 Questions Why did we make the text field ioField an instance variable but did not make instance variables of the other buttons, addButton, subtractButton, or resetButton?

8 What would happen if the user running the GUI were to run the GUI and simply click the addition button without typing anything into the text field?

9 What would happen if the user running the GUI were to type the number 10 into the text field and then click the addition button three times?

10 What would happen if you change the main method to: public static void main(String[] args) { Calculator calculator1 = new Calculator(); Calculator1.setVisible(true); Calculator calculator2 = new Calculator(); Calculator2.setVisible(true); } If you click the close-window button in one of the windows, will one window go away or will both windows go away?

11 Exercise Add multiplication and division to the simple calculator Create a real simple calculator with digits and operators as separate buttons

12 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Calculator extends JFrame implements ActionListener{ public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int NUMBER_OF_DIGITS = 30; private JTextField ioField; private double result = 0.0; public Calculator() { setTitle("Simplified Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setLayout(new BorderLayout()); JPanel textPanel = new JPanel(); textPanel.setLayout(new FlowLayout()); ioField = new JTextField("Enter numbers here.", NUMBER_OF_DIGITS); ioField.setBackground(Color.WHITE); textPanel.add(ioField); add(textPanel, BorderLayout.NORTH); JPanel buttonPanel = new JPanel(); buttonPanel.setBackground(Color.GRAY); buttonPanel.setLayout(new FlowLayout()); Display 17.19

13 JButton addButton = new JButton("+"); addButton.addActionListener(this); buttonPanel.add(addButton); JButton subtractButton = new JButton("-"); subtractButton.addActionListener(this); buttonPanel.add(subtractButton); JButton resetButton = new JButton("Reset"); resetButton.addActionListener(this); buttonPanel.add(resetButton); add(buttonPanel, BorderLayout.CENTER); } public static void main(String[] args) { Calculator aCalculator = new Calculator(); aCalculator.setVisible(true); } public void actionPerformed(ActionEvent e) { try { assumingCorrectNumberFormats(e); } catch (NumberFormatException e2) { ioField.setText("Error: Re-enter Number."); } public void assumingCorrectNumberFormats(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("+")){ result = result + stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } else if (actionCommand.equals("-")){ result = result - stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } else if (actionCommand.equals("Reset")){ result = 0.0; ioField.setText ("0.0"); } else ioField.setText("Unexpected error."); } private static double stringToDouble(String stringObject){ return Double.parseDouble(stringObject.trim()); }

14 import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class TextFieldDemo extends JFrame implements ActionListener{ public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int NUMBER_OF_CHAR = 30; private JTextField name; public TextFieldDemo() { super("Text Field Demo"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(2,1)); JPanel namePanel = new JPanel(); namePanel.setLayout(new BorderLayout()); namePanel.setBackground(Color.WHITE); name = new JTextField(NUMBER_OF_CHAR); namePanel.add(name, BorderLayout.SOUTH); JLabel nameLabel = new JLabel("Enter your name here"); namePanel.add(nameLabel, BorderLayout.CENTER); add(namePanel); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.setBackground(Color.GREEN); JButton actionButton = new JButton("Click me"); actionButton.addActionListener(this); buttonPanel.add(actionButton); JButton clearButton = new JButton("Clear"); clearButton.addActionListener(this); buttonPanel.add(clearButton); add(buttonPanel); } public void actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand(); if (actionCommand.equals("Click me")) name.setText("Hello " + name.getText()); else if (actionCommand.equals("Clear")) name.setText(""); else name.setText("Unexpected error."); } public static void main(String[] args) { TextFieldDemo gui = new TextFieldDemo(); gui.setVisible(true); } Display 17.17

15

16 Questions What is the difference between an object of the class JTextArea and an object of the JTextField?

17 What would happen if when running the GUI and you enter your name, then click the “Click me” button three times?

18 import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class TextFieldDemo extends JFrame implements ActionListener{ public static final int WIDTH = 400; public static final int HEIGHT = 200; public static final int NUMBER_OF_CHAR = 30; private JTextField name; public TextFieldDemo() { super("Text Field Demo"); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(2,1)); JPanel namePanel = new JPanel(); namePanel.setLayout(new BorderLayout()); namePanel.setBackground(Color.WHITE); name = new JTextField(NUMBER_OF_CHAR); namePanel.add(name, BorderLayout.SOUTH); JLabel nameLabel = new JLabel("Enter your name here"); namePanel.add(nameLabel, BorderLayout.CENTER); add(namePanel); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.setBackground(Color.GREEN); JButton actionButton = new JButton("Click me"); actionButton.addActionListener(this); buttonPanel.add(actionButton); JButton clearButton = new JButton("Clear"); clearButton.addActionListener(this); buttonPanel.add(clearButton); add(buttonPanel); } public void actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand(); if (actionCommand.equals("Click me")) name.setText("Hello " + name.getText()); else if (actionCommand.equals("Clear")) name.setText(""); else name.setText("Unexpected error."); } public static void main(String[] args) { TextFieldDemo gui = new TextFieldDemo(); gui.setVisible(true); } Display 17.17


Download ppt "CPSC 233 Tutorial #17. GUI programming Objective – To understand better how to create simple interactive GUI programs."

Similar presentations


Ads by Google