programming in java_-_17_-_swing

Post on 15-Jan-2015

1.187 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

© 2011 BlueSignet LLC. All rights reserved.

Programming in JavaSwing

© 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()

{

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

}

}

Parent Class

© 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();

}

}

Parent Class

Sub Class

© 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();

}

}

Parent Class

Sub Class

Parent Class Constructor

Sub Class Constructor

© 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();

}

}

SwingExample

main

© 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();

}

}

SwingExample

main

© 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();

}

}

SwingExample

main

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 2011 BlueSignet LLC. All rights reserved.

SwingExample

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

}

}

main

ButtonAction

© 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 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 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 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 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 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 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 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 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 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 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 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 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 void main(String[] args){ SwingExample myProgram = new SwingExample(); }

}

© 2011 BlueSignet LLC. All rights reserved.

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

JPanel UI ItemJFrame

© 2011 BlueSignet LLC. All rights reserved.

Layering

JPanel UI Item

© 2011 BlueSignet LLC. All rights reserved.

Layering

JPanel

© 2011 BlueSignet LLC. All rights reserved.

JPanel

Layering

JFrame

© 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 static void main(String[] args){ SwingExample myProgram = new SwingExample(); }

}

© 2011 BlueSignet LLC. All rights reserved.

The End?Thank You For Watching!

top related