programming in java_-_17_-_swing

53
© 2011 BlueSignet LLC. All rights reserved. Programming in Java Swing

Upload: josodo

Post on 15-Jan-2015

1.189 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

Programming in JavaSwing

Page 2: Programming in java_-_17_-_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)

Page 3: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

JFrame Class

Page 4: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

JFrame Class

Page 5: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

JFrame Class

Page 6: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

JFrame Class

Page 7: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

JFrame Class

Page 8: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

JFrame Class

Page 9: Programming in java_-_17_-_swing

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

}}

Page 10: Programming in java_-_17_-_swing

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

}}

Page 11: Programming in java_-_17_-_swing

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

}

}

Page 12: Programming in java_-_17_-_swing

© 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

Page 13: Programming in java_-_17_-_swing

© 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

Page 14: Programming in java_-_17_-_swing

© 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

Page 15: Programming in java_-_17_-_swing

© 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

Page 16: Programming in java_-_17_-_swing

© 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

Page 17: Programming in java_-_17_-_swing

© 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

Page 18: Programming in java_-_17_-_swing

© 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

Page 19: Programming in java_-_17_-_swing

© 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

Page 20: Programming in java_-_17_-_swing

© 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

Page 21: Programming in java_-_17_-_swing

© 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

Page 22: Programming in java_-_17_-_swing

© 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

Page 23: Programming in java_-_17_-_swing

© 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

Page 24: Programming in java_-_17_-_swing

© 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

Page 25: Programming in java_-_17_-_swing

© 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

Page 26: Programming in java_-_17_-_swing

© 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

Page 27: Programming in java_-_17_-_swing

© 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

Page 28: Programming in java_-_17_-_swing

© 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

Page 29: Programming in java_-_17_-_swing

© 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

Page 30: Programming in java_-_17_-_swing

© 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

Page 31: Programming in java_-_17_-_swing

© 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

Page 32: Programming in java_-_17_-_swing

© 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

Page 33: Programming in java_-_17_-_swing

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

}

Page 34: Programming in java_-_17_-_swing

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

}

Page 35: Programming in java_-_17_-_swing

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

}

Page 36: Programming in java_-_17_-_swing

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

}

Page 37: Programming in java_-_17_-_swing

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

}

Page 38: Programming in java_-_17_-_swing

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

}

Page 39: Programming in java_-_17_-_swing

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

}

Page 40: Programming in java_-_17_-_swing

© 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

Page 41: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

Layering

JFrame JPanel UI Item

Page 42: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

Layering

JPanel UI ItemJFrame

Page 43: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

Layering

JPanel UI Item

Page 44: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

Layering

JPanel

Page 45: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

JPanel

Layering

JFrame

Page 46: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

Layering

JFrame

Page 47: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

Layering

Page 48: Programming in java_-_17_-_swing

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

}

Page 49: Programming in java_-_17_-_swing

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

}

Page 50: Programming in java_-_17_-_swing

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

}

Page 51: Programming in java_-_17_-_swing

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

}

Page 52: Programming in java_-_17_-_swing

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

}

Page 53: Programming in java_-_17_-_swing

© 2011 BlueSignet LLC. All rights reserved.

The End?Thank You For Watching!