初学Java中的Swing,写了一个简单的计算器,和大家分享一下!

25d489a5a1e425b945af5df184f10043.gif

支持键盘和鼠标操作

支持多个数运算。例如:输入1+2+3,结果是6!

源代码:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

class GUI extends JFrame implements ActionListener,KeyListener

{

JTextField textField;

private String strx = "";

private String stry ="";

private String str ="";

private String op="";

private String result="";

private int flag=1;

private String strtemp;

private float x;

private float y;

public GUI()

{

super();

this.setTitle("计算器");

this.setVisible(true);

this.setBounds(800, 400, 280, 250);

this.setResizable(true);

this.addKeyListener(this);

BorderLayout borLayout = new BorderLayout();

this.getContentPane().setLayout(borLayout);

final JPanel viewpanel = new JPanel();

this.getContentPane().add(viewpanel, BorderLayout.NORTH);

textField = new JTextField();

textField.setEditable(false);

textField.setText("0");

textField.setHorizontalAlignment(SwingConstants.RIGHT);

textField.setColumns(20);

viewpanel.add(textField);

final JPanel bottonPanel = new JPanel();

bottonPanel.setSize(200, 200);

GridLayout gri = new GridLayout(4,0);

gri.setHgap(10);

gri.setVgap(10);

bottonPanel.setLayout(gri);

String[][] names ={{"1","2","3","+"},

{"4","5","6","-"},

{"7","8","9","*"},

{".","0","=","/"}

};

JButton[][] buttons = new JButton[4][4];

for(int i=0;i<4;i++)

{

for(int j=0;j<4;j++)

{

buttons[i][j] = new JButton(names[i][j]);

buttons[i][j].setName(names[i][j]);

buttons[i][j].setSize(40, 35);

buttons[i][j].addActionListener(this);

bottonPanel.add(buttons[i][j]);

}

}

this.getContentPane().add(bottonPanel,BorderLayout.CENTER);

final JLabel label1 = new JLabel();

label1.setPreferredSize(new Dimension(10,0));

this.getContentPane().add(label1,BorderLayout.WEST);

final JButton button = new JButton("CE");

button.setName("CE");

button.addActionListener(this);

this.getContentPane().add(button,BorderLayout.EAST);

}

public void calculate()

{

if(str!="")

{

System.out.println("strx == "+strx);

System.out.println("stry == "+stry);

try{

if(strx=="")

{

strx=strtemp;

}

x=Float.parseFloat(strx);

y=Float.parseFloat(stry);

}catch(Exception ex)

{

ex.printStackTrace();

}

if(op=="+")

{

result = (x + y) + "";

}

else if(op=="-")

{

result = (x - y) + "";

}else if(op=="*")

{

result = (x * y) + "";

}else

{

if(y==0)

{

result="ERROR";

}

else{

result = (x / y) + "";

}

}

}

}

private void CaculateTemp()

{

if(op=="*"||op=="+"||op=="-"||op=="/")

{

calculate();

strx=result;

str=result;

stry="";

}

}

private void CE()

{

strx = "";

stry ="";

str ="";

op="";

result="";

flag=1;

textField.setText("0");

}

private void Cacu(String s)

{

if(s.equals("CE"))

{

CE();

} else if(s.equals("="))

{

calculate();

textField.setText(result);

strtemp=result;

str=strtemp;

stry="";

op="";

}

else

{

if(s.equals("+"))

{

if(result!="")

{

str=result;

strx=result;

}

CaculateTemp();

flag=0;

op="+";

}else if(s.equals("-"))

{

if(result!="")

{

str=result;

strx=result;

}

CaculateTemp();

flag=0;

op="-";

}else if(s.equals("*"))

{

if(result!="")

{

str=result;

strx=result;

}

CaculateTemp();

flag=0;

op="*";

}else if(s.equals("/"))

{

if(result!="")

{

str=result;

strx=result;

}

CaculateTemp();

flag=0;

op="/";

}else

{

if(flag==1)

{

strx = strx + s;

System.out.println("strx = "+strx);

}

if(flag==0)

{

stry = stry + s;

System.out.println("stry = "+stry);

}

}

str = str + s;

System.out.println("str = "+str);

textField.setText(str);

}

}

public void actionPerformed(ActionEvent e)

public void keyTyped(KeyEvent e)

public void keyReleased(KeyEvent e)

public void keyPressed(KeyEvent e)

}

public class JSQ {

public static void main(String[] args) {

GUI a = new GUI();

}

}

经过本人多次调试,此程序中仍然存在问题。

1、不能先输入等号

2、不能连续输入多个操作符

3、进行每次运算的时候,都要先清零!

4、运行的时候,出现的窗口要先最小化,再打开,才能显示(不知道为什么)

5、支持键盘和鼠标操作,但是如果一用鼠标操作,就不能再用键盘操作了!(个人觉得,可能监听器加的位置有问题,但又不知道该加到那里可以让键盘,鼠标操作随意切换)

6、可能还有其他问题,所以此处略去n个字!!!!!!

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐