使用Java的GUI图形用户界面编程设计并编写一个计算器程序

基本要求]
(1)实现加、减、乘、除的双目单类别的基础运算;
(2)实现加、减、乘、除的多目多类别的混合运算;
(3)实现退格、清零等运算。
[界面要求

[实现提示]
GUI编程 Component的事件响应。

进口的java.awt.BorderLayout;
进口java.awt.GridLayout中;

进口java.awt.event.ActionEvent;

进口java.awt.event.ActionListener;

进口的javax。 swing.JButton;

进口javax.swing.JFrame中;

进口javax.swing.JPanel;

进口javax.swing.JTextField中;

/ /暂时不考虑这个问题甚至添加

/ /点的第二个操作点的操作员点运算符点=结果

公共类计算器实现的ActionListener {
> JTextField的JTF =新的JTextField(10);

私人布尔追加= FALSE;

私人字符串OP1 =“0”;

私人字符串运算符=“+”;

@覆盖

公共无效的actionPerformed(ActionEvent的E){

字符串COMN = e.getActionCommand();。!

/ /处理数字

如果(“0123456789”的indexOf(COMN)= -1){

如果(追加){/ /追加

字符串温度= jtf.getText() ;

jtf.setText(温度+ COMN);

}其他{/ /替换

jtf.setText(COMN);

追加= TRUE;

/ /处理运营商

否则,如果(“+ - * /”的indexOf(COMN)= -1){

OP1 = JTF。的getText();

运算符= COMN;

追加= FALSE;

的} else if(“=”的indexOf(COMN)= -1。){

字符串OP2 = jtf.getText();

双D1 = Double.parseDouble(OP1);

双D2 = Double.parseDouble(OP2);

如果(“+”等于(操作者)。){

D1 = D1 + D2;

}否则,如果(“ - ”等于(操作员)。){

D1 = D1 - D2;

}否则,如果(“*”的equals(操作员)。){

D1 = D1 * D2;

的} else if(“/”等于(操作者)。){

D1 = D1 / D2;

jtf.setText(D1 +“”);

追加= FALSE;

}否则,如果(等于(COMN)“。”){

字符串温度= jtf.getText();

如果(temp.indexOf(“;”)== -1){

jtf.setText(温度+“。”);

追加= TRUE;

否则,如果(“+ / - ”的equals(COMN)){

字符串温度= jtf.getText();

如果(temp.startsWith(“-1”)){

JTF。的setText(temp.substring(1));

}其他{

jtf.setText(“ - ”+温度);

否则,如果(“退格”。等于(COMN)){

字符串温度= jtf.getText();

如果(temp.length()> 0){

jtf.setText(temp.substring(0,temp.length() - 1)); ..

}

的} else if(“CE”等于(COMN)| |“C”等于(COMN)){

jtf.setText(“0”);

追加= FALSE;

公众计算器(){

JFrame的JF =新的JFrame(“计算器”);

jf.add(JTF,BorderLayout.NORTH);

字串[] S1 = {“退格”,“CE”,“C”,“+”,“7”,“8”,“9”,“/”,“4”,

“5”,“6”,“*”,“1”,“2”,“3”,“ - ”,“0”,“+ / - ”,“”,。 “=”};

的JPanel JP =新的JPanel();

jf.add(JP,使用BorderLayout.CENTER);

网格布局GL =新的网格布局(5,4); jp.setLayout(GL);

JButton的[] JB =新的JButton [s1.length];

为(int i = 0;我<s1.length,我+ +){

JB [我] =新的JButton(S1由[i]);

jp.add(JB由[i]);。

JB [I] addActionListener方法,(这);

} jf.add(JP);

jtf.setEditable(假);

jf.setLocation(400,300);

jf.pack();

jf.setResizable(假);/ /设置窗口不可改变

jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);

jf.setVisible(真);

公共静态无效的主要(字串[] args){

新的计算器();
这个函数比较简单,不知道能不能满足要求
温馨提示:内容为网友见解,仅供参考
第1个回答  2014-06-10
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 Calculator extends JFrame {
JButton b0 = new JButton("0");
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
JButton b6 = new JButton("6");
JButton b7 = new JButton("7");
JButton b8 = new JButton("8");
JButton b9 = new JButton("9");
JButton jiaButton = new JButton("+");
JButton jianButton = new JButton("-");
JButton chengButton = new JButton("*");
JButton chuButton = new JButton("/");
JButton yuButton = new JButton("%");
JButton jjButton = new JButton("+/-");
JButton sqrtButton = new JButton("sqrt");
JButton dianButton = new JButton(".");
JButton dengButton = new JButton("=");
JButton daoButton = new JButton("1/x");
JButton backButton = new JButton("Backpace");
JButton cButton = new JButton("C");
public double op1;
public double op2;
public static final int JIA = 0;
public static final int JIAN = 1;
public static final int CHENG = 2;
public static final int CHU = 3;
public static final int JJ = 4;
public static final int DIAN = 5;
public int current0p = 0;
private boolean opEnd = false;

JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JTextField result = new JTextField(20);

public Calculator() {
initPanel2();
initPanel3();
panel2.setLayout(new GridLayout(5, 4));
panel1.setLayout(new BorderLayout());
panel1.add(panel3, BorderLayout.NORTH);// 设置位置
panel1.add(panel2, BorderLayout.CENTER);// 设置位置
getContentPane().add(panel1);
addActionListeners();
setSize(260, 260);
setLocation(500, 300);
setVisible(true);
setDefaultCloseOperation(Calculator.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("计算器");
}
private void initPanel2() {
// 把组件添加相应panel上

panel2.add(b7);
panel2.add(b8);
panel2.add(b9);
panel2.add(chuButton);

panel2.add(b4);
panel2.add(b5);
panel2.add(b6);
panel2.add(chengButton);

panel2.add(b1);
panel2.add(b2);
panel2.add(b3);
panel2.add(jianButton);

panel2.add(b0);
panel2.add(jjButton);
panel2.add(dianButton);
panel2.add(jiaButton);

panel2.add(daoButton);
panel2.add(yuButton);
panel2.add(sqrtButton);
panel2.add(dengButton);

}

private void addActionListeners() {
ActionHandler c = new ActionHandler();
b0.addActionListener(c);
b1.addActionListener(c);
b2.addActionListener(c);
b3.addActionListener(c);
b4.addActionListener(c);
b5.addActionListener(c);
b6.addActionListener(c);
b7.addActionListener(c);
b8.addActionListener(c);
b9.addActionListener(c);

jiaButton.addActionListener(c);
dengButton.addActionListener(c);
chengButton.addActionListener(c);
chuButton.addActionListener(c);
jianButton.addActionListener(c);
jjButton.addActionListener(c);
dianButton.addActionListener(c);
sqrtButton.addActionListener(c);
yuButton.addActionListener(c);
daoButton.addActionListener(c);
backButton.addActionListener(c);
cButton.addActionListener(c);
}

class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {

if (e.getSource() == b0) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "0");

}
if (e.getSource() == b1) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "1");
opEnd = true;
}

if (e.getSource() == b2) {

if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "2");
opEnd = true;
}
if (e.getSource() == b3) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "3");
opEnd = true;

}
if (e.getSource() == b4) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "4");
opEnd = true;
}
if (e.getSource() == b5) {

if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "5");
opEnd = true;
}
if (e.getSource() == b6) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "6");
opEnd = true;
}
if (e.getSource() == b7) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "7");
opEnd = true;
}
if (e.getSource() == b8) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "8");
opEnd = true;
}
if (e.getSource() == b9) {
if (opEnd == false) {
result.setText("");

}
result.setText(result.getText() + "9");
opEnd = true;
}
try {
if (e.getSource() == jiaButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = JIA;
}
if (e.getSource() == chengButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = CHENG;

}
if (e.getSource() == chuButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = CHU;

}
if (e.getSource() == jianButton) {
op1 = Double.parseDouble(result.getText());

// 2、说明操作数已经输入完毕
opEnd = false;

current0p = JIAN;

}

if (e.getSource() == jjButton) {
String tmp = result.getText();
if (tmp.equals("") || tmp.equals("0")) {
return;
}
if (tmp.charAt(0) == '-') {
tmp = tmp.substring(1);

} else {
tmp = '-' + tmp;
}
result.setText(tmp);
}
if (e.getSource() == dianButton) {
String tmp = result.getText();
if (tmp.equals("")) {
return;
}
if (tmp.indexOf(".") != -1) {
return;
}
tmp = tmp + ".";
result.setText(tmp);

}
if (e.getSource() == sqrtButton) {
String tmp = result.getText();
if (tmp.equals(" ")) {
return;
}
double d;
d = Double.parseDouble(tmp);// 先定义double类型d
if (d < 0) {
result.setText("不能对负数求平方根");
return;
}
op2 = Math.sqrt(d);
result.setText(op2 + "");
}

if (e.getSource() == backButton) {
String s = result.getText();
result.setText("");
for (int i = 0; i < s.length() - 1; i++) {
char a = s.charAt(i);
result.setText(result.getText() + a);
}

}
if (e.getSource() == cButton) {
result.setText("0");
opEnd = false;
}
if (e.getSource() == dengButton) {
op2 = Double.parseDouble(result.getText());

switch (current0p) {
case JIA:
result.setText(op1 + op2 + "");
break;
case JIAN:
result.setText(op1 - op2 + "");
break;
case CHENG:
result.setText(op1 * op2 + "");
break;
case CHU:
if (op2 == 0) {
result.setText("被除数不能为零");
break;
}
result.setText(op1 / op2 + "");
break;
}
opEnd = false;
}
} catch (Exception e1) {
result.setText("Wrong");
opEnd = false;
}
}

}

private void initPanel3() {

panel3.setLayout(new GridLayout(2, 1));
panel3.add(result);
panel3.add(panel4);
panel4.setLayout(new GridLayout(1, 2));

panel4.add(backButton);
panel4.add(cButton);
// panel3.setPreferredSize(new Dimension(260,65));
}

public static void main(String[] args) {
Calculator c = new Calculator();// 生成类实例

}

}追问

抱歉啊,。。你的这个图片是这个样子的。。

跟我的图好像不是一样。。。能不能稍微帮我改下啊。我的是这个样子的

java编写程序:创建一个 计算器 的 界面,包含 0-9 个 数字 , A-F个字 ...
import java.awt.*;import javax.swing.*;public class MyCalculator extends JFrame{public MyCalculator() {String ss1 = "0123456789ABCDEF";String[] ss2 = {"+","-","*","\/","√ ̄","aⁿ","CE","="};\/\/有些字符不在GBK中,所以文本使用了UTF-8编码。如果要用GBK编码,...

如何使用Java AWT 创建一个简易计算器
等号按钮操作包括将标签 l1 的值存储为变量 num2,获取检查变量的值以执行相应的算术操作,并显示结果于标签 l1。清除按钮将所有变量更新为默认值 0,并将标签 l1 设置为 null,以便开始新的计算。退格按钮使用 substring 函数删除标签 l1 值的最后一位数字。处理了 StringIndexOutOfBoundsException 异常...

GUI程序设计(java)
import java.awt.*;import java.awt.event.*;public class CalculatorFrame extends JFrame { private JTextField txtDis = new JTextField("0", 16); \/\/显示文本框 private JButton[] btnGrp = new JButton[17]; \/\/17个按钮 private JPanel jpnMain = new JPanel(); \/\/主面板 private JP...

Java程序--编写一个具有图形界面的计算器程序
import java.awt.*;import java.awt.event.*;import java.io.*;public class Test implements ActionListener { Frame f = new Frame("一步运算计算器");\/\/创建窗架 TextField[] tf = new TextField[3];Label La1 = new Label("+");Button btn = new Button("=");int[] a = new ...

求一计算器java代码,最好简单一点(图形界面)
tf1.setText("0"); tf2=new TextField(10);\/\/显示记忆的索引值 tf2.setEditable(false); \/\/实例化所有按钮、设置其前景色并注册监听器 b0=new Button("Backspace"); b0.setForeground(Color.red); b0.addActionListener(new Bt()); b1=new Button("CE"); b1.setForeground(Color.red); b1.add...

用JAVA写一个计算器 要求有图形界面。简单的加减乘除,不要小数点运算...
private boolean firstDigit = true; \/\/ 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字 private double resultNum = 0.0; \/\/ 计算的中间结果 private boolean operateValidFlag = true; \/\/判断操作是否合法 private JLabel stuno = new JLabel("计算器"); \/\/学号 priv...

java课程设计题目及代码是什么?
1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。设计要求是设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。2、代码:数字按钮NumberButton类如下:import java.awt.import java.awt.event.import...

关于用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;priv...

如何用JAVA语言编写计算器小程序?
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[...

用Java程序设计语言设计一个计算器
setTitle("简单计算器");\/\/ 初始化变量 dotExist = false; \/\/ 表示当前的数是否有小数点 operated = false; \/\/ 表示任意运算符是否被按下 equaled = false; \/\/ 表示等号是否被按下 storedNumber = 0;lastOperator = '?';\/\/ 初始化窗口变量 operation = new JTextField("0");operation....

相似回答