JAVA初学者求助——计算器修改
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
public static void main(String[] args) {
new CalFrame();
}
}
class CalFrame extends JFrame{
JPanel fatherPanle=new JPanel(); //定义父面板
JPanel textPanle=new JPanel(); //定义显示文本的面板
JPanel butPanle=new JPanel(); //定义显示各个按钮的面板
JPanel resultP=new JPanel(); //定义显示结果的面板
JTextField text=new JTextField("Good Boy"); //定义文本用来存放结果
JLabel lab=new JLabel("欢迎你来到我的计算器"); //定义欢迎的标题
BorderLayout b=new BorderLayout(); //定义一个边框布局对象
GridLayout g=new GridLayout(4,4); //定义一个网格布局对象
JButton but[]=new JButton[10]; //定义存放数字按钮的数组
MyAction1 action1=new MyAction1(); //定义按钮发生的动作
MyAction2 action2=new MyAction2();
int start;
boolean end;
String command;
String lastCommand;
double result;
public CalFrame(){ //构造函数
numButtonValue(); //以下均为函数调用
addButton();
butPanle .add(comButtonValue("."));
comButtonValue(".").addActionListener(action1);
butPanle.add(comButtonValue("clean"));
comButtonValue("clean").addActionListener(action2);
butPanle .add(comButtonValue("+"));
comButtonValue("+").addActionListener(action2);
butPanle.add(comButtonValue("-"));
comButtonValue("-").addActionListener(action2);
butPanle.add(comButtonValue("*"));
comButtonValue("*").addActionListener(action2);
butPanle.add(comButtonValue("/"));
comButtonValue("/").addActionListener(action2);
addPToP();
addTextToP();
FatherPToJF();
start=0;
end=false;
command="=";
result=0;
}
public void addTextToP(){ //定义函数将文本加入到文本面板中
textPanle.add(lab);
text.setBackground(Color .yellow );
textPanle.add(text);
}
public void addPToP(){ //定义函数将子面板添加到父面板中
fatherPanle.setLayout(b);
fatherPanle.add(textPanle ,BorderLayout.NORTH);
fatherPanle.add(butPanle,BorderLayout.CENTER);
fatherPanle.add(resultP.add(new JButton("=")),BorderLayout .SOUTH);
}
public void FatherPToJF(){ //定义函数将父面板添加到框架中,并且设置框架
this.getContentPane().add(fatherPanle);
this.setSize(300,250);
this.setLocation(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("自编java小程序");
}
public void numButtonValue(){ //定义函数给按钮赋值
for(int x=0;x<10;x++){
but[x]=new JButton(String.valueOf(x));
but[x].setSize(50,40);
}
}
public JButton comButtonValue(String c){ //定义函数加操作符按钮
return new JButton(c);
}
public void addButton(){ //定义函数将按钮添加到按钮面板中
butPanle.setLayout(g);
for(int i=0;i<but.length;i++){
butPanle.add(but);
but.addActionListener(action1);
}
}
class MyAction1 implements ActionListener{ //定义事件处理的子类
public void actionPerformed(ActionEvent e1){//实现动作发生的方法
Object x=e1.getSource();
if(start==0){
c();
start=1;
}
if(x==but[0]){
text.setText(text.getText()+but[0].getText());
}if(x==but[1]){
text.setText(text.getText()+but[1].getText());
}if(x==but[2]){
text.setText(text.getText()+but[2].getText());
}if(x==but[3]){
text.setText(text.getText()+but[3].getText());
}if(x==but[4]){
text.setText(text.getText()+but[4].getText());
}if(x==but[5]){
text.setText(text.getText()+but[5].getText());
}if(x==but[6]){
text.setText(text.getText()+but[6].getText());
}if(x==but[7]){
text.setText(text.getText()+but[7].getText());
}if(x==but[8]){
text.setText(text.getText()+but[8].getText());
}if(x==but[9]){
text.setText(text.getText()+but[9].getText());
}if(e1.getActionCommand().equals(".")){
text.setText(text.getText()+".");
}
}
}
class MyAction2 implements ActionListener{
public void actionPerformed(ActionEvent e2){ //实现动作发生的方法
if(start==1){
result=Double.parseDouble(text.getText());
c();}
if(e2.getActionCommand().equals("+")){
command="+";end=true;
}if(e2.getActionCommand().equals("-")){
command="-";end=true;
}if(e2.getActionCommand().equals("*")){
command="*";end=true;
}if(e2.getActionCommand().equals("/")){
command="/";end=true;
}
if(e2.getActionCommand().equals("=")&&end==true){
jiSuan();
start=0;
}
}
}
public void jiSuan(){
double x=Double.parseDouble(text.getText());
if(command=="+"){
result+=x;
}else if(command=="-"){
result-=x;
}else if(command=="*"){
result*=x;
}else if(command=="/"){
result/=x;
}
text.setText(String.valueOf(result));
}
public void c(){
text.setText("");
}
}