⽤java编计算机_⽤Java编写⼀个计算机⽤Java编写⼀个计算机
package com;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.GridLayout;
public class GUIDemol {
public static void main(String[] args){
Calculator f=new Calculator();
f.setTitle(“计算机”);
f.setSize(250,180);
f.setLocation(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class Calculator extends JFrame implements ActionListener{
JTextField jt;
JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb0,jbdian,jbjia,jbjian,jbcheng,jbchu,jbdeng;
String flag;
double result=0;
double num1,num2;
Calculator(){
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(4,4,4,4));
jb1=new JButton(“1”);
jb2=new JButton(“2”);
jb3=new JButton(“3”);
jb4=new JButton(“4”);
jb5=new JButton(“5”);
jb6=new JButton(“6”);
jb7=new JButton(“7”);
jb8=new JButton(“8”);
jb9=new JButton(“9”);
jb0=new JButton(“0”);
jbjia=new JButton("+");
jbjian=new JButton("-");
jbcheng=new JButton("*");
jbchu=new JButton("/");
jbdeng=new JButton("=");
jbdian=new JButton(".");
jp.add(jb1);
jp.add(jb2);
jp.add(jb3);
jp.add(jbjia);
jp.add(jb4);
jp.add(jb5);
jp.add(jb6);
jp.add(jbjian);
计算机专业javajp.add(jb7);
jp.add(jb8);
jp.add(jb9);
jp.add(jbcheng);
jp.add(jbdian);
jp.add(jb0);
jp.add(jbdeng);
jp.add(jbchu);
jt=new JTextField(20);
jt.setHorizontalAlignment(JTextField.RIGHT); add(jt,BorderLayout.NORTH);
add(jp);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
jb6.addActionListener(this);
jb7.addActionListener(this);
jb8.addActionListener(this);
jb9.addActionListener(this);
jb0.addActionListener(this);
jbdian.addActionListener(this);
jbjia.addActionListener(this);
jbjian.addActionListener(this);
jbcheng.addActionListener(this);
jbchu.addActionListener(this);
jbdeng.addActionListener(this);
}
public void actionPerformed(ActionEvent e){ String Text();
Source()==jb1){
jt.setText(s+“1”);
} else Source()==jb2){
jt.setText(s+“2”);
} else Source()==jb3){
jt.setText(s+“3”);
} else Source()==jb4){
jt.setText(s+“4”);
} else Source()==jb5){
jt.setText(s+“5”);
} else Source()==jb6){
jt.setText(s+“6”);
} else Source()==jb7){
jt.setText(s+“7”);
} else Source()==jb8){
jt.setText(s+“8”);
} else Source()==jb9){
jt.setText(s+“9”);
} else Source()==jb0){
jt.setText(s+“0”);
} else Source()==jbdian){ jt.setText(s+".");
}
else Source()==jbjia){
num1=Double.parseDouble(s); flag=“jia”;
jt.setText("");
}else Source()==jbjian){ num1=Double.parseDouble(s); flag=“jian”;
jt.setText("");
}else Source()==jbcheng){ num1=Double.parseDouble(s); flag=“cheng”;
jt.setText("");
}else Source()==jbchu){ num1=Double.parseDouble(s); flag=“chu”;
jt.setText("");
}else Source()==jbdeng){ num2=Double.parseDouble(s);
if(“jia”.equals(flag))
result=num1+num2;
else if(“jian”.equals(flag))
result=num1-num2;
else if(“cheng”.equals(flag)) result=num1*num2;
else if(“chu”.equals(flag)) result=num1/num2;
s=result+"";
jt.setText(s);
}
}
}
这个有⼀点缺陷⿇烦有⼈帮我解决(实践后可知)
标签:Java,计算机,jt,jp,else,add,new,编写,JButton

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。