/**
* @(#)CalculatorFrame.java
*
* JFC Calculator application
*
* @author
* @version 1.00 2007/8/28
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.EventListener;
public class CalculatorFrame extends JFrame implements ActionListener{
/**
* The constructor
*/
JTextField jtf = new JTextField();
public CalculatorFrame() {
this.setDefaultCloseOperation(JFrame.ERROR);
Container c = this.getContentPane();
JPanel jp1 = new JPanel();
c.add(jtf,BorderLayout.NORTH);
c.add(jp1,BorderLayout.CENTER);
jp1.setLayout(new GridLayout(4,4));
JButton b = null;
b = new JButton("1");
b.addActionListener(this);
jp1.add(b);
b = new JButton("2");
b.addActionListener(this);
jp1.add(b);
b = new JButton("3");
b.addActionListener(this);
jp1.add(b);
b = new JButton("+");
b.addActionListener(this);
jp1.add(b);
b = new JButton("4");
b.addActionListener(this);
jp1.add(b);
b = new JButton("5");
b.addActionListener(this);
jp1.add(b);
b = new JButton("6");
b.addActionListener(this);
jp1.add(b);
b = new JButton("-");
b.addActionListener(this);
jp1.add(b);
b = new JButton("7");
b.addActionListener(this);
jp1.add(b);
b = new JButton("8");
b.addActionListener(this);
jp1.add(b);
b = new JButton("9");
b.addActionListener(this);
jp1.add(b);
b = new JButton("*");
b.addActionListener(this);
jp1.add(b);
b = new JButton("0");
b.addActionListener(this);
jp1.add(b);
b = new JButton(".");
b.addActionListener(this);
jp1.add(b);
b = new JButton("=");
b.addActionListener(this);
jp1.add(b);
b = new JButton("\\");
b.addActionListener(this);
jp1.add(b);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new JMenuItem();
menuFile.setText("File");
menuFileExit.setText("Exit");
// Add action listener.for the menu button
menuFileExit.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// CalculatorFrame.this.windowClosed();
jtf.setText(jtf.getText() + e.getActionCommand());
}
}
);
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("Calculator");
setJMenuBar(menuBar);
setSize(new Dimension(400, 400));
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
CalculatorFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
// TODO: Check if it is safe to close the application
// Exit application.
System.exit(0);
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
}
}
我用的是JFC打开的.在JCreator中.谢谢啊.运行结果是:
--------------------Configuration: Calculator - j2sdk1.4.2 <Default> - <Default>--------------------
java.lang.IllegalArgumentException: defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE
at javax.swing.JFrame.setDefaultCloseOperation(JFrame.java:357)
at CalculatorFrame.<init>(CalculatorFrame.java:23)
at Calculator.main(Calculator.java:15)
Exception in thread "main"