Programming in Java                                                              Swing© 2011 BlueSignet LLC. All rights re...
What is Swing?                  Platform independent GUI API for Java                  Relatively simple to implement     ...
JFrame Class© 2011 BlueSignet LLC. All rights reserved.
JFrame Class© 2011 BlueSignet LLC. All rights reserved.
JFrame Class© 2011 BlueSignet LLC. All rights reserved.
JFrame Class© 2011 BlueSignet LLC. All rights reserved.
JFrame Class© 2011 BlueSignet LLC. All rights reserved.
JFrame Class© 2011 BlueSignet LLC. All rights reserved.
JFrame Class import javax.swing.JFrame; public class SwingTest {   public static void main(String[] args)   {     JFrame f...
JFrame Class import javax.swing.JFrame; public class SwingTest {   public static void main(String[] args)   {     JFrame f...
JFrame Class import java.awt.event.*; import javax.swing.*; public class SwingExample extends JFrame {   public SwingExamp...
JFrame Class import java.awt.event.*; import javax.swing.*; public class SwingExample extends JFrame {   public SwingExamp...
JFrame Class import java.awt.event.*; import javax.swing.*; public class SwingExample extends JFrame {   public SwingExamp...
JFrame Class import java.awt.event.*; import javax.swing.*; public class SwingExample extends JFrame {   public SwingExamp...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
JFrame Class import java.awt.event.*; import javax.swing.*;                                                          Swing...
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{  JPanel mainPanel...
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{  JPanel mainPanel...
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{  JPanel mainPanel...
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{  JPanel mainPanel...
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{  JPanel mainPanel...
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{  JPanel mainPanel...
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class SwingExample extends JFrame{  JPanel mainPanel...
How do you place controls on a frame?                  Java expects UI controls to be encased inside                  cont...
Layering                                              JFrame   JPanel   UI Item© 2011 BlueSignet LLC. All rights reserved.
Layering                                              JFrame   JPanel   UI Item© 2011 BlueSignet LLC. All rights reserved.
Layering                                              JPanel   UI Item© 2011 BlueSignet LLC. All rights reserved.
Layering                                              JPanel© 2011 BlueSignet LLC. All rights reserved.
Layering                                              JFrame   JPanel© 2011 BlueSignet LLC. All rights reserved.
Layering                                              JFrame© 2011 BlueSignet LLC. All rights reserved.
Layering© 2011 BlueSignet LLC. All rights reserved.
Control Placement Example import javax.swing.*; public class SwingExample extends JFrame {   public SwingExample()   {    ...
Control Placement Example import javax.swing.*; public class SwingExample extends JFrame {   public SwingExample()   {    ...
Control Placement Example import javax.swing.*; public class SwingExample extends JFrame {   public SwingExample()   {    ...
Control Placement Example import javax.swing.*; public class SwingExample extends JFrame {   public SwingExample()   {    ...
import java.awt.*; import java.awt.event.*; import javax.swing.*;public class SwingExample extends JFrame{  JPanel fNPanel...
The End?                                              Thank You For Watching!© 2011 BlueSignet LLC. All rights reserved.
Upcoming SlideShare
Loading in …5
×

Programming in java_-_17_-_swing

1,515 views

Published on

Published in: Technology, Education
  • Be the first to comment

Programming in java_-_17_-_swing

  1. 1. Programming in Java Swing© 2011 BlueSignet LLC. All rights reserved.
  2. 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. 3. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  4. 4. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  5. 5. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  6. 6. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  7. 7. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  8. 8. JFrame Class© 2011 BlueSignet LLC. All rights reserved.
  9. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 41. Layering JFrame JPanel UI Item© 2011 BlueSignet LLC. All rights reserved.
  42. 42. Layering JFrame JPanel UI Item© 2011 BlueSignet LLC. All rights reserved.
  43. 43. Layering JPanel UI Item© 2011 BlueSignet LLC. All rights reserved.
  44. 44. Layering JPanel© 2011 BlueSignet LLC. All rights reserved.
  45. 45. Layering JFrame JPanel© 2011 BlueSignet LLC. All rights reserved.
  46. 46. Layering JFrame© 2011 BlueSignet LLC. All rights reserved.
  47. 47. Layering© 2011 BlueSignet LLC. All rights reserved.
  48. 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. 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. 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. 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. 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. 53. The End? Thank You For Watching!© 2011 BlueSignet LLC. All rights reserved.

×