基本要求:
1、采用Java Application方式建立应用程序框架
2、仿照Windows计算器,利用多种布局方式实现界面设计
3、完成简单的整数加、减、乘、除运算
简单计算器的界面如下:
代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Gui_Calculator {
JFrame jFrame;//
JLabel text,text2,text3,text4;// *** 作数1,运算符, *** 作数2,结果
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;//数字
JButton b_add,b_sub,b_mul,b_div;//+,-,*,/
JButton b_del,b_equal;//删除,=
JPanel jPanel,jPanel2;//jPanel放 *** 作数1,运算符, *** 作数2,结果 jPanel2放按钮
Container contentPane;//
Font font=new Font("微软黑体",Font.PLAIN,25);//设置字体为25磅的普通微软黑体
double outcome=0;//初始化结果为零
public Gui_Calculator() {
jFrame=new JFrame("计算器");
jFrame.setBounds(300, 200, 300, 400);//位置及大小
jFrame.addWindowListener(new WindowAdapter(){//关闭窗口的监听
public void windowClosing(WindowEvent e){
System.out.println("系统退出!");
System.exit(1);
}
});
contentPane=jFrame.getContentPane();
//文本框右对齐
text=new JLabel(" ",SwingConstants.RIGHT);
text.setFont(font);
text2=new JLabel(" ",SwingConstants.RIGHT);
text2.setFont(font);
text3=new JLabel(" ",SwingConstants.RIGHT);
text3.setFont(font);
text4=new JLabel(" ",SwingConstants.RIGHT);
text4.setFont(font);
//数字button的创建及监听 b0~b9:内容与b0类似
b0=new JButton("0");
b0.setFont(font);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {//没有运算符 填写 *** 作数1
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"0");
}
else {//有运算符 填写 *** 作数2
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"0");
}
}
if(text4.getText()!=" ") {//有运算结果 重新填写 *** 作数1并清空运算符、 *** 作数2、结果
text.setText("0");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b1=new JButton("1");
b1.setFont(font);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"1");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"1");
}
}
if(text4.getText()!=" ") {
text.setText("1");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b2=new JButton("2");
b2.setFont(font);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"2");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"2");
}
}
if(text4.getText()!=" ") {
text.setText("2");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b3=new JButton("3");
b3.setFont(font);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"3");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"3");
}
}
if(text4.getText()!=" ") {
text.setText("3");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b4=new JButton("4");
b4.setFont(font);
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"4");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"4");
}
}
if(text4.getText()!=" ") {
text.setText("4");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b5=new JButton("5");
b5.setFont(font);
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"5");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"5");
}
}
if(text4.getText()!=" ") {
text.setText("5");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b6=new JButton("6");
b6.setFont(font);
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"6");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"6");
}
}
if(text4.getText()!=" ") {
text.setText("6");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b7=new JButton("7");
b7.setFont(font);
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"7");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"7");
}
}
if(text4.getText()!=" ") {
text.setText("7");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b8=new JButton("8");
b8.setFont(font);
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"8");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"8");
}
}
if(text4.getText()!=" ") {
text.setText("8");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
b9=new JButton("9");
b9.setFont(font);
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text2.getText()==" ") {
if(text.getText()==" ") {
text.setText("");
}
text.setText(text.getText()+"9");
}
else {
if(text4.getText()==" ") {
if(text3.getText()==" ") {
text3.setText("");
}
text3.setText(text3.getText()+"9");
}
}
if(text4.getText()!=" ") {
text.setText("9");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
}
});
//运算符button的创建及监听 b_add:+,b_sub:-,b_mul:*,b_div:/
b_add=new JButton("+");
b_add.setFont(font);
b_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text3.getText()==" ") {
if(text.getText()!=" ") {
text2.setText("+");
}
}
if(text4.getText()!="不能除零!!!"&&text4.getText()!=" ") {
text.setText(""+outcome);
text2.setText("+");
text3.setText(" ");
text4.setText(" ");
}
}
});
b_sub=new JButton("-");
b_sub.setFont(font);
b_sub.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text3.getText()==" ") {
if(text.getText()!=" ") {
text2.setText("-");
}
}
if(text4.getText()!="不能除零!!!"&&text4.getText()!=" ") {
text.setText(""+outcome);
text2.setText("-");
text3.setText(" ");
text4.setText(" ");
}
}
});
b_mul=new JButton("*");
b_mul.setFont(font);
b_mul.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text3.getText()==" ") {
if(text.getText()!=" ") {
text2.setText("*");
}
}
if(text4.getText()!="不能除零!!!"&&text4.getText()!=" ") {
text.setText(""+outcome);
text2.setText("*");
text3.setText(" ");
text4.setText(" ");
}
}
});
b_div=new JButton("/");
b_div.setFont(font);
b_div.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text3.getText()==" ") {
if(text.getText()!=" ") {
text2.setText("/");
}
}
if(text4.getText()!="不能除零!!!"&&text4.getText()!=" ") {
text.setText(""+outcome);
text2.setText("/");
text3.setText(" ");
text4.setText(" ");
}
}
});
//删除button的创建及监听 b_del
b_del=new JButton("CE");
b_del.setFont(font);
b_del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText(" ");
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
}
});
//等于button的创建及监听 b_equal
b_equal=new JButton("=");
b_equal.setFont(font);
b_equal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(text3.getText()!=" ") {//有 *** 作数2 进行运算
switch (text2.getText()){
case "+"://加
outcome=Double.valueOf(text.getText().trim())+Double.valueOf(text3.getText().trim());
text4.setText("="+outcome);
break;
case "-"://减
outcome=Double.valueOf(text.getText().trim())-Double.valueOf(text3.getText().trim());
text4.setText("="+outcome);
break;
case "*"://乘
outcome=Double.valueOf(text.getText().trim())*Double.valueOf(text3.getText().trim());
text4.setText("="+outcome);
break;
case "/"://除
if(Integer.valueOf(text3.getText())==0) {//除数为零
text4.setText("不能除零!!!");
}
else {//除数不为零
outcome=Double.valueOf(text.getText().trim())/Double.valueOf(text3.getText().trim());
text4.setText("="+outcome);
}
break;
}
}
if(text2.getText()==" "&&text.getText()!=" ") {//没有运算符 有 *** 作数1 直接等于 *** 作数1
outcome=Double.valueOf(text.getText());
text4.setText("="+outcome);
}
}
});
jPanel=new JPanel();
jPanel2=new JPanel();
jPanel.setLayout(new GridLayout(4,1));
jPanel2.setLayout(new GridLayout(4,4));
//添加组件
jPanel.add(text);
jPanel.add(text2);
jPanel.add(text3);
jPanel.add(text4);
jPanel2.add(b_add);
jPanel2.add(b_sub);
jPanel2.add(b_mul);
jPanel2.add(b_div);
jPanel2.add(b9);
jPanel2.add(b0);
jPanel2.add(b_del);
jPanel2.add(b_equal);
jPanel2.add(b5);
jPanel2.add(b6);
jPanel2.add(b7);
jPanel2.add(b8);
jPanel2.add(b1);
jPanel2.add(b2);
jPanel2.add(b3);
jPanel2.add(b4);
contentPane.add(jPanel,BorderLayout.NORTH);
contentPane.add(jPanel2);
jFrame.show();
}
public static void main(String[] args) {
new Gui_Calculator();
}
}
该计算器的功能简单只能实现加、减、乘、除以及求出结果后的连续运算的简单功能
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)