5
SimpleNumPad
- import java.awt.event.*;
- import java.awt.*;
- public class SimpleNumPad {
- public static void main(String[] args) {
- ApplicationWindow appwin = new ApplicationWindow("Application");
- appwin.setVisible(true);
- }
- static class ApplicationWindow extends Frame {
- ApplicationWindow(String name) {
- super(name);
- addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- Menu filemenu = new Menu("File", true);
- MenuItem quit = new MenuItem("Quit");
- quit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- filemenu.add(quit);
- MenuBar menubar = new MenuBar();
- menubar.add(filemenu);
- setMenuBar(menubar);
- setLayout(new GridBagLayout());
- GridBagConstraints c = new GridBagConstraints();
- c.fill = GridBagConstraints.BOTH;
- c.weightx = 1.0;
- c.weighty = 1.0;
- c.insets = (new Insets(1, 1, 1, 1));
- c.gridwidth = 1;
- c.gridy = 0;
- c.gridx = 0;
- add(new Button("7"), c);
- c.gridx = 1;
- add(new Button("8"), c);
- c.gridx = 2;
- add(new Button("9"), c);
- c.gridy = 1;
- c.gridx = 0;
- add(new Button("4"), c);
- c.gridx = 1;
- add(new Button("5"), c);
- c.gridx = 2;
- add(new Button("6"), c);
- c.gridy = 2;
- c.gridx = 0;
- add(new Button("1"), c);
- c.gridx = 1;
- add(new Button("2"), c);
- c.gridx = 2;
- add(new Button("3"), c);
- c.gridwidth = 2;
- c.gridy = 3;
- c.gridx = 0;
- add(new Button("0"), c);
- c.gridwidth = 1;
- c.gridx = 2;
- add(new Button("."), c);
- pack();
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- int width = getSize().width;
- int height = getSize().height;
- int x = (screenSize.width - width) / 2;
- int y = (screenSize.height - height) / 3;
- setLocation(x, y);
- setResizable(true);
- }
- }
- }
$ javac SimpleNumPad.java
$ java SimpleNumPad
Comments