/**
* @(#)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"
----------------解决方案--------------------------------------------------------
java.lang.IllegalArgumentException: defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE
关闭窗口使用其中的一个。不用JFrame.ERROR。
JFrame.ERROR是图像错误时退出。
----------------解决方案--------------------------------------------------------
好象看不是很懂哦,能不能再讲讲,谢谢哈.
----------------解决方案--------------------------------------------------------
[CODE]错误提示不是写的很清楚吗?呵呵。。就是非法参数异常,你传入this.setDefaultCloseOperation(JFrame.ERROR); 的参数有问题。。defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE 也就是说缺省操作必须是后面括号其中之一(DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE )[/CODE]
----------------解决方案--------------------------------------------------------
this.setDefaultCloseOperation(JFrame.ERROR);
JFrame.ERROR我没用过。jdk文档上说是异步跟踪图像出错时退出。
编译器说默认的退出方式要是 JFrame.DO_NOTHING_ON_CLOSE,JFrame.HIDE_ON_CLOSE,JFrame.DISPOSE_ON_CLOSE,JFrame.EXIT_ON_CLOSE中的一个。你吧把JFrame.ERROR换成以上中其中一个就可以了。具体的意思,看他们的字面意思大概就知道了。
还有要显示出来的话加个main函数。
----------------解决方案--------------------------------------------------------
终于看见了,谢谢2楼,4楼,5楼的,谢谢拉.现在终于可以了.
----------------解决方案--------------------------------------------------------
好象数字不能在文本框里显示,能不能在帮我看一下啊.
----------------解决方案--------------------------------------------------------
你没实现按钮注册的监听器,当然不会显示了。
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
}什么都没写,肯定不显了。
----------------解决方案--------------------------------------------------------
呵呵,谢谢哈,现在可以了.
还请问能不能教教我要把结果计算出来,代码要怎么改.
----------------解决方案--------------------------------------------------------
要定义一个boolean型变量,判断输入的是否包含"+","-"等 非数字,如果是数字就用jtf.append("新输入的数字"),当输入的是非数字时,将Integer.parseInt(jtf.getText().trim())可以将输入的数字组合成一个操作数 【其中trim()是为了去掉空格】。
----------------解决方案--------------------------------------------------------