java计算器gui代码,java制作简易计算器代码
要用java编的计算器代码
给你一个比较简单的吧。以前写的。
任县网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。成都创新互联于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
共两个类。还只是完成+、-、×、÷运算而已。
GUI只是用了AWT,很简单,相信一看就能懂了。
Calculator.java
public class Calculator{
private String result = "0";
private int op = 0,add = 1,sub = 2,mul = 3,div = 4;
private double stringToDouble(String x){
double y = Double.parseDouble(x);
return y;
}
private void operate(String x){
double x1 = stringToDouble(x);
double y = stringToDouble(result);
switch (op){
case 0:
result = x;
break;
case 1:
result = String.valueOf(y+x1);
break;
case 2:
result = String.valueOf(y-x1);
break;
case 3:
result = String.valueOf(y*x1);
break;
case 4:
if(x1!=0){
result = String.valueOf(y/x1);
}else{
result = "The divisor can't be zero!";
}
break;
}
}
public String opAdd(String x){
operate(x);
op = add;
return result;
}
public String opSubtract(String x){
operate(x);
op = sub;
return result;
}
public String opMultiply(String x){
operate(x);
op = mul;
return result;
}
public String opDivide(String x){
operate(x);
op = div;
return result;
}
public String opEquals(String x){
operate(x);
op = 0;
return result;
}
public void opClean(){
op = 0;
result = "0";
}
}
CalculatorGUI.java
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
public class CalculatorGUI{
private Frame f;
private Panel p1,p2;
private Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
private Button bPoint,bAdd,bDec,bMul,bDiv,bCal;
private TextField tf;
private String s,op;
private Calculator cal = new Calculator();
private boolean ifOp;
public CalculatorGUI(){
f = new Frame("Calculator");
p1 = new Panel();
p2 = new Panel();
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("3");
b4 = new Button("4");
b5 = new Button("5");
b6 = new Button("6");
b7 = new Button("7");
b8 = new Button("8");
b9 = new Button("9");
bPoint = new Button(".");
bAdd = new Button("+");
bDec = new Button("-");
bMul = new Button("*");
bDiv = new Button("/");
bCal = new Button("=");
tf = new TextField(25);
tf.setEditable(false);
}
public void launchFrame(){
f.setSize(220,160);
f.setResizable(false);
f.addWindowListener(new myWindowListener());
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
p1.add(tf);
f.add(p1,BorderLayout.NORTH);
p2.setLayout(new GridLayout(4,4));
b0.addActionListener(new setLabelText_ActionListener());
b1.addActionListener(new setLabelText_ActionListener());
b2.addActionListener(new setLabelText_ActionListener());
b3.addActionListener(new setLabelText_ActionListener());
b4.addActionListener(new setLabelText_ActionListener());
b5.addActionListener(new setLabelText_ActionListener());
b6.addActionListener(new setLabelText_ActionListener());
b7.addActionListener(new setLabelText_ActionListener());
b8.addActionListener(new setLabelText_ActionListener());
b9.addActionListener(new setLabelText_ActionListener());
bPoint.addActionListener(new setLabelText_ActionListener());
bAdd.addActionListener(new setOperator_ActionListener());
bDec.addActionListener(new setOperator_ActionListener());
bMul.addActionListener(new setOperator_ActionListener());
bDiv.addActionListener(new setOperator_ActionListener());
bCal.addActionListener(new setOperator_ActionListener());
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(bAdd);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(bDec);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(bMul);
p2.add(b0);
p2.add(bPoint);
p2.add(bCal);
p2.add(bDiv);
f.add(p2,BorderLayout.SOUTH);
f.setVisible(true);
}
public void setTextFieldText_Temp(){
if (tf.getText().length()15 (tf.getText().indexOf(".")==-1 || !s.equals("."))){
tf.setText(tf.getText()+s);
}else{
tf.setText((tf.getText()+s).substring(0,15));
}
}
public void setTextFieldText(){
if(ifOp){
ifOp = false;
tf.setText("");
setTextFieldText_Temp();
}else{
setTextFieldText_Temp();
}
}
public static void main(String[] args){
CalculatorGUI calculator = new CalculatorGUI();
calculator.launchFrame();
}
class myWindowListener extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
class setLabelText_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
s = tempB.getLabel();
setTextFieldText();
}
}
class setOperator_ActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Button tempB = (Button)e.getSource();
op = tempB.getLabel();
if(op.equals("+")){
tf.setText(cal.opAdd(tf.getText()));
ifOp = true;
}else if(op.equals("-")){
tf.setText(cal.opSubtract(tf.getText()));
ifOp = true;
}else if(op.equals("*")){
tf.setText(cal.opMultiply(tf.getText()));
ifOp = true;
}else if(op.equals("/")){
tf.setText(cal.opDivide(tf.getText()));
ifOp = true;
}else if(op.equals("=")){
tf.setText(cal.opEquals(tf.getText()));
ifOp = true;
}
}
}
}
求解:写一段Java程序,要求简单实现计算器的功能,是GUI编程,代码简洁最好。
import java.awt.BorderLayout;
import java.awt.GridLayout;
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;
//暂时不用考虑连加问题
//点第一个运算符 点运算符 点第二个运算符 点=出结果
public class 计算器 implements ActionListener {
JTextField jtf = new JTextField(10);
private boolean append = false;
private String op1 = "0";
private String operator = "+";
@Override
public void actionPerformed(ActionEvent e) {
String comn = e.getActionCommand();
// 处理数字
if ("0123456789".indexOf(comn) != -1) {
if (append) {// 追加
String temp = jtf.getText();
jtf.setText(temp + comn);
} else {// 替换
jtf.setText(comn);
append = true;
}
}
// 处理运算符
else if ("+-*/".indexOf(comn) != -1) {
op1 = jtf.getText();
operator = comn;
append = false;
} else if ("=".indexOf(comn) != -1) {
String op2 = jtf.getText();
double d1 = Double.parseDouble(op1);
double d2 = Double.parseDouble(op2);
if ("+".equals(operator)) {
d1 = d1 + d2;
} else if ("-".equals(operator)) {
d1 = d1 - d2;
} else if ("*".equals(operator)) {
d1 = d1 * d2;
} else if ("/".equals(operator)) {
d1 = d1 / d2;
}
jtf.setText(d1 + "");
append = false;
} else if (".".equals(comn)) {
String temp = jtf.getText();
if (temp.indexOf(".") == -1) {
jtf.setText(temp + ".");
append = true;
}
} else if ("+/-".equals(comn)) {
String temp = jtf.getText();
if (temp.startsWith("-1")) {
jtf.setText(temp.substring(1));
} else {
jtf.setText("-" + temp);
}
} else if ("Backspace".equals(comn)) {
String temp = jtf.getText();
if (temp.length() 0) {
jtf.setText(temp.substring(0, temp.length() - 1));
}
} else if ("CE".equals(comn) || "C".equals(comn)) {
jtf.setText("0");
append = false;
}
}
public 计算器() {
JFrame jf = new JFrame("计算器");
jf.add(jtf, BorderLayout.NORTH);
String[] s1 = { "Backspace", "CE", "C", "+", "7", "8", "9", "/", "4",
"5", "6", "*", "1", "2", "3", "-", "0", "+/-", ".", "=" };
JPanel jp = new JPanel();
jf.add(jp, BorderLayout.CENTER);
GridLayout gl = new GridLayout(5, 4);
jp.setLayout(gl);
JButton[] jb = new JButton[s1.length];
for (int i = 0; i s1.length; i++) {
jb[i] = new JButton(s1[i]);
jp.add(jb[i]);
jb[i].addActionListener(this);
}
jf.add(jp);
jtf.setEditable(false);
jf.setLocation(400, 300);
jf.pack();
jf.setResizable(false);// 设置窗口的大小不可变
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String[] args) {
new 计算器();
}
}
这个功能比较简单 不知道能不能满足要求
有关java里面gui的编程题,实现这样一个建议的计算器
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.Color;
public class Ex5_2 extends JFrame implements ActionListener
{
private JPanel p1 = new JPanel(); //创建面板
private JPanel p2 = new JPanel();
private JTextField t1; //文本框1用来显示输入信息
private JTextField t2; //文本框2用来显示结果信息
private JLabel label; //标签信息
StringBuffer str; //显示屏所显示的字符串
double x,y; //x和y都是运算数
int z; //Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"
private JButton b[] = new JButton[12]; //创建一个有12个按钮的数组
private JButton b1,b2,b3,b4,b5,b6,b7,b8; //算术功能按钮
public Ex5_2()
{
super("简易计算器"); //窗口名称
Container c = getContentPane(); //创建内容面板对象
t1 = new JTextField(30);
t1.setEditable(false); //只能显示,不能编辑
t2 = new JTextField(30);
t2.setEditable(false); //只能显示,不能编辑
label = new JLabel("欢迎使用小巫版计算器^_^o~ 努力!");
label.setForeground(Color.blue);
//创建一个空字符串缓冲区
str=new StringBuffer();
p2.add(label); //添加标签到面板
p2.add(t2); //添加文本框到面板
p2.add(t1); //添加文本框到面板
p2.setLayout(new GridLayout(4,1)); //把面扳布局为4行1列
for(int i=0;i10;i++) //分别为数组中0~9号的按钮设置标签,并注册监听器
{
String s=""+i;
b[i]= new JButton(s);
b[i].addActionListener(this);
}
//实例化各个按钮
b[10]= new JButton("-/+"); b[11]= new JButton(".");
b1= new JButton("/"); b2= new JButton("Back");
b3= new JButton("*"); b4= new JButton("C");
b5= new JButton("+"); b6= new JButton("Sqrt");
b7= new JButton("-"); b8= new JButton("=");
//设置按钮前景色
for(int i=0;i12;i++)
{
b[i].setForeground(Color.blue);
}
b1.setForeground(Color.red); b3.setForeground(Color.red);
b5.setForeground(Color.red); b7.setForeground(Color.red);
b2.setForeground(Color.blue); b4.setForeground(Color.blue);
b6.setForeground(Color.red); b8.setForeground(Color.blue);
//添加到面板
p1.add(b[7]); p1.add(b[8]); p1.add(b[9]); p1.add(b1); p1.add(b2);
p1.add(b[4]); p1.add(b[5]); p1.add(b[6]); p1.add(b3); p1.add(b4);
p1.add(b[1]); p1.add(b[2]); p1.add(b[3]); p1.add(b5); p1.add(b6);
p1.add(b[0]); p1.add(b[10]); p1.add(b[11]);p1.add(b7);p1.add(b8);
p1.setLayout(new GridLayout(4,5,5,5));
//注册监听器
b[10].addActionListener(this); b[11].addActionListener(this);
b1.addActionListener(this); b2.addActionListener(this);
b3.addActionListener(this); b4.addActionListener(this);
b5.addActionListener(this); b6.addActionListener(this);
b7.addActionListener(this); b8.addActionListener(this);
//将面板添加到内容面板
c.add(p2);
c.add(p1);
c.setLayout(new FlowLayout()); //设置为顺序布局
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口关闭动作
setVisible(true); //设置为可见
setResizable(false); //禁止调整框架大小
setSize(400,300); //设置窗口大小
}
//主方法实现创建一个窗口
public static void main(String[] args)
{ Ex5_2 f = new Ex5_2(); }
//按钮的事件处理
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==b4) //选择"C"清零
{
t1.setText("0"); //把文本框清零
t1.setHorizontalAlignment(JTextField.RIGHT); //文本对齐右边
str.setLength(0); //清空字符串缓冲区以准备接收新的输入运算数
}
else if(e.getSource()==b[10])//单击"+/-"选择输入的运算数是正数还是负数
{
x=Double.parseDouble(t1.getText().trim());//trim函数作用是去掉字符串中的空格
t1.setText(""+(-x));
t1.setHorizontalAlignment(JTextField.RIGHT);
}
else if (e.getSource()==b5)//单击加号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(t1.getText().trim());
str.setLength(0);
y=0d;
z=0;
}
else if(e.getSource()==b7)//单击减号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(t1.getText().trim());
str.setLength(0);
y=0d;
z=1;
}
else if(e.getSource()==b3)//单击乘号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(t1.getText().trim());
str.setLength(0);
y=0d;
z=2;
}
else if(e.getSource()==b1)//单击除号按钮获得x的值和z的值并清空y的值
{
x=Double.parseDouble(t1.getText().trim());
str.setLength(0);
y=0d;
z=3;
}
else if(e.getSource()==b8)//单击等号按钮输出计算结果
{
str.setLength(0);
switch(z)
{
case 0: t1.setText(""+(x+y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;
case 1: t1.setText(""+(x-y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;
case 2: t1.setText(""+(x*y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;
case 3: t1.setText(""+(x/y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;
}
}
else if(e.getSource()==b[11])//单击"."按钮输入小数
{
if(t1.getText().trim().indexOf('.')!=-1)//判断字符串中是否已经包含了小数点
{
}
else //如果没有小数点
{
if(t1.getText().trim().equals("0"))//如果初时显示为0
{
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
}
else if(t1.getText().trim().equals(""))//如果初时显示为空则不做任何操作
{}
else
{
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
}
}
y=0d;
}
else if(e.getSource()==b6) //求平方根
{
x=Double.parseDouble(t1.getText().trim());
if(x0)
{
t1.setText("数字格式异常");
t1.setHorizontalAlignment(JTextField.RIGHT);
}
else
{
t1.setText(""+Math.sqrt(x));
t1.setHorizontalAlignment(JTextField.RIGHT);
}
str.setLength(0);
y=0d;
}
else
{
if(e.getSource()==b[0])//如果选择的是"0"这个数字键
{
if(t1.getText().trim().equals("0"))//如果显示屏显示的为零不做操作
{}
else
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
y=Double.parseDouble(t1.getText().trim());
}
else if (e.getSource()==b2) //选择的是back键
{
if(!t1.getText().trim().equals("0"))//如果显示屏显示的不是零
{
if(str.length()!=1)
{
t1.setText(str.delete(str.length()-1,str.length()).toString());//可能抛出字符串越界异常
t1.setHorizontalAlignment(JTextField.RIGHT);
}
else
{
t1.setText("0"); t1.setHorizontalAlignment(JTextField.RIGHT);
str.setLength(0);
}
}
y=Double.parseDouble(t1.getText().trim());
}
else
{
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT);
y=Double.parseDouble(t1.getText().trim());
}
}
}
catch(NumberFormatException e1){ t1.setText("数字格式异常");
t1.setHorizontalAlignment(JTextField.RIGHT); }
catch(StringIndexOutOfBoundsException e1){t1.setText("字符串索引越界");
t1.setHorizontalAlignment(JTextField.RIGHT);}
}
}
新闻标题:java计算器gui代码,java制作简易计算器代码
链接地址:http://azwzsj.com/article/hcdssd.html