java 计算器 事件处理

如题:添加事件处理使计算器可以运行(特别感谢)publicclassCalculatorextendsJFrameimplementsActionListener{pri... 如题:
添加事件处理 使计算器可以运行(特别感谢)
public class Calculator extends JFrame implements ActionListener {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;

public Calculator() {
//对图形组件实例化
jf=new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);

String str="123+456-789*0.=/";
for(int i=0;i<allButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));

}

}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());

northPanel.add(jtf);
for(int i=0;i<16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);

jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;i<allButtons.length;i++){

allButtons[i].addActionListener(this);

}

clearButton.addActionListener(this);

}

//事件处理

public void actionPerformed(ActionEvent e) {
//在这里完成事件处理 使计算器可以运行

}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i<16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}

public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args){
new Calculator().showMe();
}
}
展开
 我来答
常州杰哥讲故事
2010-07-12 · ❤️杰哥讲故事,带你领略奇幻世界...
常州杰哥讲故事
采纳数:30 获赞数:46

向TA提问 私信TA
展开全部
最好将监听事件单独实现,源代码修改如下:
package cn.jiqimiao.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator extends JFrame {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
private double result;// 用于存储计算结果
private String lastCommand;// 记录点击的符号
private boolean start;// 开始标志

public Calculator() {
// 对图形组件实例化
jf = new JFrame("任静的计算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter() {
public void windowClosing() {
System.exit(0);
}
});
allButtons = new JButton[16];
clearButton = new JButton("清除");
jtf = new JTextField(25);

String str = "123+456-789*0.=/";
for (int i = 0; i < allButtons.length; i++) {
allButtons[i] = new JButton(str.substring(i, i + 1));
}
}

public void init() {
lastCommand = "=";
// 创建事件侦听对象
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
ActionListener clear = new ClearAction();
// 完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4, 4));
southPanel.setLayout(new FlowLayout());

northPanel.add(jtf);
for (int i = 0; i < 16; i++) {
centerPanel.add(allButtons[i]);
String text = allButtons[i].getText();
// 添加监听事件,符号添加符号事件,数字添加数字事件
if (text.equals("+") || text.equals("-") || text.equals("*")
|| text.equals("/") || text.equals("=")) {
addButtonListener(allButtons[i], command);
} else {
addButtonListener(allButtons[i], insert);
}
}
southPanel.add(clearButton);
// 添加清除事件
addButtonListener(clearButton, clear);

jf.add(northPanel, BorderLayout.NORTH);
jf.add(centerPanel, BorderLayout.CENTER);
jf.add(southPanel, BorderLayout.SOUTH);
}

public void setFontAndColor() {
Font f = new Font("宋体", Font.BOLD, 24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f, 0xa0, 0xfb));
for (int i = 0; i < 16; i++) {
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}

public void showMe() {
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new Calculator().showMe();
}

/**
* 点击数字按钮事件,用于显示数字及将start标志置为false;
*/
private class InsertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (start) {
jtf.setText("");
start = false;
}
jtf.setText(jtf.getText() + input);
}
}

/**
* 点击符号按钮事件,并进行计算
*/
private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
// 如果start为true,表示开始那么点击-号表示输入负数
if (start) {
if (command.equals("-")) {
jtf.setText(command);
start = false;
} else
lastCommand = command;
} else {// start为false,进行计算
calculate(Double.parseDouble(jtf.getText()));
lastCommand = command;
start = true;
}
}
}

/**
* 点击清除按钮事件
*/
private class ClearAction implements ActionListener {

public void actionPerformed(ActionEvent e) {
jtf.setText("");
lastCommand = "=";
start = true;
result = 0;
}

}

/**
* 计算方法更具输入的数据和符号进行计算
*
* @param x
* 输入的数字
*
*/
public void calculate(double x) {
if (lastCommand.equals("+"))
result += x;
else if (lastCommand.equals("-"))
result -= x;
else if (lastCommand.equals("*"))
result *= x;
else if (lastCommand.equals("/"))
result /= x;
else if (lastCommand.equals("="))
result = x;
jtf.setText("" + result);
}

/**
* 用来设置按钮及添加相应的监听
*
* @param button
* 按钮
* @param listener
* 相应事件侦听对象
*
*/
private void addButtonListener(JButton button, ActionListener listener) {
button.addActionListener(listener);
}
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式