/**
* A Simple stand-alone GUI program based on Swing.
* The program displays all the command line args in a JFrame,
* each arg becomes a JButton.
*
* There are a number of methods defined in this program that
* use various layout managers, one one should get called.
* Try using different layout managers to see how they change
* the appearance of things...
* (by default you will only see one button!)
*
* @author Dave Hollinger
* @version 1.0
*/
import javax.swing.*;
import java.awt.*; // needed for Container
class ListArgs {
static private final String label = "Hello World";
static private final String title = "Simple Java GUI";
public static void main(String [] args) {
// create a top-level container with a title
JFrame frame = new JFrame(title);
// set the size
frame.setSize(400,200);
// establish what happens when the window is closed
// (without this the program would keep running!)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// grab the content container associated with the frame.
Container c = frame.getContentPane();
/* uncomment one of the lines below to establish which layout manager
is used (these methods are defined below).
*/
defaultLayout(c,args);
//flowLayout(c,args);
//gridLayout(c,args);
//gridLayout2(c,args);
// make the frame visible
frame.setVisible(true);
}
/**
* defaultLayout doesn't specify a layout manager.
* (the default is BorderLayout).
*
* The result is that all the buttons are placed on top of each other
* and you can only see the last one added
* @param c is the container to which we add the buttons
* @param args list of strings used to create buttons.
*/
static void defaultLayout(Container c, String [] args) {
// add each command line arg as a button
for (int i=0;i<args.length;i++) {
c.add( new JButton(args[i]));
System.out.println("Adding " + args[i]);
}
}
/**
* flowLayout
* when adding each button the layout manager tries to get things to
* fit as best as possible
*
* @param c is the container to which we add the buttons
* @param args list of strings used to create buttons.
*/
static void flowLayout(Container c, String [] args) {
// set layout manager to FlowLayout, alignment is left,
// with 5 pixels space between elements (horiz and vertical).
c.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
// add each command line arg as a button
for (int i=0;i<args.length;i++) {
c.add( new JButton(args[i]));
System.out.println("Adding " + args[i]);
}
}
/**
* gridLayout created so that each button is in a row by itself
*
* @param c is the container to which we add the buttons
* @param args list of strings used to create buttons.
*/
static void gridLayout(Container c, String [] args) {
// use grid layout with enough rows and 5 pixels between elements.
c.setLayout(new GridLayout(args.length,1,5,5));
// add each command line arg as a button
for (int i=0;i<args.length;i++) {
c.add( new JButton(args[i]));
System.out.println("Adding " + args[i]);
}
}
/**
* gridLayout2 created so that there are 2 buttons on each row
*
* @param c is the container to which we add the buttons
* @param args list of strings used to create buttons.
*/
static void gridLayout2(Container c, String [] args) {
// use grid layout with enough rows and 5 pixels between elements.
c.setLayout(new GridLayout(args.length/2+1,2,5,5));
// add each command line arg as a button
for (int i=0;i<args.length;i++) {
c.add( new JButton(args[i]));
System.out.println("Adding " + args[i]);
}
}
}