要用java写一个简单的计算器,作为初学者的我一开始觉得很简单,但是实际操作起来却遇到了很多问题,在这里给大家一一分享。
要完成一个计算器,首先要想好它的功能——能进行四则运算,并且能循环使用即可。所以我们需要1、2、3、4、5、6、7、8、9、0、+、-、*、/、=、AC这16个按钮Button,然后需要一个显示计算过程和计算结果的地方,这里我们采用TextField这一组件来显示。
将这些添加到我们所构造的Frame子类中。
import java.awt.*;
import java.awt.event.*;
public class template extends Frame
{
static TextField tf1;
static Button bt1;
static Button bt2;
static Button bt3;
static Button bt4;
static Button bt5;
static Button bt6;
static Button bt7;
static Button bt8;
static Button bt9;
static Button bt10;
static Button bt11;
static Button bt12;
static Button bt13;
static Button bt14;
static Button bt15;
static Button bt16;
public static void main(String[] args)
{
template calculator = new template();
calculator.setResizable(false);
calculator.setSize(400,400);
calculator.setVisible(true);
calculator.setLayout(new FlowLayout());
tf1 = new TextField();
bt1 = new Button("1");
bt2 = new Button("2");
bt3 = new Button("3");
bt4 = new Button("4");
bt5 = new Button("5");
bt6 = new Button("6");
bt7 = new Button("7");
bt8 = new Button("8");
bt9 = new Button("9");
bt10 = new Button("0");
bt11 = new Button("+");
bt12 = new Button("-");
bt13 = new Button("*");
bt14 = new Button("/");
bt15 = new Button("=");
bt16 = new Button("AC");
calculator.add(tf1);
calculator.add(bt1);
calculator.add(bt2);
calculator.add(bt3);
calculator.add(bt4);
calculator.add(bt5);
calculator.add(bt6);
calculator.add(bt7);
calculator.add(bt8);
calculator.add(bt9);
calculator.add(bt10);
calculator.add(bt11);
calculator.add(bt12);
calculator.add(bt13);
calculator.add(bt14);
calculator.add(bt15);
calculator.add(bt16);
}
}
在这里我们添加了一个画板Panel,先将组件Button、TextField放到画板里面,再把画板添加到计算器窗口中,但这样做完之后,我们javac、java一遍之后会发现窗口是空白的
这是我们没有选择布局的原因,在主函数中添加布局就能看到各个组件了。
如添加流形布局calculator.setLayout(new FlowLayout());
就可以得到以下窗口
在这里大家可以去查一下相关的布局方法,然后将计算器布置得漂亮一点。在布置的过程中可以采用画板Panel作为中间组件,先把按钮等组件放置在画板上,再把画板添加到窗口就行了。
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
lb1 = new Label("运算数:");
lb2 = new Label("运算符号:");
lb3 = new Label("结果:");
tf1 = new TextField("");
tf2 = new TextField("");
tf3 = new TextField("");
bt1 = new Button("1");
bt2 = new Button("2");
bt3 = new Button("3");
bt4 = new Button("4");
bt5 = new Button("5");
bt6 = new Button("6");
bt7 = new Button("7");
bt8 = new Button("8");
bt9 = new Button("9");
bt10 = new Button("0");
bt11 = new Button("+");
bt12 = new Button("-");
bt13 = new Button("*");
bt14 = new Button("/");
bt15 = new Button("=");
bt16 = new Button("AC");
FlowLayout fl = new FlowLayout();
fl.setHgap(1000);
fl.setVgap(30);
this.setLayout(fl);
p1.setLayout(new GridLayout(3,2,20,20));
p2.setLayout(new GridLayout(4,4,20,20));
p1.add(lb1);
p1.add(tf1);
p1.add(lb2);
p1.add(tf2);
p1.add(lb3);
p1.add(tf3);
p2.add(bt1);
p2.add(bt2);
p2.add(bt3);
p2.add(bt11);
p2.add(bt4);
p2.add(bt5);
p2.add(bt6);
p2.add(bt12);
p2.add(bt7);
p2.add(bt8);
p2.add(bt9);
p2.add(bt13);
p2.add(bt16);
p2.add(bt10);
p2.add(bt15);
p2.add(bt14);
add(p1);
add(p2);
这是本人的计算器GUI,采用的是GridLayout和FlowLayout并用的方式。
有了整齐的界面,下一步就是要实现功能了。
首先得先做到按不同的按钮显示栏会有不同的结果,如按下数字1,那么显示栏“运算数”会显示1,再按数字2,会显示12,这里需要用到ActionListener,我们要在申明类的时候引用ActionListener接口。
public class Calculator extends Frame implements ActionListener
然后,在按钮上添加ActionListener。
bt1.addActionListener(this);
这一部分最好放到自己申明的类的方法里,不要放到主函数中,不然会有些错误。
public Calculator()
{
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
lb1 = new Label("运算数:");
lb2 = new Label("运算符号:");
lb3 = new Label("结果:");
tf1 = new TextField("");
tf2 = new TextField("");
tf3 = new TextField("");
bt1 = new Button("1");
bt2 = new Button("2");
bt3 = new Button("3");
bt4 = new Button("4");
bt5 = new Button("5");
bt6 = new Button("6");
bt7 = new Button("7");
bt8 = new Button("8");
bt9 = new Button("9");
bt10 = new Button("0");
bt11 = new Button("+");
bt12 = new Button("-");
bt13 = new Button("*");
bt14 = new Button("/");
bt15 = new Button("=");
bt16 = new Button("AC");
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt10.addActionListener(this);
bt11.addActionListener(this);
bt12.addActionListener(this);
bt13.addActionListener(this);
bt14.addActionListener(this);
bt15.addActionListener(this);
bt16.addActionListener(this);
FlowLayout fl = new FlowLayout();
fl.setHgap(1000);
fl.setVgap(30);
this.setLayout(fl);
p1.setLayout(new GridLayout(3,2,20,20));
p2.setLayout(new GridLayout(4,4,20,20));
p1.add(lb1);
p1.add(tf1);
p1.add(lb2);
p1.add(tf2);
p1.add(lb3);
p1.add(tf3);
p2.add(bt1);
p2.add(bt2);
p2.add(bt3);
p2.add(bt11);
p2.add(bt4);
p2.add(bt5);
p2.add(bt6);
p2.add(bt12);
p2.add(bt7);
p2.add(bt8);
p2.add(bt9);
p2.add(bt13);
p2.add(bt16);
p2.add(bt10);
p2.add(bt15);
p2.add(bt14);
add(p1);
add(p2);
}
添加监听器之后,我们需要判断当有按钮按下的时候是哪个按钮,可用方法getSource(),然后根据不同的按钮来做出不同的判断。
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == bt1)
{
tf1.setText(tf1.getText()+bt1.getLabel());
}
if(e.getSource() == bt2)
{
tf1.setText(tf1.getText()+bt2.getLabel());
}
if(e.getSource() == bt3)
{
tf1.setText(tf1.getText()+bt3.getLabel());
}
if(e.getSource() == bt4)
{
tf1.setText(tf1.getText()+bt4.getLabel());
}
if(e.getSource() == bt5)
{
tf1.setText(tf1.getText()+bt5.getLabel());
}
if(e.getSource() == bt6)
{
tf1.setText(tf1.getText()+bt6.getLabel());
}
if(e.getSource() == bt7)
{
tf1.setText(tf1.getText()+bt7.getLabel());
}
if(e.getSource() == bt8)
{
tf1.setText(tf1.getText()+bt8.getLabel());
}
if(e.getSource() == bt9)
{
tf1.setText(tf1.getText()+bt9.getLabel());
}
if(e.getSource() == bt10)
{
tf1.setText(tf1.getText()+bt10.getLabel());
}
if(e.getSource() == bt11)
{
tf2.setText(bt11.getLabel());
a = Integer.parseInt(tf1.getText());
tf1.setText("");
}
if(e.getSource() == bt12)
{
tf2.setText(bt12.getLabel());
a = Integer.parseInt(tf1.getText());
tf1.setText("");
}
if(e.getSource() == bt13)
{
tf2.setText(bt13.getLabel());
a = Integer.parseInt(tf1.getText());
tf1.setText("");
}
if(e.getSource() == bt14)
{
tf2.setText(bt14.getLabel());
a = Integer.parseInt(tf1.getText());
tf1.setText("");
}
if(e.getSource() == bt15)
{
b = Integer.parseInt(tf1.getText());
res = tf2.getText().contains("+");
if(res)
{
c = a + b;
}
res = tf2.getText().contains("-");
if(res)
{
c = a - b;
}
res = tf2.getText().contains("*");
if(res)
{
c = a * b;
}
res = tf2.getText().contains("/");
if(res && b != 0)
{
c = a / b;
}
if(b == 0 && res)
tf3.setText("ERROR!");
else
tf3.setText(String.valueOf(c));
}
if(e.getSource() == bt16)
{
tf1.setText("");
tf2.setText("");
tf3.setText("");
}
}
}
这里面较为关键也是问题出现较多的是数据类型的问题,getText()类型是string,而运算结果类型是int,所以需要进行转换,Integer.parseInt(tf1.getText())是将string转化为int型,String.valueOf©是将int转换为string型,另外需要注意的是,双引号“”表示的是string型,单引号‘’表示的是char型,当我们要判断运算符的时候,直接用双等号==判断可能得不到我们需要的结果,这时候使用contains()来判断是否含有某符号会比较实用。
这样之后,我们就得到了一个可以进行循环简单运算的计算器。
但是我们会发现一个问题——怎么才能关闭这个窗口呢?当我们点击右上角“X”的时候,窗口并没有响应,我们只能在dos窗口按ctrl+c或者任务管理器关闭程序窗口,那我们要做一个可以点击“X”关闭的计算器该怎么做呢?
这就需要用到窗口监视器了。
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
dispose();
System.exit(0);
}
});
在这里用到了接口WindowListener的应用版windowadapter,如果不用windowadapter的话,就需要将windowlistener中的所有方法都列出来,这里只需要列出你需要的——也就是点击“X”时窗口的反应就可以了,也不需要用implements WindowListener。
于是一个完美的计算器就完成了!