Java语言编写计算器程序 (swing界面)
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame
{
//声明一个文本栏控件,用于显示计算结果
private JTextField txtResult;
private JPanel p;
//定义一个字符数组,将计算器中的文字都放在该数组中
private String name[] = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/"};
//声明一个按钮数组,该数组的长度以字符串数组的长度为准
private JButton button[] = new JButton[name.length];
//定义一个存放计算结果的变量,初始为0
private double result = 0;
//存放最后一个操作符,初始为=
private String lastCommand = "=";
//标识是否开始
private boolean start = true;
public Calculator()
{
super("计算器");
//实例化文本栏控件
txtResult = new JTextField(20);
//设置文本框不是焦点状态
txtResult.setFocusable(false);
//将文本栏控件放置在窗体框架的上方
this.add(txtResult, BorderLayout.NORTH);
//实例化面板对象,同时设置此面板布局为4行4列
p = new JPanel(new GridLayout(4, 4));
//循环实例化按钮
//实例化按钮监听对象
ButtonAction ba = new ButtonAction();
//实例化键盘监听对象
KeyAction ka = new KeyAction();
for(int i=0; i<button.length; i++)
{
button[i] = new JButton(name[i]);
//注册监听
button[i].addActionListener(ba);
button[i].addKeyListener(ka);
p.add(button[i]);
}
this.add(p, BorderLayout.CENTER);
this.setSize(200, 150);
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//计算
public void calculate(double x)
{
if(lastCommand.equals("+"))
{
result += x;
}
else if(lastCommand.equals("-"))
{
result -= x;
}
else if(lastCommand.equals("*"))
{
result *= x;
}
else if(lastCommand.equals("/"))
{
result /= x;
}
else if(lastCommand.equals("="))
{
result = x;
}
//将结果显示在文本栏
txtResult.setText(""+result);
}
//单击按钮监听
privatejavaswing酷炫界面 class ButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input = e.getActionCommand();
if(input.equals("+")||input.equals("-")||input.equals("/")||input.equals("*")||input.equals("="))
{
if(start)
{
if(input.equals("-"))
{
txtResult.setText(input);
start = false;
}
else
{
lastCommand = input;
}
}
else
{
calculate(Double.parseDouble(txtResult.getText()));
lastCommand = input;
start = true;
}
}
else
{
if(start)
{
txtResult.setText("");
start = false;
}
txtResult.setText(txtResult.getText()+input);
}
}
}
//键盘监听
private class KeyAction extends KeyAdapter
{
public void KeyTyped(KeyEvent e)
{
char key = e.getKeyChar();
if(key == '0'||key == '1'||key == '2'||key == '3'||key == '4'||key == '5'||key == '6'||key == '7'||key == '8'||key == '9')
{
if(start)
{
txtResult.setText("");
start = false;
}
txtResult.setText(txtResult.getText()+key);
}
else if(key == '+'||key == '-'||key == '*'||key == '/'||key == '=')
{
if(start)
{
if(key == '-')
{
txtResult.setText(String.valueOf(key));
start = false;
}
else
lastCommand = String.valueOf(key);
}
else
{
calculate(Double.parseDouble(txtResult.getText()));
lastCommand = String.valueOf(key);
start = true;
}
}
}
}
public static void main(String[] args)
{
Calculator f = new Calculator();
f.setVisible(true);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论