1 / 18

CPSC 233 Tutorial #17

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

Télécharger la présentation

CPSC 233 Tutorial #17

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC 233 Tutorial #17

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

  3. 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()

  4. 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());

  5. 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."); } }

  6. 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?

  7. 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?

  8. 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?

  9. 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?

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

  11. 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());

  12. 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."); } }

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

  14. Questions • What is the difference between an object of the class JTextArea and an object of the JTextField?

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

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

More Related