JLabel
Example of JLabel
:
/* Louis Taber
Pima Community College
3/2/2003
Display a .gif in a JLabel */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class GIF
{
static Container cp; // Content Pane
static ExitButton eb;
public static void main(String[] args)
{
// Get a JFrame
JFrame aWindow = new JFrame( "apache_pb.gif");
// Position and size JFrame
aWindow.setBounds( 50, 100, 400, 100);
// Set Close opereation
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get Content Pane
cp = aWindow.getContentPane();
// Create Image Icon
ImageIcon gif = new ImageIcon("apache_pb.gif");
// Add it to a JLabel
JLabel g1 = new JLabel("Apache", gif, JLabel.CENTER);
// Add text tip -- shows id pointer stays on JLabel
g1.setToolTipText("http://www.apache.org/");
cp.add( g1 );
// Display results
aWindow.setVisible(true);
}
}
JButton
Example of JButton
:
/* Louis Taber
Pima Community College
3/2/2003
Use a JButton */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ExitButton
{
static Container cp; // Content Pane
static ExitButton eb;
public static void main(String[] args)
{
// Get a JFrame
JFrame aWindow = new JFrame( "Exit JButton");
// Position and size JFrame
aWindow.setBounds( 50, 100, 300, 100);
// Set Close opereation
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get Content Pane
cp = aWindow.getContentPane();
// Call ComponentE to add in Exit Button
cp.add( new ComponentE() );
// Set background color to red
cp.setBackground(Color.red);
// Display the JFrame
aWindow.setVisible(true);
}
}
class ComponentE extends JComponent
implements ActionListener
{
JButton jb;
public ComponentE()
{
// get JButton
jb = new JButton("EXIT");
// Set up the colors
jb.setBackground(Color.blue);
jb.setForeground(Color.white);
// Add JButton to Content Pane
add( jb );
jb.addActionListener(this);
setLayout( new FlowLayout() ); // ????
}
public void actionPerformed( ActionEvent e)
{
System.out.println("Exit NOW");
System.exit(0);
}
}
JTextField
Example of JTextField
:
/* Louis Taber
Pima Community College
3/2/2003
Use a JTextField */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JTextFieldExample
{
static Container cp; // Content Pane
static ExitButton eb;
public static void main(String[] args)
{
// Get a JFrame
JFrame aWindow = new JFrame( "Exit JButton");
// Position and size JFrame
aWindow.setBounds( 50, 100, 500, 100);
// Set Close opereation
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get Content Pane
cp = aWindow.getContentPane();
// Call ComponentTF to add a Text Field
cp.add( new ComponentTF() );
// Set background color to green
cp.setBackground(Color.green);
// Display the JFrame
aWindow.setVisible(true);
}
}
class ComponentTF extends JComponent
implements ActionListener,
KeyListener
{
JLabel jl;
JTextField jt;
public ComponentTF()
{
// get JLabel
jl = new JLabel("What do you want? ");
// Get text field - 30 Characters wide
jt = new JTextField(30);
// Add JButton to Content Pane
add( jl );
// Set background color to orange
jt.setBackground(Color.orange);
add( jt );
jt.addActionListener(this);
jt.addKeyListener(this);
setLayout( new FlowLayout() ); // ????
}
public void actionPerformed( ActionEvent e)
{
System.out.println("You typed: " + e.getActionCommand() );
System.exit(0);
}
/* Print character and modifiers. */
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
int modifiers = e.getModifiers();
System.out.println("Character \"" + c + "\" was typed. " +
"Modifiers: " + modifiers );
}
/* Part of KeyListener interface */
public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}
JCheckBox
Example of JCheckBox
/* Louis Taber
Pima Community College
3/2/2003
Use a JButton */
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class CheckBox
{
static Container cp; // Content Pane
public static void main(String[] args)
{
// Get a JFrame
JFrame aWindow = new JFrame( "Weather");
// Position and size JFrame
aWindow.setBounds( 50, 100, 300, 100);
// Set Close opereation
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Get Content Pane
cp = aWindow.getContentPane();
// Call ComponentCheck to add CheckBoxes
cp.add( new CheckBoxes() );
// Set background color to red
cp.setBackground(Color.red);
// Display the JFrame
aWindow.setVisible(true);
}
}
class CheckBoxes extends JComponent
implements ItemListener
{
JCheckBox rain, snow;
public CheckBoxes()
{
String [] weather_types =
{
"Rain", "Snow", "Sleet", "Hail",
"Clear", "Fog", "Ooblick"
};
for( int k=0; k<weather_types.length; k++)
{
// get JCheckBox
JCheckBox wt = new JCheckBox(weather_types[k]);
// add obkect to JComponent
add(wt);
// Tie it into Listener
wt.addItemListener(this);
}
setLayout( new FlowLayout() ); // ????
}
public void itemStateChanged( ItemEvent e)
{
Object obj = e.getItem();
if( obj instanceof JCheckBox)
{
JCheckBox cb = (JCheckBox)obj;
System.out.print( cb.getText() );
System.out.println(" " + ((e.getStateChange() == e.SELECTED)
? "SELECTED" : "DE-SELECTED") );
}
}
}