1.首先,由于计算器分为显示框
和按钮
,所以我们选用BorderLayout
边界布局;
2.窗体中放置三个JPanel面板
,第一个面板放文本框
,置于顶部;第二个面板放“清零”(AC)
按钮与“退格”
按钮,置于中部。第三个面板放数字
与操作符
组件(0-9,+ - * / . =)
,4*4网格布局
,置于窗体底部;
(1)定义面板,文本框和按钮组件:
JPanel top;
JPanel center;
JPanel botton;
JTextField xianshi;
JButton ac;
JButton tuige;
JButton bt1;
JButton bt2;
JButton bt3;
JButton bt4;
JButton bt5;
JButton bt6;
JButton bt7;
JButton bt8;
JButton bt9;
JButton bt0;
JButton dian;
JButton jia;
JButton jian;
JButton cheng;
JButton chu;
JButton dengyu;
(2)顶部布局
//绘制顶部布局(文本框)
BorderLayout borderLayout=new BorderLayout();
top=new JPanel();
add(top,BorderLayout.NORTH);
GridLayout t=new GridLayout();
top.setLayout(t);
xianshi=new JTextField();
top.add(xianshi);
(3)中部布局
//绘制中部布局(退格和归零两个按钮)
center=new JPanel();
add(center,BorderLayout.CENTER);
GridLayout c=new GridLayout(1,2,5,5);
center.setLayout(c);
ac=new JButton("AC");
tuige=new JButton("退格");
center.add(ac);
center.add(tuige);
(4)底部布局
//绘制底部布局(4*4的网格放一堆按钮)
botton=new JPanel();
add(botton,BorderLayout.SOUTH);
GridLayout b=new GridLayout(4,4,5,5);
botton.setLayout(b);
bt1=new JButton("1");
bt2=new JButton("2");
bt3=new JButton("3");
jia=new JButton("+");
bt4=new JButton("4");
bt5=new JButton("5");
bt6=new JButton("6");
jian=new JButton("-");
bt7=new JButton("7");
bt8=new JButton("8");
bt9=new JButton("9");
cheng=new JButton("*");
bt0=new JButton("0");
dian=new JButton(".");
chu=new JButton("/");
dengyu=new JButton("=");
(5)添加按钮到底部面板
botton.add(bt1);
botton.add(bt2);
botton.add(bt3);
botton.add(jia);
botton.add(bt4);
botton.add(bt5);
botton.add(bt6);
botton.add(jian);
botton.add(bt7);
botton.add(bt8);
botton.add(bt9);
botton.add(cheng);
botton.add(bt0);
botton.add(dian);
botton.add(chu);
botton.add(dengyu);
二、添加监听器
1.首先,我们需要两个监听器,第一个监听器用来监听0-9数字键
,为数字监听器;另一个监听器用来监听加减乘除按钮
,为功能监听器。
2.我们需要立一个flag,且令flag初始值为true,以便于我们重启计算器。
(1).创建数字监听器Listener1:
class Listener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String dianji = e.getActionCommand();//获得点击的按钮
if(flag) {
xianshi.setText("");
flag=false;
}if(!dianji.equals("退格")){
xianshi.setText(xianshi.getText()+dianji);
}if(dianji.equals("AC")) {
xianshi.setText("0");
flag=true;
}if(dianji.equals("退格")) {
if(xianshi.getText().length()>1) {
xianshi.setText(xianshi.getText().substring(0,xianshi.getText().length()-1));
//字符串长度减1
}else {
xianshi.setText("0");
}if(xianshi.getText().equals("0")) {
flag=true;//当文本框显示0的时候,重启
}
}if(dianji.equals(".")) {
}
}
}
(2).创建功能监听器Listener2
class Listener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String dianji = e.getActionCommand();
if(flag){
fuhao=dianji;
}else {
if(fuhao.equals("+")) {
result += Double.parseDouble(xianshi.getText());//字符串转double
}if(fuhao.equals("-")) {
result -= Double.parseDouble(xianshi.getText());
}if(fuhao.equals("*")) {
result *= Double.parseDouble(xianshi.getText());
}if(fuhao.equals("/")) {
result /= Double.parseDouble(xianshi.getText());
}if(fuhao.equals("=")) {
result = Double.parseDouble(xianshi.getText());
}
xianshi.setText(""+result);
fuhao=dianji;
flag=true;
}
}
}
##四、完整代码如下:
package jiemian;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JSQ extends JFrame {
public static void main(String[] args) {
JSQ frame=new JSQ();
frame.setSize(350,250);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
JPanel top;
JPanel center;
JPanel botton;
JTextField xianshi;
JButton ac;
JButton tuige;
JButton bt1;
JButton bt2;
JButton bt3;
JButton bt4;
JButton bt5;
JButton bt6;
JButton bt7;
JButton bt8;
JButton bt9;
JButton bt0;
JButton dian;
JButton jia;
JButton jian;
JButton cheng;
JButton chu;
JButton dengyu;
Listener1 a1;
Listener2 a2;
boolean flag;//定义运算开始标志
double result;//定义result接收运算结果
String fuhao;//定义fuhao接收符号
public JSQ() {
fuhao="=";
result=0;
flag=true;
this.setTitle("小王计算器");
//绘制顶部布局(文本框)
BorderLayout borderLayout=new BorderLayout();
top=new JPanel();
add(top,BorderLayout.NORTH);
GridLayout t=new GridLayout();
top.setLayout(t);
xianshi=new JTextField();
top.add(xianshi);
//绘制中部布局(退格和归零两个按钮)
center=new JPanel();
add(center,BorderLayout.CENTER);
GridLayout c=new GridLayout(1,2,5,5);
center.setLayout(c);
ac=new JButton("AC");
tuige=new JButton("退格");
center.add(ac);
center.add(tuige);
//绘制底部布局(4*4的网格放一堆按钮)
botton=new JPanel();
add(botton,BorderLayout.SOUTH);
GridLayout b=new GridLayout(4,4,5,5);
botton.setLayout(b);
bt1=new JButton("1");
bt2=new JButton("2");
bt3=new JButton("3");
jia=new JButton("+");
bt4=new JButton("4");
bt5=new JButton("5");
bt6=new JButton("6");
jian=new JButton("-");
bt7=new JButton("7");
bt8=new JButton("8");
bt9=new JButton("9");
cheng=new JButton("*");
bt0=new JButton("0");
dian=new JButton(".");
chu=new JButton("/");
dengyu=new JButton("=");
a1=new Listener1();
a2=new Listener2();
//添加按钮到底部面板
botton.add(bt1);
botton.add(bt2);
botton.add(bt3);
botton.add(jia);
botton.add(bt4);
botton.add(bt5);
botton.add(bt6);
botton.add(jian);
botton.add(bt7);
botton.add(bt8);
botton.add(bt9);
botton.add(cheng);
botton.add(bt0);
botton.add(dian);
botton.add(chu);
botton.add(dengyu);
//按钮全部加上监听器(a1为数字监听器,a2为运算符号的功能监听器)
bt1.addActionListener(a1);
bt2.addActionListener(a1);
bt3.addActionListener(a1);
jia.addActionListener(a2);
bt4.addActionListener(a1);
bt5.addActionListener(a1);
bt6.addActionListener(a1);
jian.addActionListener(a2);
bt7.addActionListener(a1);
bt8.addActionListener(a1);
bt9.addActionListener(a1);
cheng.addActionListener(a2);
bt0.addActionListener(a1);
dian.addActionListener(a1);
chu.addActionListener(a2);
dengyu.addActionListener(a2);
tuige.addActionListener(a1);
ac.addActionListener(a1);
}
class Listener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String dianji = e.getActionCommand();//获得点击的按钮
if(flag) {
xianshi.setText("");
flag=false;
}if(!dianji.equals("退格")){
xianshi.setText(xianshi.getText()+dianji);
}if(dianji.equals("AC")) {
xianshi.setText("0");
flag=true;
}if(dianji.equals("退格")) {
if(xianshi.getText().length()>1) {
xianshi.setText(xianshi.getText().substring(0,xianshi.getText().length()-1));
//字符串长度减1
}else {
xianshi.setText("0");
}if(xianshi.getText().equals("0")) {
flag=true;//当文本框显示0的时候,重启
}
}if(dianji.equals(".")) {
}
}
}
class Listener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String dianji = e.getActionCommand();
if(flag){
fuhao=dianji;
}else {
if(fuhao.equals("+")) {
result += Double.parseDouble(xianshi.getText());//字符串转double
}if(fuhao.equals("-")) {
result -= Double.parseDouble(xianshi.getText());
}if(fuhao.equals("*")) {
result *= Double.parseDouble(xianshi.getText());
}if(fuhao.equals("/")) {
result /= Double.parseDouble(xianshi.getText());
}if(fuhao.equals("=")) {
result = Double.parseDouble(xianshi.getText());
}
xianshi.setText(""+result);
fuhao=dianji;
flag=true;
}
}
}
}