Swing Application, an Overview
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;
public Component createComponents() {
final JLabel label =
new JLabel(labelPrefix + "0 ");
JButton button = new
JButton("I'm a Swing button!");
button.setMnemonic(KeyEvent.VK_I);
button.setToolTipText("Tips!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
numClicks++;
label.setText(labelPrefix + numClicks);
}
});
JPanel pane = new JPanel();
pane.setBorder(
BorderFactory.createTitledBorder("Components"));
pane.setLayout(new GridLayout(0, 1));
pane.setBackground(new Color(0xffA0A0));
pane.add(button);
pane.add(label);
return pane;
}
public static void main(String[] args) {
//Create the top-level container and add contents to it.
JFrame frame = new JFrame("SwingApplication");
SwingApplication app = new SwingApplication();
Component contents = app.createComponents();
frame.getContentPane().add(contents, BorderLayout.CENTER);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}