Please send questions to
st10@humboldt.edu .
/**
* ManyButtons2
*
* this Java applet, a simple example of an array of
* objects that added a more interesting layout manager
* for the buttons, is an old CIS 235 applet example that
* was redone in larger fonts for display purposes.
*
* RECOMMENDED SIZE: 400 x 650
*
* modified by: Sharon M. Tuttle
* last modified: 2-12-01
**/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ManyButtons2 extends Applet implements ActionListener
{
Label instruction;
Panel line1Panel, line2Panel, below1Panel;
Button[] buttonSet;
TextField numButtonsField;
boolean numButtonsChosen = false;
Label dummy; // placeholder until buttons added
// added to make applet more displayable
Font biggerFont;
public void init()
{
biggerFont = new Font("Serif", Font.PLAIN, 30);
setLayout(new BorderLayout());
setBackground(Color.lightGray);
System.out.println("ManyButtons2, Version 1.1");
// this will hold the labels and number-of-buttons
// request
line1Panel = new Panel();
line1Panel.setLayout(new FlowLayout());
line2Panel = new Panel();
line2Panel.setLayout(new FlowLayout());
// to get second "line" and rest, need below1Panel
below1Panel = new Panel();
below1Panel.setLayout(new BorderLayout());
Label ident = new Label("CIS 235 Spring 2000");
ident.setFont(biggerFont);
line1Panel.add(ident);
Label title = new Label("Array of Buttons Example");
title.setFont(biggerFont);
line1Panel.add(title);
instruction = new Label("# of buttons" +
" desired:");
instruction.setFont(biggerFont);
line2Panel.add(instruction);
numButtonsField = new TextField(10);
line2Panel.add(numButtonsField);
numButtonsField.addActionListener(this);
numButtonsField.setFont(biggerFont);
numButtonsField.setText("");
//--------
// put lines 1 and 2 onto applet
//--------
// add line 1
add(line1Panel, BorderLayout.NORTH);
// add line 2 and rest
below1Panel.add(line2Panel, BorderLayout.NORTH);
// dummy label, to hold place for button panel later
dummy = new Label(" ");
below1Panel.add(dummy, BorderLayout.CENTER);
add(below1Panel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
int numButtons;
// only allowed to enter number of buttons once!
if (numButtonsChosen == false)
{
try
{
numButtons = Integer.parseInt(e.getActionCommand());
// is number of buttons reasonable?
if ((numButtons > 0) && (numButtons <= 20))
{
numButtonsChosen = true;
addButtonPanel(numButtons);
}
// what if numButtons is NOT reasonable?
else
{
numButtonsField.setText("Try > 0, <= 20");
}
} // end try block
catch(NumberFormatException ex)
{
numButtonsField.setText("Try an integer");
}
}
// if reach here --- button should have been pressed!
else
{
// print button chosen in TextField
numButtonsField.setText(e.getActionCommand());
}
}
private void addButtonPanel(int numBtns)
{
Panel buttonP; // panel of buttons
int numCols = 4, numRows; // numRows will be
// computed below
int horizSp=3, vertSp=3; // for buttonP
buttonSet = new Button[numBtns];
//-------
// change instruction label to reflect
// new situation: button presses!
//-------
// this command will REMOVE a component from
// the applet --- won't SEE this without
// validate(), below, however, to FORCE
// applet re-layout.
line2Panel.remove(instruction);
instruction = new Label("was last button pressed");
instruction.setFont(biggerFont);
// instruction is now being added AFTER the
// two labels and textfield
line2Panel.add(instruction);
numButtonsField.setText("");
// create a panel to hold the new buttons
buttonP = new Panel();
buttonP.setBackground(Color.blue);
// how many ROWS of 4 buttons each can we
// have with this many buttons?
// (ceiling: nearest "whole" number >= argument)
numRows = (int) Math.ceil( ((double)numBtns) / numCols);
// debugging code -- feel free to delete/comment out as desired
//System.out.println("numBtns: " + numBtns);
//System.out.println("numCols: " + numCols);
//System.out.println("((double)numBtns) / numCols): " +
// ((double)numBtns)/numCols );
//System.out.println("numRows: " + numRows);
buttonP.setLayout(new GridLayout(numRows, numCols,
horizSp, vertSp));
// make buttons sensitive to user input,
// and add them to buttonPanel
for(int i = 0; i < buttonSet.length; i++)
{
buttonSet[i] = new Button("Button " + i);
buttonSet[i].setFont(biggerFont);
buttonSet[i].addActionListener(this);
buttonP.add(buttonSet[i]);
}
// remove dummy, add buttonP in center of applet instead
below1Panel.remove(dummy);
below1Panel.add(buttonP, BorderLayout.CENTER);
// force applet layout to be re-done
validate();
}
}