1 / 51

F27SB2 Software Development 2

F27SB2 Software Development 2. Lecture 5: Java GUIs 4. Example: counter. display number in JLabel JButton s to increment/decrement number. JFrame. number. JLabel. JPanel. JButton. -. +. JButton. Example: counter. class Counter extends JFrame implements ActionListener

diella
Télécharger la présentation

F27SB2 Software Development 2

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. F27SB2 Software Development 2 Lecture 5: Java GUIs 4

  2. Example: counter • display number in JLabel • JButtons to increment/decrement number JFrame number JLabel JPanel JButton - + JButton

  3. Example: counter class Counter extends JFrame implements ActionListener { JLabel l; JButtonup,down; JPanel p; int value = 0; public Counter() { setLayout(new GridLayout(2,1)); Font f = new Font("Serif",Font.ITALIC,36); l = new JLabel("0",JLabel.CENTER); l.setFont(f); l.setBackground(Color.white); l.setOpaque(true); add(l);

  4. Example: counter p = new JPanel(new GridLayout(1,2)); up = new JButton("+"); up.setFont(f); up.setBackground(Color.white); up.setOpaque(true); p.add(up); up.addActionListener(this); down = new JButton("-"); down.setFont(f); down.setBackground(Color.white); down.setOpaque(true); p.add(down); down.addActionListener(this);

  5. Example: counter add(p); } public void actionPerformed(ActionEvent e) { if(e.getSource()==up) value++; else if(e.getSource()==down) value--; l.setText(value+""); } } class TestCounter { ... }

  6. Example: counter 2 • add JButton to reset number to 0 JFrame number JLabel JPanel JButton 0 - + JButton JButton

  7. Example: counter 2 class Counter2 extends JFrame implements ActionListener { JLabel l; JButtonup,down,zero; JPanel p; int value = 0; public Counter2() { setLayout(new GridLayout(2,1)); Font f = new Font("Serif",Font.ITALIC,18); l = new JLabel("0",JLabel.CENTER); l.setFont(f); l.setBackground(Color.white); add(l);

  8. Example: counter 2 p = new JPanel(new GridLayout(1,3)); up = new JButton("+"); ... down = new JButton("-"); ... zero = new JButton("0"); zero.setFont(f); zero.setBackground(Color.white); zero.setOpaque(true); p.add(zero); zero.addActionListener(this);

  9. Example: counter 2 add(p); } public void actionPerformed(ActionEvent e) { if(e.getSource()==up) value++; else if(e.getSource()==down) value--; else if(e.getSource()==zero) value=0; l.setText(value+""); } }

  10. Example: counter 2 class TestCounter2 { ... }

  11. Method or sub-class? • counter2 has lots of repeated code to set up JButtons • generalise by constructing a new method: class Counter3 extends JFrame implements ActionListener { ... JButton C3Button(String text) { JButton b = new JButton(text); b.setFont(newFont("Serif",Font.ITALIC,36)); b.setBackground(Color.white); b.setOpaque(true); return b; }

  12. Method or sub-class? public Counter3() { ... p = new JPanel(new GridLayout(1,3)); up = C3Button("+"); up.addActionListener(this); p.add(up); down = C3Button("-"); down.addActionListener(this); p.add(down); zero = C3Button("0"); zero.addActionListener(this); p.add(zero); add(p); }

  13. Method or sub-class? • recognise that we are actually constructing a specialised variant of JButton • instead of writing method, sub-class JButton class C4Button extends JButton { C4Button(String text) { super(text); setFont(new Font("Serif",Font.ITALIC,36)); setBackground(Color.white); setOpaque(true); } }

  14. Method or sub-class? • C4Buttonis a new class which inherits all properties of the super class JButton • super(text) calls the super-class constructor JButton(text) to make a new JButtonobject • setFontand setBackground now implicitly affect the new JButton object created by super

  15. Method or sub-class? • can now use a C4Button anywhere we can use a JButton class Counter4 extends JFrame implements ActionListener { JLabel l; C4Button up,down,zero; JPanel p; int value = 0;

  16. Method or sub-class? public Counter4() { setLayout(new GridLayout(2,1)); l = new JLabel("0",JLabel.CENTER); l.setFont(new Font("Serif",Font.ITALIC,36)); l.setBackground(Color.white); l.setOpaque(true); add(l); p = new JPanel(new GridLayout(1,3));

  17. Method or sub-class? up = new C4Button("+"); up.addActionListener(this); p.add(up); down = new C4Button("-"); down.addActionListener(this); p.add(down); zero = new C4Button("0"); zero.addActionListener(this); p.add(zero); add(p); }

  18. Text Field • often want to input arbitrary information as text via an interface public class JTextField extends JTextComponent JTextField() • like a JLabel • displays a single line of optionally editable text

  19. Text Field JTextField(intcolumns) JTextField(String text) JTextField(String text,intcolumns) • columns => set maximum text width cursor • text=> set initial text getText() • return text at any time setText(String text) • change text cursor columns

  20. Text Field • use mouse to select JTextField for text entry • press return to cause: ActionEvent • detected by: ActionListener • handled by: ActionPerformed

  21. Example: number guessing • computer “thinks” of a number between 1 and 100 • user enters their guess as text • computer says if guess is high, low or correct JFrame responses JLabel JPanel JLabel inputt prompt JTextField

  22. Example: number guessing • use BorderLayout forJFrame • default • add to north & centre • use FlowLayout for JPanel • default • prompt will be much bigger than input

  23. Example: number guessing class Guess extends JFrame implements ActionListener { long number; JLabelresponse,prompt; JTextField input; JPanel p; long abs(long x) { if(x<0) return -x; return x; }

  24. Example: number guessing public Guess() { number = abs(new Random().nextInt())%100+1; Font f = new Font("serif",Font.ITALIC,18); response = new JLabel("I'm thinking of a number between 1 and 100", JLabel.CENTER); response.setBackground(Color.white); response.setFont(f); response.setOpaque(true); add(BorderLayout.NORTH,response); p = new JPanel();

  25. Example: number guessing prompt = new JLabel("Enter your guess and press return", JLabel.CENTER); prompt.setFont(f); prompt.setBackground(Color.white); prompt.setOpaque(true); p.add(prompt); input = new JTextField(3); input.setOpaque(true); p.add(input); input.addActionListener(this); add(BorderLayout.CENTER,p); }

  26. Example: number guessing public void actionPerformed(ActionEvent e) { if(e.getSource()==input) { int guess = Integer.parseInt(input.getText()); if(guess==number) response.setText("Correct!"); else if(guess<number) response.setText("Low..."); else response.setText("High..."); } } }

  27. Example: number guessing class TestGuess { ... }

  28. Dynamic interface changes • at the end of a guessing session offer options • play again • stop • introduce “more”/“stop” JButtons? • could: • add new JButtons to current JPanel • disable JButtons during play • enable JButtons and disable JTextField at end

  29. Dynamic interface changes • GUI will become cluttered • could switch current JPanel with new JPanelfor “more”/“stop” only remove(Component c) • removes Componentcfrom Container • can then add new Component to Container • NB need to setVisible(false) for old Component and setVisible(true) for new Component

  30. Dynamic interface change JFrame responses JLabel JPanel JLabel stopt moret play JButton JButton

  31. Dynamic interface changes class Guess2 extends JFrame implements ActionListener { long number; JLabelresponse,prompt; JTextField input; JPanel p1,p2; JButtonstop,more; JLabel play; ...

  32. Dynamic interface changes JLabelsetupLabel(String s,JPanelp) { JLabell = new JLabel(s,JLabel.CENTER); l.setFont(new Font("serif",Font.ITALIC,18)); l.setBackground(Color.white); p.add(l); return l; } JButtonsetupButton(String s,JPanelp) { JButtonb = new JButton(s); b.setFont(new Font("serif",Font.ITALIC,18)); b.setBackground(Color.white); p.add(b); b.addActionListener(this); return b; }

  33. Dynamic interface changes public Guess2() { ... p2 = new JPanel(); play = setupLabel("Play again?",p2); stop =setupButton("STOP",p2); more = setupButton("MORE",p2); }

  34. Dynamic interface changes public void actionPerformed(ActionEvent e) { if(e.getSource()==input) { int guess = Integer.parseInt(input.getText()); if(guess==number) { response.setText("Correct!"); remove(p1); add(BorderLayout.CENTER,p2); p1.setVisible(false);p2.setVisible(true); } else if(guess<number) response.setText("Low..."); else response.setText("High..."); }

  35. Dynamic interface changes else if(e.getSource()==stop)System.exit(0); else if(e.getSource()==more) { remove(p2); add(BorderLayout.CENTER,p1); p2.setVisible(false); p1.setVisible(true); response.setText("I'm thinking of number between 1 and 100"); number = abs(new Random().nextInt())%100+1; input.setText(""); } } }

  36. Dynamic interface changes class TestGuess2 { ... }

  37. Example: form JFrame • consider a form to capture • given name • family name JTextField given text family text JLabel email text comment submit JButton

  38. Example: form • check all JTextFields have entries • error message in comment • write all entries to file • offer more/stop replacing comment/submit • more • offer comment/submit replacing more/stop • clear all JTextFields • stop • close file

  39. Example: form • NB ignore events from JTextFields class Form extends JFrame implements ActionListener { JLabelgiven,family,email,comment; JTextFieldgt,ft,et; JButtonsubmit,more,stop; PrintWriter file;

  40. Example: form • new methods to set up JLabel/JButton/ JTextField • add new object to a Container c JLabelsetupLabel(String s,intf,Container c) { JLabel l = new JLabel(s,JLabel.CENTER); l.setFont(new Font("serif",f,18)); l.setBackground(Color.white); l.setOpaque(true); c.add(l); return l; }

  41. Example: form JButtonsetupButton(String s,Container c) { JButton b = new JButton(s); b.setFont(new Font("serif",Font.ITALIC,18)); b.setBackground(Color.white); b.setOpaque(true); c.add(b); b.addActionListener(this); return b; }

  42. Example: form JTextFieldsetupTextField(Container c) { JTextField t = new JTextField(); t.setFont(new Font("sanserif",Font.PLAIN,18)); t.setBackground(Color.white); t.setOpaque(true); c.add(t); return t; }

  43. Example: form public Form() { setLayout(new GridLayout(4,2)); given = setupLabel("Given name",Font.PLAIN,this); gt = setupTextField(this); family = setupLabel("Family name",Font.PLAIN,this); ft = setupTextField(this); email = setupLabel("Email address",Font.PLAIN,this); et = setupTextField(this); comment = setupLabel("",Font.ITALIC,this); submit = setupButton("SUBMIT",this);

  44. Example: form • set up moreand stop but don’t add them more = new JButton("MORE"); more.setFont(new Font("serif",Font.ITALIC,18)); more.setBackground(Color.white); more.setOpaque(true); more.addActionListener(this); stop = new JButton("STOP"); stop.setFont(new Font("serif",Font.ITALIC,18)); stop.setBackground(Color.white); stop.setOpaque(true); stop.addActionListener(this); }

  45. Example: form void doSubmit() { if(gt.getText().equals("")) comment.setText("Enter given name"); else if(ft.getText().equals("")) comment.setText("Enter family name"); else if(et.getText().equals("")) comment.setText("Enter email address"); else { file.println(gt.getText()); file.println(ft.getText()); file.println(et.getText());

  46. Example: form • replace comment/submit with more/stop remove(comment); comment.setVisible(false); remove(submit); submit.setVisible(false); add(more); more.setVisible(true); add(stop); stop.setVisible(true); setVisible(true); } }

  47. Example: form void doMore() { comment.setText(""); gt.setText(""); ft.setText(""); et.setText(""); • replace more/stop with comment/submit remove(more); more.setVisible(false); remove(stop); stop.setVisible(false); add(comment); comment.setVisible(true); add(submit); submit.setVisible(true); }

  48. Example: form void doStop() { file.close(); System.exit(0); } public void actionPerformed(ActionEvent e) { if(e.getSource()==submit) doSubmit(); else if(e.getSource()==more) doMore(); else if(e.getSource()==stop) doStop(); }

  49. Example: form public void setup() throws IOException { file = new PrintWriter (new FileWriter("register.log"),true); } } class TestForm { public static void main(String [] args) throws IOException { Form f; f = new Form(); ... f.setup(); } }

  50. Example: form

More Related