Parents : Curriculum : ICT

6th Form Evening

/*
* Swing application that produces a GUI which has user interaction
*
* Written for the 6th Form Evening at Nailsea School 29/01/07
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class SwingApplication {

private static String labelPrefix = "Number of button clicks: ";
private int numClicks = 0;
final JLabel label = new JLabel(labelPrefix + "0 ");
private JPanel pane = new JPanel(new GridLayout(0, 1));


public Component createComponents() {

JButton addButton = new JButton("Add one button");
JButton colourButton = new JButton("Press to make me angry");
addButton.setMnemonic(KeyEvent.VK_I);
// add code in here
label.setLabelFor(addButton);
//add code in here


/*
* An easy way to put space between a top-level container
* and its contents is to put the contents in a JPanel
* that has an "empty" border.
*/


pane.add(addButton);
pane.add(label);

pane.add(colourButton);

pane.setBorder(BorderFactory.createEmptyBorder(

30, //top
30, //left
10, //bottom
30) //right
);


return pane;

}


/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {


//Create and set up the window.
JFrame frame = new JFrame("6th Form Tester");
// add code in here


// Create a new SwingApplication Object
SwingApplication app = new SwingApplication();
Component contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);


//Display the window.
frame.pack();
frame.setVisible(true);

}

 

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}