JPanels

This applet (code sample 1 at the bottom) has two panels.

One panel is in purple and the other is in yellow.

Panels never need to be declared globally. They are only used for formatting in init.

Declare and Construct the Panel Panel p = new Panel ();
Make a new colour and set the background colour p.setBackground (new Color (193, 150, 255));
Set the background colour p.setBackground (Color.yellow);
Add the widgets that need to be in it to the panel p.add (firstop);
p.add (first);
p.add (secondop);
p.add (second);
Add the panel to the applet. NOTE: you do not need to add the widgets in the panel to the applet. In fact, if you do add them again, the applet will not run correctly. add(p);

You can also make panels that lay things out in a grid (code sample 2)

There are two panels in this example. One holds the toppings and the other holds the pizza size.

Make a GridLayout in dimensions 2,2. Note the order that things are added and how they appeared above.

Panel p = new Panel (new GridLayout (2, 2));
p.add (mushrooms);
p.add (pepperoni);
p.add (pineapple);
p.add (hotpeppers);
add(p);

Make a GridLayout in dimensions 2,1. Panel p2 = new Panel (new GridLayout (2, 1));
p2.add (regular);
p2.add (large);
add(p2);

Code Sample 1

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class calculator extends Applet implements ActionListener
{
JTextField first, second;
JLabel result;

public void init ()
{
JLabel title = new JLabel ("Calculator");
title.setFont (new Font ("Jokerman", Font.BOLD, 40));
title.setForeground (Color.orange);
JLabel firstop = new JLabel ("First Operand:");
first = new JTextField (2);
JLabel secondop = new JLabel ("Second Operand:");
second = new JTextField (2);
Panel p = new Panel ();
p.setBackground (new Color (193, 150, 255));
p.add (firstop);
p.add (first);
p.add (secondop);
p.add (second);

JButton add = new JButton ("+");
add.setActionCommand ("add");
add.addActionListener (this);
JButton sub = new JButton ("-");
sub.setActionCommand ("sub");
sub.addActionListener (this);
JButton mult = new JButton ("*");
mult.setActionCommand ("mult");
mult.addActionListener (this);
JButton div = new JButton ("/");
div.setActionCommand ("div");
div.addActionListener (this);
JButton mod = new JButton ("%");
mod.setActionCommand ("mod");
mod.addActionListener (this);
Panel p2 = new Panel ();
p2.setBackground (Color.yellow);
p2.add (add);
p2.add (sub);
p2.add (mult);
p2.add (div);
p2.add (mod);

result = new JLabel ("Result: .");
add (title);
add (p);
add (p2);
add (result);

}


//actionPerformed is where we handle the user's clicking on the screen
public void actionPerformed (ActionEvent e)
{
try
{
int f = Integer.parseInt (first.getText ());
int s = Integer.parseInt (second.getText ());
int r = f + s;
if (e.getActionCommand ().equals ("sub"))
r = f - s;
else if (e.getActionCommand ().equals ("mult"))
r = f * s;
else if (e.getActionCommand ().equals ("div"))
r = f / s;
else if (e.getActionCommand ().equals ("mod"))
r = f % s;
result.setText ("Result: " + r);
}
catch (java.lang.NumberFormatException m)
{
result.setText ("both operands need numbers");
}
catch (java.lang.ArithmeticException h)
{
result.setText ("can't divide by zero");
}
}
}

Code Sample 2

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class pizzaPicker extends Applet implements ActionListener
{
JCheckBox mushrooms, pepperoni, pineapple, hotpeppers;
JRadioButton regular, large;
JLabel result;
int total = 0;
public void init ()
{
resize (250, 200);
JLabel title = new JLabel ("Perfect Pizza Picker");
title.setFont (new Font ("Verdana", Font.PLAIN, 20));
title.setForeground (Color.magenta);

JLabel toppings = new JLabel ("Select Toppings:");
mushrooms = new JCheckBox ("Mushrooms");
mushrooms.setActionCommand ("mushrooms");
mushrooms.addActionListener (this);
pepperoni = new JCheckBox ("Pepperoni");
pepperoni.setActionCommand ("pepperoni");
pepperoni.addActionListener (this);
pineapple = new JCheckBox ("Pineapple");
pineapple.setActionCommand ("pineapple");
pineapple.addActionListener (this);
hotpeppers = new JCheckBox ("Hot Peppers");
hotpeppers.setActionCommand ("hotpeppers");
hotpeppers.addActionListener (this);

JLabel size = new JLabel ("Select pizza size:");
regular = new JRadioButton ("Regular");
regular.addActionListener (this);
regular.setActionCommand ("regular");
large = new JRadioButton ("Large");
large.addActionListener (this);
large.setActionCommand ("large");

JButton done = new JButton ("Next Customer - Reset");
done.setActionCommand ("done");
done.addActionListener (this);

result = new JLabel ("Total: $0.00 ");
result.setForeground (Color.magenta);

Panel p = new Panel (new GridLayout (2, 2));
p.add (mushrooms);
p.add (pepperoni);
p.add (pineapple);
p.add (hotpeppers);

ButtonGroup group = new ButtonGroup ();
group.add (regular);
group.add (large);

Panel p2 = new Panel (new GridLayout (2, 1));
p2.add (regular);
p2.add (large);

add (title);
add (toppings);
add (p);
add (size);
add (p2);
add (result);
add (done);

}


public void actionPerformed (ActionEvent e)
{

}
}