180 likes | 199 Views
CPSC 233 Tutorial #17. GUI programming. Objective To understand better how to create simple interactive GUI programs. Be the compiler. implements. class BListener extends ActionListener { public void actionPerformed(ActionEvent e) { if (b.getText().equals("A"))
E N D
GUI programming • Objective • To understand better how to create simple interactive GUI programs.
Be the compiler implements class BListener extends ActionListener { publicvoid actionPerformed(ActionEvent e) { if (b.getText().equals("A")) b.setText("B"); else b.setText("A"); } } } import java.awt.event.*; import javax.swing.*; import java.awt.*; publicclass Example { JFrame frame; JButton b; publicstaticvoid main(String[] args) { Example gui = new Example(); gui.go(); } publicvoid 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); } new BListener()
Display 17.19 importjavax.swing.*; import java.awt.*; importjava.awt.event.*; publicclass Calculator extendsJFrameimplementsActionListener{ publicstaticfinalintWIDTH = 400; publicstaticfinalintHEIGHT = 200; publicstaticfinalintNUMBER_OF_DIGITS = 30; privateJTextFieldioField; privatedouble result = 0.0; public Calculator() { setTitle("Simplified Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setLayout(newBorderLayout()); JPaneltextPanel = newJPanel(); textPanel.setLayout(newFlowLayout()); ioField = newJTextField("Enter numbers here.", NUMBER_OF_DIGITS); ioField.setBackground(Color.WHITE); textPanel.add(ioField); add(textPanel, BorderLayout.NORTH); JPanelbuttonPanel = newJPanel(); buttonPanel.setBackground(Color.GRAY); buttonPanel.setLayout(newFlowLayout());
publicvoid assumingCorrectNumberFormats(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("+")){ result = result + stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } elseif (actionCommand.equals("-")){ result = result - stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } elseif (actionCommand.equals("Reset")){ result = 0.0; ioField.setText ("0.0"); } else ioField.setText("Unexpected error."); } privatestaticdouble stringToDouble(String stringObject){ return Double.parseDouble(stringObject.trim()); } } 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); } publicstaticvoid main(String[] args) { Calculator aCalculator = new Calculator(); aCalculator.setVisible(true); } publicvoid actionPerformed(ActionEvent e) { try { assumingCorrectNumberFormats(e); } catch (NumberFormatException e2) { ioField.setText("Error: Re-enter Number."); } }
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?
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?
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?
What would happen if you change the main method to: public static voidmain(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?
Exercise • Add multiplication and division to the simple calculator • Create a real simple calculator with digits and operators as separate buttons
Display 17.19 importjavax.swing.*; import java.awt.*; importjava.awt.event.*; publicclass Calculator extendsJFrameimplementsActionListener{ publicstaticfinalintWIDTH = 400; publicstaticfinalintHEIGHT = 200; publicstaticfinalintNUMBER_OF_DIGITS = 30; privateJTextFieldioField; privatedouble result = 0.0; public Calculator() { setTitle("Simplified Calculator"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(WIDTH, HEIGHT); setLayout(newBorderLayout()); JPaneltextPanel = newJPanel(); textPanel.setLayout(newFlowLayout()); ioField = newJTextField("Enter numbers here.", NUMBER_OF_DIGITS); ioField.setBackground(Color.WHITE); textPanel.add(ioField); add(textPanel, BorderLayout.NORTH); JPanelbuttonPanel = newJPanel(); buttonPanel.setBackground(Color.GRAY); buttonPanel.setLayout(newFlowLayout());
publicvoid assumingCorrectNumberFormats(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("+")){ result = result + stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } elseif (actionCommand.equals("-")){ result = result - stringToDouble(ioField.getText()); ioField.setText(Double.toString(result)); } elseif (actionCommand.equals("Reset")){ result = 0.0; ioField.setText ("0.0"); } else ioField.setText("Unexpected error."); } privatestaticdouble stringToDouble(String stringObject){ return Double.parseDouble(stringObject.trim()); } } 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); } publicstaticvoid main(String[] args) { Calculator aCalculator = new Calculator(); aCalculator.setVisible(true); } publicvoid actionPerformed(ActionEvent e) { try { assumingCorrectNumberFormats(e); } catch (NumberFormatException e2) { ioField.setText("Error: Re-enter Number."); } }
Display 17.17 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); } publicvoid actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand(); if (actionCommand.equals("Click me")) name.setText("Hello " + name.getText()); elseif (actionCommand.equals("Clear")) name.setText(""); else name.setText("Unexpected error."); } publicstaticvoid main(String[] args) { TextFieldDemo gui = new TextFieldDemo(); gui.setVisible(true); } } import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; publicclassTextFieldDemoextends JFrame implements ActionListener{ publicstaticfinalintWIDTH = 400; publicstaticfinalintHEIGHT = 200; publicstaticfinalintNUMBER_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);
Questions • What is the difference between an object of the class JTextArea and an object of the JTextField?
What would happen if when running the GUI and you enter your name, then click the “Click me” button three times?
Display 17.17 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); } publicvoid actionPerformed(ActionEvent e){ String actionCommand = e.getActionCommand(); if (actionCommand.equals("Click me")) name.setText("Hello " + name.getText()); elseif (actionCommand.equals("Clear")) name.setText(""); else name.setText("Unexpected error."); } publicstaticvoid main(String[] args) { TextFieldDemo gui = new TextFieldDemo(); gui.setVisible(true); } } import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; publicclassTextFieldDemoextends JFrame implements ActionListener{ publicstaticfinalintWIDTH = 400; publicstaticfinalintHEIGHT = 200; publicstaticfinalintNUMBER_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);