SlideShare a Scribd company logo
1 of 53
Download to read offline
Programming in Java
                                                              Swing



© 2011 BlueSignet LLC. All rights reserved.
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.
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 = new JFrame();
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.setSize(800, 600);
     f.setLocationRelativeTo(null);
     f.setVisible(true);
   }
 }

© 2011 BlueSignet LLC. All rights reserved.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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(); }
}
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(); }
}
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(); }
}
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(); }
}
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(); }
}
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(); }
}
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(); }
}
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.
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()
   {
     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.
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.
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.
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.
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(); }
}
The End?
                                              Thank You For Watching!



© 2011 BlueSignet LLC. All rights reserved.

More Related Content

What's hot

Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android DevelopmentJussi Pohjolainen
 
Saindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidSaindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidDaniel Baccin
 
Cake Php 1.2 (Ocphp)
Cake Php 1.2 (Ocphp)Cake Php 1.2 (Ocphp)
Cake Php 1.2 (Ocphp)guest193fe1
 
100% Code Coverage - TDD mit Java EE
100% Code Coverage - TDD mit Java EE100% Code Coverage - TDD mit Java EE
100% Code Coverage - TDD mit Java EEStefan Macke
 

What's hot (6)

Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
 
Saindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender androidSaindo da zona de conforto… resolvi aprender android
Saindo da zona de conforto… resolvi aprender android
 
Cake Php 1.2 (Ocphp)
Cake Php 1.2 (Ocphp)Cake Php 1.2 (Ocphp)
Cake Php 1.2 (Ocphp)
 
Spring hibernate jsf_primefaces_intergration
Spring hibernate jsf_primefaces_intergrationSpring hibernate jsf_primefaces_intergration
Spring hibernate jsf_primefaces_intergration
 
SOLID PRINCIPLES
SOLID PRINCIPLESSOLID PRINCIPLES
SOLID PRINCIPLES
 
100% Code Coverage - TDD mit Java EE
100% Code Coverage - TDD mit Java EE100% Code Coverage - TDD mit Java EE
100% Code Coverage - TDD mit Java EE
 

Viewers also liked

3 Msx88 Ac0203
3 Msx88 Ac02033 Msx88 Ac0203
3 Msx88 Ac0203josodo
 
ARQ-1 SimR 09
ARQ-1 SimR 09ARQ-1 SimR 09
ARQ-1 SimR 09josodo
 
Virtual box alumno
Virtual box alumnoVirtual box alumno
Virtual box alumnojosodo
 
Blogs sesion2 florida
Blogs sesion2 floridaBlogs sesion2 florida
Blogs sesion2 floridajosodo
 
3 Msx88 Ac0202
3 Msx88 Ac02023 Msx88 Ac0202
3 Msx88 Ac0202josodo
 
Diseño y gestión de formación on line
Diseño y gestión de formación on lineDiseño y gestión de formación on line
Diseño y gestión de formación on linejosodo
 
Business_model_generation
Business_model_generationBusiness_model_generation
Business_model_generationjosodo
 

Viewers also liked (8)

3 Msx88 Ac0203
3 Msx88 Ac02033 Msx88 Ac0203
3 Msx88 Ac0203
 
ARQ-1 SimR 09
ARQ-1 SimR 09ARQ-1 SimR 09
ARQ-1 SimR 09
 
Virtual box alumno
Virtual box alumnoVirtual box alumno
Virtual box alumno
 
Blogs sesion2 florida
Blogs sesion2 floridaBlogs sesion2 florida
Blogs sesion2 florida
 
3 Msx88 Ac0202
3 Msx88 Ac02023 Msx88 Ac0202
3 Msx88 Ac0202
 
Diseño y gestión de formación on line
Diseño y gestión de formación on lineDiseño y gestión de formación on line
Diseño y gestión de formación on line
 
Edu mica
Edu micaEdu mica
Edu mica
 
Business_model_generation
Business_model_generationBusiness_model_generation
Business_model_generation
 

Similar to Java Swing Programming Guide

Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonEric Wendelin
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonMatthew McCullough
 
import javaxswing import javaawtevent import javai.pdf
import javaxswing import javaawtevent import javai.pdfimport javaxswing import javaawtevent import javai.pdf
import javaxswing import javaawtevent import javai.pdfADITIEYEWEAR
 
import java.awt.event.ActionEvent; import java.awt.event.ActionLis.pdf
import java.awt.event.ActionEvent; import java.awt.event.ActionLis.pdfimport java.awt.event.ActionEvent; import java.awt.event.ActionLis.pdf
import java.awt.event.ActionEvent; import java.awt.event.ActionLis.pdfanupambedcovers
 
Othello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfOthello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfarccreation001
 
correct the error import javaxswingJFrame import javaxs.pdf
correct the error import javaxswingJFrame import javaxs.pdfcorrect the error import javaxswingJFrame import javaxs.pdf
correct the error import javaxswingJFrame import javaxs.pdfdevangmittal4
 
This is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdfThis is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdffashionscollect
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Javasuraj pandey
 
Hi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfHi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfeyeonsecuritysystems
 
What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.Mark Rees
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdffashionfolionr
 
correct the error and add code same in the pic import jav.pdf
correct the error and add code same in the pic   import jav.pdfcorrect the error and add code same in the pic   import jav.pdf
correct the error and add code same in the pic import jav.pdfdevangmittal4
 
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdfimport java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdfgalagirishp
 
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdfbadshetoms
 

Similar to Java Swing Programming Guide (20)

GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
Groovy-er desktop applications with Griffon
Groovy-er desktop applications with GriffonGroovy-er desktop applications with Griffon
Groovy-er desktop applications with Griffon
 
Groovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With GriffonGroovy-er Desktop Applications With Griffon
Groovy-er Desktop Applications With Griffon
 
import javaxswing import javaawtevent import javai.pdf
import javaxswing import javaawtevent import javai.pdfimport javaxswing import javaawtevent import javai.pdf
import javaxswing import javaawtevent import javai.pdf
 
import java.awt.event.ActionEvent; import java.awt.event.ActionLis.pdf
import java.awt.event.ActionEvent; import java.awt.event.ActionLis.pdfimport java.awt.event.ActionEvent; import java.awt.event.ActionLis.pdf
import java.awt.event.ActionEvent; import java.awt.event.ActionLis.pdf
 
Othello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdfOthello.javapackage othello;import core.Game; import userInter.pdf
Othello.javapackage othello;import core.Game; import userInter.pdf
 
correct the error import javaxswingJFrame import javaxs.pdf
correct the error import javaxswingJFrame import javaxs.pdfcorrect the error import javaxswingJFrame import javaxs.pdf
correct the error import javaxswingJFrame import javaxs.pdf
 
This is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdfThis is Java, What I am creating is a multi lab pacman type game.T.pdf
This is Java, What I am creating is a multi lab pacman type game.T.pdf
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Java
 
java2 swing
java2 swingjava2 swing
java2 swing
 
Hi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdfHi my question pretains to Java programing, What I am creating is a .pdf
Hi my question pretains to Java programing, What I am creating is a .pdf
 
What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.What do you mean it needs to be Java based? How jython saved the day.
What do you mean it needs to be Java based? How jython saved the day.
 
I am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdfI am getting a syntax error. I cant seem to find whats causing t.pdf
I am getting a syntax error. I cant seem to find whats causing t.pdf
 
correct the error and add code same in the pic import jav.pdf
correct the error and add code same in the pic   import jav.pdfcorrect the error and add code same in the pic   import jav.pdf
correct the error and add code same in the pic import jav.pdf
 
swings.pptx
swings.pptxswings.pptx
swings.pptx
 
To connect two jframe
To connect two jframeTo connect two jframe
To connect two jframe
 
From Swing to JavaFX
From Swing to JavaFXFrom Swing to JavaFX
From Swing to JavaFX
 
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdfimport java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
import java.awt.FlowLayout;import java.awt.event.KeyEvent;import.pdf
 
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdfPLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
PLEASE HELP ME !!IT IS Due Tonight ;(!How can I make the add but.pdf
 
TY.BSc.IT Java QB U2
TY.BSc.IT Java QB U2TY.BSc.IT Java QB U2
TY.BSc.IT Java QB U2
 

More from josodo

Sustainable presentation
Sustainable presentationSustainable presentation
Sustainable presentationjosodo
 
The netherlands presentation
The netherlands presentationThe netherlands presentation
The netherlands presentationjosodo
 
Crec aprenentatge col.laboratiu
Crec aprenentatge col.laboratiuCrec aprenentatge col.laboratiu
Crec aprenentatge col.laboratiujosodo
 
Crec aprenentatge significatiu
Crec aprenentatge significatiuCrec aprenentatge significatiu
Crec aprenentatge significatiujosodo
 
Como no usar_ppt_(don_mcmillan)v2
Como no usar_ppt_(don_mcmillan)v2Como no usar_ppt_(don_mcmillan)v2
Como no usar_ppt_(don_mcmillan)v2josodo
 
Edublogs
EdublogsEdublogs
Edublogsjosodo
 
Software testing 2
Software testing 2Software testing 2
Software testing 2josodo
 
Http10
Http10Http10
Http10josodo
 
Software testing 1
Software testing 1Software testing 1
Software testing 1josodo
 
Unidad 4 ftp (a)
Unidad 4 ftp (a)Unidad 4 ftp (a)
Unidad 4 ftp (a)josodo
 
Comercializacion on line-v2
Comercializacion on line-v2Comercializacion on line-v2
Comercializacion on line-v2josodo
 
Final report 2010 30 of april
Final report 2010 30 of aprilFinal report 2010 30 of april
Final report 2010 30 of apriljosodo
 
Final report 2010 30 of april
Final report 2010 30 of aprilFinal report 2010 30 of april
Final report 2010 30 of apriljosodo
 
Forte ii project report
Forte ii project reportForte ii project report
Forte ii project reportjosodo
 
Pohl soler pac2_md_ms_fitxes_casos_v4_js
Pohl soler pac2_md_ms_fitxes_casos_v4_jsPohl soler pac2_md_ms_fitxes_casos_v4_js
Pohl soler pac2_md_ms_fitxes_casos_v4_jsjosodo
 
Pohl soler pac2_md_ms_fitxes_projectes_v5_js
Pohl soler pac2_md_ms_fitxes_projectes_v5_jsPohl soler pac2_md_ms_fitxes_projectes_v5_js
Pohl soler pac2_md_ms_fitxes_projectes_v5_jsjosodo
 
Sesion inicial
Sesion inicialSesion inicial
Sesion inicialjosodo
 
Propuesta formativa asesores docentes conselleria avanzado-florida abril 2010
Propuesta formativa asesores docentes conselleria avanzado-florida abril 2010Propuesta formativa asesores docentes conselleria avanzado-florida abril 2010
Propuesta formativa asesores docentes conselleria avanzado-florida abril 2010josodo
 
Pohl soler pac2_acords_grup_v2_dp
Pohl soler pac2_acords_grup_v2_dpPohl soler pac2_acords_grup_v2_dp
Pohl soler pac2_acords_grup_v2_dpjosodo
 
Sustainable vacation presentation
Sustainable vacation presentationSustainable vacation presentation
Sustainable vacation presentationjosodo
 

More from josodo (20)

Sustainable presentation
Sustainable presentationSustainable presentation
Sustainable presentation
 
The netherlands presentation
The netherlands presentationThe netherlands presentation
The netherlands presentation
 
Crec aprenentatge col.laboratiu
Crec aprenentatge col.laboratiuCrec aprenentatge col.laboratiu
Crec aprenentatge col.laboratiu
 
Crec aprenentatge significatiu
Crec aprenentatge significatiuCrec aprenentatge significatiu
Crec aprenentatge significatiu
 
Como no usar_ppt_(don_mcmillan)v2
Como no usar_ppt_(don_mcmillan)v2Como no usar_ppt_(don_mcmillan)v2
Como no usar_ppt_(don_mcmillan)v2
 
Edublogs
EdublogsEdublogs
Edublogs
 
Software testing 2
Software testing 2Software testing 2
Software testing 2
 
Http10
Http10Http10
Http10
 
Software testing 1
Software testing 1Software testing 1
Software testing 1
 
Unidad 4 ftp (a)
Unidad 4 ftp (a)Unidad 4 ftp (a)
Unidad 4 ftp (a)
 
Comercializacion on line-v2
Comercializacion on line-v2Comercializacion on line-v2
Comercializacion on line-v2
 
Final report 2010 30 of april
Final report 2010 30 of aprilFinal report 2010 30 of april
Final report 2010 30 of april
 
Final report 2010 30 of april
Final report 2010 30 of aprilFinal report 2010 30 of april
Final report 2010 30 of april
 
Forte ii project report
Forte ii project reportForte ii project report
Forte ii project report
 
Pohl soler pac2_md_ms_fitxes_casos_v4_js
Pohl soler pac2_md_ms_fitxes_casos_v4_jsPohl soler pac2_md_ms_fitxes_casos_v4_js
Pohl soler pac2_md_ms_fitxes_casos_v4_js
 
Pohl soler pac2_md_ms_fitxes_projectes_v5_js
Pohl soler pac2_md_ms_fitxes_projectes_v5_jsPohl soler pac2_md_ms_fitxes_projectes_v5_js
Pohl soler pac2_md_ms_fitxes_projectes_v5_js
 
Sesion inicial
Sesion inicialSesion inicial
Sesion inicial
 
Propuesta formativa asesores docentes conselleria avanzado-florida abril 2010
Propuesta formativa asesores docentes conselleria avanzado-florida abril 2010Propuesta formativa asesores docentes conselleria avanzado-florida abril 2010
Propuesta formativa asesores docentes conselleria avanzado-florida abril 2010
 
Pohl soler pac2_acords_grup_v2_dp
Pohl soler pac2_acords_grup_v2_dpPohl soler pac2_acords_grup_v2_dp
Pohl soler pac2_acords_grup_v2_dp
 
Sustainable vacation presentation
Sustainable vacation presentationSustainable vacation presentation
Sustainable vacation presentation
 

Recently uploaded

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

Java Swing Programming Guide

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