Programming in java_-_17_-_swing

  • 1,169 views
Uploaded on

 

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,169
On Slideshare
0
From Embeds
0
Number of Embeds
3

Actions

Shares
Downloads
97
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Programming in Java Swing© 2011 BlueSignet LLC. All rights reserved.
  • 2. What is Swing? Platform independent GUI API for Java Relatively simple to implement Derives some of its functionality from Abstract Window Toolkit (AWT)© 2011 BlueSignet LLC. All rights reserved.
  • 3. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  • 4. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  • 5. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  • 6. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  • 7. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  • 8. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  • 9. JFrame Class import javax.swing.JFrame; public class SwingTest { public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(800, 600); f.setLocationRelativeTo(null); f.setVisible(true); } }© 2011 BlueSignet LLC. All rights reserved.
  • 10. JFrame Class import javax.swing.JFrame; public class SwingTest { public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(800, 600); f.setLocationRelativeTo(null); f.setVisible(true); } }© 2011 BlueSignet LLC. All rights reserved.
  • 11. JFrame Class import java.awt.event.*; import javax.swing.*; public class SwingExample extends JFrame { public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 12. JFrame Class import java.awt.event.*; import javax.swing.*; public class SwingExample extends JFrame { public SwingExample() { Parent Class JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 13. JFrame Class import java.awt.event.*; import javax.swing.*; public class SwingExample extends JFrame { public SwingExample() { Parent Class JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { Sub Class SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 14. JFrame Class import java.awt.event.*; import javax.swing.*; public class SwingExample extends JFrame { public SwingExample() { Parent Class JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); Parent Class Constructor p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { Sub Class SwingExample myProgram = new SwingExample(); } } Sub Class Constructor© 2011 BlueSignet LLC. All rights reserved.
  • 15. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 16. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 17. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 18. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 19. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 20. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 21. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 22. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 23. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 24. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 25. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 26. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 27. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 28. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 29. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 30. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 31. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 32. JFrame Class import java.awt.event.*; import javax.swing.*; SwingExample public class SwingExample extends JFrame { main public SwingExample() { JPanel p = new JPanel(); JButton b = new JButton("Click Me!"); b.addActionListener(new ButtonAction(this)); p.add(b); add(p); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { JOptionPane.showMessageDialog(_sourceFrame, "Hey, Buddy!"); } } public static void main(String[] args) { ButtonAction SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 33. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{ JPanel mainPanel; JButton buttonOK; JTextField textFieldFirstName, textFieldLastName; Dimension textFieldSize = new Dimension(200, 25); public SwingExample() { mainPanel = new JPanel(); textFieldFirstName = new JTextField(); textFieldLastName = new JTextField(); buttonOK = new JButton("OK"); buttonOK.addActionListener(new ButtonAction(this)); textFieldFirstName.setPreferredSize(textFieldSize); textFieldLastName.setPreferredSize(textFieldSize); mainPanel.add(new JLabel("First Name:")); mainPanel.add(textFieldFirstName); mainPanel.add(new JLabel("Last Name:")); mainPanel.add(textFieldLastName); mainPanel.add(buttonOK); add(mainPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setTitle("What is your name?"); textFieldFirstName.requestFocusInWindow(); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { String firstName = textFieldFirstName.getText(); String lastName = textFieldLastName.getText(); if(firstName.equals("") || lastName.equals("")) { JOptionPane.showMessageDialog(_sourceFrame, "Please enter your name."); return; } JOptionPane.showMessageDialog(_sourceFrame, "Hello, " + firstName + " " + lastName); textFieldFirstName.setText(""); textFieldLastName.setText(""); textFieldFirstName.requestFocusInWindow(); } } public static BlueSignet LLC. All rights reserved. © 2011 void main(String[] args) { SwingExample myProgram = new SwingExample(); }}
  • 34. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{ JPanel mainPanel; JButton buttonOK; JTextField textFieldFirstName, textFieldLastName; Dimension textFieldSize = new Dimension(200, 25); public SwingExample() { mainPanel = new JPanel(); textFieldFirstName = new JTextField(); textFieldLastName = new JTextField(); buttonOK = new JButton("OK"); buttonOK.addActionListener(new ButtonAction(this)); textFieldFirstName.setPreferredSize(textFieldSize); textFieldLastName.setPreferredSize(textFieldSize); mainPanel.add(new JLabel("First Name:")); mainPanel.add(textFieldFirstName); mainPanel.add(new JLabel("Last Name:")); mainPanel.add(textFieldLastName); mainPanel.add(buttonOK); add(mainPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setTitle("What is your name?"); textFieldFirstName.requestFocusInWindow(); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { String firstName = textFieldFirstName.getText(); String lastName = textFieldLastName.getText(); if(firstName.equals("") || lastName.equals("")) { JOptionPane.showMessageDialog(_sourceFrame, "Please enter your name."); return; } JOptionPane.showMessageDialog(_sourceFrame, "Hello, " + firstName + " " + lastName); textFieldFirstName.setText(""); textFieldLastName.setText(""); textFieldFirstName.requestFocusInWindow(); } } public static BlueSignet LLC. All rights reserved. © 2011 void main(String[] args) { SwingExample myProgram = new SwingExample(); }}
  • 35. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{ JPanel mainPanel; JButton buttonOK; JTextField textFieldFirstName, textFieldLastName; Dimension textFieldSize = new Dimension(200, 25); public SwingExample() { mainPanel = new JPanel(); textFieldFirstName = new JTextField(); textFieldLastName = new JTextField(); buttonOK = new JButton("OK"); buttonOK.addActionListener(new ButtonAction(this)); textFieldFirstName.setPreferredSize(textFieldSize); textFieldLastName.setPreferredSize(textFieldSize); mainPanel.add(new JLabel("First Name:")); mainPanel.add(textFieldFirstName); mainPanel.add(new JLabel("Last Name:")); mainPanel.add(textFieldLastName); mainPanel.add(buttonOK); add(mainPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setTitle("What is your name?"); textFieldFirstName.requestFocusInWindow(); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { String firstName = textFieldFirstName.getText(); String lastName = textFieldLastName.getText(); if(firstName.equals("") || lastName.equals("")) { JOptionPane.showMessageDialog(_sourceFrame, "Please enter your name."); return; } JOptionPane.showMessageDialog(_sourceFrame, "Hello, " + firstName + " " + lastName); textFieldFirstName.setText(""); textFieldLastName.setText(""); textFieldFirstName.requestFocusInWindow(); } } public static BlueSignet LLC. All rights reserved. © 2011 void main(String[] args) { SwingExample myProgram = new SwingExample(); }}
  • 36. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{ JPanel mainPanel; JButton buttonOK; JTextField textFieldFirstName, textFieldLastName; Dimension textFieldSize = new Dimension(200, 25); public SwingExample() { mainPanel = new JPanel(); textFieldFirstName = new JTextField(); textFieldLastName = new JTextField(); buttonOK = new JButton("OK"); buttonOK.addActionListener(new ButtonAction(this)); textFieldFirstName.setPreferredSize(textFieldSize); textFieldLastName.setPreferredSize(textFieldSize); mainPanel.add(new JLabel("First Name:")); mainPanel.add(textFieldFirstName); mainPanel.add(new JLabel("Last Name:")); mainPanel.add(textFieldLastName); mainPanel.add(buttonOK); add(mainPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setTitle("What is your name?"); textFieldFirstName.requestFocusInWindow(); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { String firstName = textFieldFirstName.getText(); String lastName = textFieldLastName.getText(); if(firstName.equals("") || lastName.equals("")) { JOptionPane.showMessageDialog(_sourceFrame, "Please enter your name."); return; } JOptionPane.showMessageDialog(_sourceFrame, "Hello, " + firstName + " " + lastName); textFieldFirstName.setText(""); textFieldLastName.setText(""); textFieldFirstName.requestFocusInWindow(); } } public static BlueSignet LLC. All rights reserved. © 2011 void main(String[] args) { SwingExample myProgram = new SwingExample(); }}
  • 37. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{ JPanel mainPanel; JButton buttonOK; JTextField textFieldFirstName, textFieldLastName; Dimension textFieldSize = new Dimension(200, 25); public SwingExample() { mainPanel = new JPanel(); textFieldFirstName = new JTextField(); textFieldLastName = new JTextField(); buttonOK = new JButton("OK"); buttonOK.addActionListener(new ButtonAction(this)); textFieldFirstName.setPreferredSize(textFieldSize); textFieldLastName.setPreferredSize(textFieldSize); mainPanel.add(new JLabel("First Name:")); mainPanel.add(textFieldFirstName); mainPanel.add(new JLabel("Last Name:")); mainPanel.add(textFieldLastName); mainPanel.add(buttonOK); add(mainPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setTitle("What is your name?"); textFieldFirstName.requestFocusInWindow(); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { String firstName = textFieldFirstName.getText(); String lastName = textFieldLastName.getText(); if(firstName.equals("") || lastName.equals("")) { JOptionPane.showMessageDialog(_sourceFrame, "Please enter your name."); return; } JOptionPane.showMessageDialog(_sourceFrame, "Hello, " + firstName + " " + lastName); textFieldFirstName.setText(""); textFieldLastName.setText(""); textFieldFirstName.requestFocusInWindow(); } } public static BlueSignet LLC. All rights reserved. © 2011 void main(String[] args) { SwingExample myProgram = new SwingExample(); }}
  • 38. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{ JPanel mainPanel; JButton buttonOK; JTextField textFieldFirstName, textFieldLastName; Dimension textFieldSize = new Dimension(200, 25); public SwingExample() { mainPanel = new JPanel(); textFieldFirstName = new JTextField(); textFieldLastName = new JTextField(); buttonOK = new JButton("OK"); buttonOK.addActionListener(new ButtonAction(this)); textFieldFirstName.setPreferredSize(textFieldSize); textFieldLastName.setPreferredSize(textFieldSize); mainPanel.add(new JLabel("First Name:")); mainPanel.add(textFieldFirstName); mainPanel.add(new JLabel("Last Name:")); mainPanel.add(textFieldLastName); mainPanel.add(buttonOK); add(mainPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setTitle("What is your name?"); textFieldFirstName.requestFocusInWindow(); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { String firstName = textFieldFirstName.getText(); String lastName = textFieldLastName.getText(); if(firstName.equals("") || lastName.equals("")) { JOptionPane.showMessageDialog(_sourceFrame, "Please enter your name."); return; } JOptionPane.showMessageDialog(_sourceFrame, "Hello, " + firstName + " " + lastName); textFieldFirstName.setText(""); textFieldLastName.setText(""); textFieldFirstName.requestFocusInWindow(); } } public static BlueSignet LLC. All rights reserved. © 2011 void main(String[] args) { SwingExample myProgram = new SwingExample(); }}
  • 39. import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{ JPanel mainPanel; JButton buttonOK; JTextField textFieldFirstName, textFieldLastName; Dimension textFieldSize = new Dimension(200, 25); public SwingExample() { mainPanel = new JPanel(); textFieldFirstName = new JTextField(); textFieldLastName = new JTextField(); buttonOK = new JButton("OK"); buttonOK.addActionListener(new ButtonAction(this)); textFieldFirstName.setPreferredSize(textFieldSize); textFieldLastName.setPreferredSize(textFieldSize); mainPanel.add(new JLabel("First Name:")); mainPanel.add(textFieldFirstName); mainPanel.add(new JLabel("Last Name:")); mainPanel.add(textFieldLastName); mainPanel.add(buttonOK); add(mainPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setTitle("What is your name?"); textFieldFirstName.requestFocusInWindow(); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { String firstName = textFieldFirstName.getText(); String lastName = textFieldLastName.getText(); if(firstName.equals("") || lastName.equals("")) { JOptionPane.showMessageDialog(_sourceFrame, "Please enter your name."); return; } JOptionPane.showMessageDialog(_sourceFrame, "Hello, " + firstName + " " + lastName); textFieldFirstName.setText(""); textFieldLastName.setText(""); textFieldFirstName.requestFocusInWindow(); } } public static BlueSignet LLC. All rights reserved. © 2011 void main(String[] args) { SwingExample myProgram = new SwingExample(); }}
  • 40. How do you place controls on a frame? Java expects UI controls to be encased inside containers There are multiple ways to achieve this One way that we find simple to understand is to use JPanels and absolute positioning© 2011 BlueSignet LLC. All rights reserved.
  • 41. Layering JFrame JPanel UI Item© 2011 BlueSignet LLC. All rights reserved.
  • 42. Layering JFrame JPanel UI Item© 2011 BlueSignet LLC. All rights reserved.
  • 43. Layering JPanel UI Item© 2011 BlueSignet LLC. All rights reserved.
  • 44. Layering JPanel© 2011 BlueSignet LLC. All rights reserved.
  • 45. Layering JFrame JPanel© 2011 BlueSignet LLC. All rights reserved.
  • 46. Layering JFrame© 2011 BlueSignet LLC. All rights reserved.
  • 47. Layering© 2011 BlueSignet LLC. All rights reserved.
  • 48. Control Placement Example import javax.swing.*; public class SwingExample extends JFrame { public SwingExample() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setTitle("Hey Buddy!"); JPanel labelPanel = new JPanel(); JLabel heyBuddyLabel = new JLabel("Hey Buddy!"); labelPanel.add(heyBuddyLabel); add(labelPanel); setVisible(true); } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 49. Control Placement Example import javax.swing.*; public class SwingExample extends JFrame { public SwingExample() { setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setTitle("Hey Buddy!"); JPanel labelPanel = new JPanel(); JLabel heyBuddyLabel = new JLabel("Hey Buddy!"); labelPanel.add(heyBuddyLabel); add(labelPanel); setVisible(true); } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 50. Control Placement Example import javax.swing.*; public class SwingExample extends JFrame { public SwingExample() { setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setTitle("Hey Buddy!"); JPanel labelPanel = new JPanel(); JLabel heyBuddyLabel = new JLabel("Hey Buddy!"); labelPanel.add(heyBuddyLabel); labelPanel.setBounds(0, 0, 70, 20); add(labelPanel); setVisible(true); } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 51. Control Placement Example import javax.swing.*; public class SwingExample extends JFrame { public SwingExample() { setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640, 480); setLocationRelativeTo(null); setTitle("Hey Buddy!"); JPanel labelPanel = new JPanel(); JLabel heyBuddyLabel = new JLabel("Hey Buddy!"); labelPanel.add(heyBuddyLabel); labelPanel.setBounds(120, 55, 70, 20); add(labelPanel); setVisible(true); } public static void main(String[] args) { SwingExample myProgram = new SwingExample(); } }© 2011 BlueSignet LLC. All rights reserved.
  • 52. import java.awt.*; import java.awt.event.*; import javax.swing.*;public class SwingExample extends JFrame{ JPanel fNPanel = new JPanel(); JPanel lNPanel = new JPanel(); JPanel buttonsPanel = new JPanel(); JButton buttonOK = new JButton("OK"); JButton buttonCancel = new JButton("Cancel"); JTextField textFieldFirstName = new JTextField(), textFieldLastName = new JTextField(); Dimension textFieldSize = new Dimension(200, 25); public SwingExample() { setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300, 135); setLocationRelativeTo(null); setTitle("What is your name?"); buttonOK.addActionListener(new ButtonAction(this)); buttonCancel.addActionListener(new ButtonAction(this)); textFieldFirstName.setPreferredSize(textFieldSize); textFieldLastName.setPreferredSize(textFieldSize); fNPanel.setBounds(0, 0, 280, 30); fNPanel.add(new JLabel("First Name:")); fNPanel.add(textFieldFirstName); lNPanel.setBounds(0, 30, 280, 30); lNPanel.add(new JLabel("Last Name:")); lNPanel.add(textFieldLastName); buttonsPanel.setBounds(140, 60, 135, 35); buttonsPanel.add(buttonOK); buttonsPanel.add(buttonCancel); add(fNPanel); add(lNPanel); add(buttonsPanel); textFieldFirstName.requestFocusInWindow(); setVisible(true); } public class ButtonAction implements ActionListener { private JFrame _sourceFrame = null; public ButtonAction(JFrame sourceFrame) { _sourceFrame = sourceFrame; } public void actionPerformed(ActionEvent a) { if(a.getActionCommand() == "Cancel") System.exit(0); String firstName = textFieldFirstName.getText(); String lastName = textFieldLastName.getText(); if(firstName.equals("") || lastName.equals("")) { JOptionPane.showMessageDialog(_sourceFrame, "Please enter your name."); return; } JOptionPane.showMessageDialog(_sourceFrame, "Hello, " + firstName + " " + lastName); textFieldFirstName.setText(""); textFieldLastName.setText(""); textFieldFirstName.requestFocusInWindow(); } } public © 2011 BlueSignet LLC. All rights args) static void main(String[] reserved. { SwingExample myProgram = new SwingExample(); }}
  • 53. The End? Thank You For Watching!© 2011 BlueSignet LLC. All rights reserved.