(一个文本框) (+) (一个文本框) (=) (结果)
(+)(-)
(×)(/)
(重置)
下面的运算符全部是按钮,上面那个是文本框,默认是+,按下面的按钮就变成相应的运算符
两个文本框打数字,=号是按钮,点击出结果
还要有报错功能,就是第二个文本框出现0和负数要对话框报错!
package Cal;import java.awt.*;
import javax.swing.*;import java.awt.event.*;
/**
* @author hyvin
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/class calculation extends JFrame {
public calculation() /* 构造方法 */
{
super(\"计数器\");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JFrame.setDefaultLookAndFeelDecorated(true);
initTextPanel(); /* 文本框 */
initControlPanel(); /* 控制键 */
initKeyPanel(); /* 数字和运算符 */
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(TextPanel, BorderLayout.NORTH);
pane.add(ControlPanel, BorderLayout.CENTER);
pane.add(KeyPanel, BorderLayout.SOUTH);
pack();
try {
UIManager
.setLookAndFeel(\"com.sun.java.swing.plaf.windows.WindowsLookAndFeel\");
} catch (Exception ex) {
;
} /* 设置Windons观感 */
SwingUtilities.updateComponentTreeUI(this);
setResizable(false);
setVisible(true);
}private void initTextPanel() /* 设置文本框 */
{
TextPanel = new JPanel();
TextPanel.setLayout(new FlowLayout());
Resultarea = new JTextField(\"0\", 25);
Resultarea.setEditable(false); /* 设置不可编辑 */
Color color = Color.white;
Resultarea.setBackground(color); /* 颜色 */
Resultarea.setHorizontalAlignment(JTextField.RIGHT); /* 设置显示位置 */
TextPanel.add(Resultarea);
}private void initControlPanel() /* 设置控制键 */
{
ControlPanel = new JPanel();
ControlPanel.setLayout(new GridLayout(1, 3, 4, 4));
JButton b1 = new JButton(\"Backspace\"); /* Backspace键 */
b1.setFont(font1);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = Resultarea.getText();
int l = s1.length();
Resultarea.setText(s1.substring(0, l - 1));
}
});ControlPanel.add(b1);
JButton b2 = new JButton(\"CE\"); /* CE键 */
b2.setFont(font1);
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Resultarea.setText(\"0\");
isNew = true;
}
});
ControlPanel.add(b2);JButton b3 = new JButton(\"C\"); /* C键 */
b3.setFont(font1);
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Resultarea.setText(\"0\");
pnum = \"\";
operation = \"\";
isNew = true;
}
});
ControlPanel.add(b3);
}private void initKeyPanel() /* 设置数字键和运算符键 */
{
String key[] = { \"7\", \"8\", \"9\", \"/\", \"sqrt\", \"4\", \"5\", \"6\", \"*\", \"%\",
\"1\", \"2\", \"3\", \"-\", \"1/x\", \"0\", \"+/-\", \".\", \"+\", \"=\" };
KeyPanel = new JPanel();
KeyPanel.setLayout(new GridLayout(4, 5, 4, 4));
for (int i = 0; i < 20; i++) {
String label = key[i];
JButton b = new JButton(label);
b.setActionCommand(key[i]);
b.setFont(font2);
KeyPanel.add(b);
b.addActionListener(new ActionListener() /* 无名监听器 */
{
public void actionPerformed(ActionEvent e) {
Key_actionPerformed(e);
}
});
}
}public void Key_actionPerformed(ActionEvent e) /* 数字键和运算符键无名监听器 */
{
String s = (e.getActionCommand());
String st = Resultarea.getText();
if (s.equals(\"0\")) /* 输入数为0 */
{
if (st.equals(\"0\"))
return;
else {
if (!isNew)
Resultarea.setText(st + \"0\");
else
Resultarea.setText(\"0\");
}
isNew = false;
return;
}if (s.equals(\"+/-\")) /* 输入数为+/- */
{
double k = Double.parseDouble(st);
{
if (k != 0)
display(-k);}
return;
}if (s.equals(\"1\") || s.equals(\"2\") || s.equals(\"3\") || s.equals(\"4\")
|| s.equals(\"5\") || s.equals(\"6\") || s.equals(\"7\")
|| s.equals(\"8\") || s.equals(\"9\")) /* 输入1-9 */
{
if (!isNew)
Resultarea.setText(st + s);
else {
Resultarea.setText(s);
isNew = false;
}
return;
}if (s.equals(\".\")) /* 输入小数点 */
{
if (Resultarea.getText().indexOf(\".\") == -1) /* 不存在小数点 */
{
if (isNew) {
Resultarea.setText(\"0.\");
isNew = false;
} else
Resultarea.setText(st + \".\");
}
return;
}isNew = true; /* 按下运算符,输入新的数 */
if (s.equals(\"=\")) {
compute(s);
operation = \"\";
} else {
if (s.equals(\"+\") || s.equals(\"-\") || s.equals(\"*\")
|| s.equals(\"/\")) /* 二目运算符号 */
{
if (operation.equals(\"\")) {
pnum = Resultarea.getText();
operation = s;
} else
compute(s);
} else /* 一目运算 */
{
Count count1 = new Count(Double.parseDouble(st));
if (s.equals(\"sqrt\")) {
display(count1.sqrt());} else {
if (s.equals(\"1/x\")) {
if (st.equals(\"0\")) {
Resultarea.setText(\"除数不能为0.\");
operation = \"\";
pnum = \"\";
} else
display(count1.reciprocal()); /* 求倒数 */
} else
display(Double.parseDouble(st) / 100); /* 输入%,使当前显示的值除于100 */
}
}
}
}private void compute(String s) {
if (s.equals(\"=\")) {
if (operation.equals(\"\"))
return;
}
double data1 = Double.parseDouble(pnum);
double data2 = Double.parseDouble(Resultarea.getText());
Count count2 = new Count(data1, data2); /* 加减乘除计算 */
if (operation.equals(\"+\"))
display((count2.plus()));
else {
if (operation.equals(\"-\"))
display((count2.minus()));
else {
if (operation.equals(\"*\"))
display((count2.multiply()));
else if (operation.equals(\"/\")) {
if (data2 == 0) {
Resultarea.setText(\"除数不能为0\");
operation = \"\";
pnum = \"\";
return;
} else
display((count2.divide()));
}
}
}
operation = \"\"; /* 符号为当前符 */
pnum = Resultarea.getText();/* 运算数为当前文本数 */
}public void display(double result) /* 显示输出方法 */
{
int a = (int) result;
if (a == result)
Resultarea.setText(String.valueOf(a));
else
Resultarea.setText(String.valueOf(result));
if (Resultarea.getText().equals(\"NaN\"))
Resultarea.setText(\"输入函数无效\");
}private JPanel TextPanel; /* 文本框棉板 */
private JTextField Resultarea; /* 文本框 */
private JPanel ControlPanel; /* 控制键键面板 */
private JPanel KeyPanel; /* 数字键和运算符键面板 */
private Font font1 = new Font(\"Dialog\", 0, 10); /* 控制键字体 */
private Font font2 = new Font(\"Dialog\", 0, 10); /* 数字键和符号键字体 */
private String pnum = \"\"; /* 前操作数 */
private boolean isNew = true; /* 控制是否是新数 */
private String operation = \"\"; /* 运算符 */
//}
class Count {/**
* 单目计算方法类,这是一个内部类.
*/
public Count(double d) {// TODO Auto-generated constructor stub
}/**
* @return
*/
public Count(double data1, double data2) {// TODO Auto-generated constructor stub
}/**
* @return
*/
public double divide() {
// 除法
double data1 = Double.parseDouble(pnum);
double data2 = Double.parseDouble(Resultarea.getText());
return data1 / data2;
}/**
* @return
*/
public double multiply() {
// 乘法
double data1 = Double.parseDouble(pnum);
double data2 = Double.parseDouble(Resultarea.getText());
return data1 * data2;
}/**
* @return
*/
public double minus() {
// 减法
double data1 = Double.parseDouble(pnum);
double data2 = Double.parseDouble(Resultarea.getText());
return data1 - data2;
}/**
* @return
*/
public double plus() {
// 加法
double data1 = Double.parseDouble(pnum);
double data2 = Double.parseDouble(Resultarea.getText());
return data1 + data2;
}public double reciprocal() {
// 求倒数
String st = Resultarea.getText();
return 1 / Double.parseDouble(st);
}/**
* @return
*/
public double sqrt() {
// 开方
String st = Resultarea.getText();
return Math.sqrt(Double.parseDouble(st));
}
};public static void main(String[] args) { /*驱动方法*/
new calculation();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Calculation extends JPanel
{
private JTextField inputTxt1,inputTxt2,resultTxt,handleTxt;
//2个输入文本框,1个显示结果文本框和显示操作符文本框
private JButton plusBtn,minusBtn,multiplyBtn,divisionBtn,amountBtn;
//加 减 乘 除 等于 5个按钮
private JPanel p1,p2;
public Calculation()
{
setLayout(new GridLayout(2,1));
p1 = TextPanel();
p2 = HandlePanel();
}
public JPanel TextPanel()
{
JPanel p = new JPanel();
inputTxt1 = new JTextField(20);
inputTxt2 = new JTextField(20);
resultTxt = new JTextField(20);
resultTxt.setEditable(false);
handleTxt = new JTextField(\"+\",5);
handleTxt.setHorizontalAlignment(JTextField.CENTER);
handleTxt.setEditable(false);
amountBtn = new JButton(\"=\");
p.add(inputTxt1);
p.add(handleTxt);
p.add(inputTxt2);
p.add(amountBtn);
p.add(resultTxt);
add(p);
amountBtn.addActionListener(new ButtonAction());
return p;
}
public JPanel HandlePanel()
{
JPanel p = new JPanel();
ButtonAction baction = new ButtonAction();
plusBtn = new JButton(\"+\");
plusBtn.setActionCommand(\"+\");
minusBtn = new JButton(\"-\");
minusBtn.setActionCommand(\"-\");
multiplyBtn = new JButton(\"*\");
multiplyBtn.setActionCommand(\"*\");
divisionBtn = new JButton(\"/\");
divisionBtn.setActionCommand(\"/\");
p.add(plusBtn);
p.add(minusBtn);
p.add(multiplyBtn);
p.add(divisionBtn);
add(p);
plusBtn.addActionListener(baction);
minusBtn.addActionListener(baction);
multiplyBtn.addActionListener(baction);
divisionBtn.addActionListener(baction);
return p;
}
class ButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o == amountBtn)
{
double no1,no2;
String handle = handleTxt.getText();
try{
no1 = Double.valueOf(inputTxt1.getText());
no2 = Double.valueOf(inputTxt2.getText());
}
catch(NumberFormatException numbere)
{
JOptionPane.showMessageDialog(null,\"输入的数字有误,请重新输入\"
,\"Input Error\",JOptionPane.ERROR_MESSAGE);
return;
}
if(no1 < 1 || no2 < 0)
{
JOptionPane.showMessageDialog(null,\"输入了负数或除数为零,请重新输入\"
,\"Input Error\",JOptionPane.ERROR_MESSAGE);
return;
}
if(handle.equals(\"+\"))
{
double temp = no1 + no2;
resultTxt.setText(String.valueOf(temp));
}
else if(handle.equals(\"-\"))
{
double temp = no1 - no2;
resultTxt.setText(String.valueOf(temp));
}
else if(handle.equals(\"*\"))
{
double temp = no1 * no2;
resultTxt.setText(String.valueOf(temp));
}
else if(handle.equals(\"/\"))
{
double temp = no1 / no2;
resultTxt.setText(String.valueOf(temp));
}
}
else if(o == plusBtn)
{
handleTxt.setText(plusBtn.getActionCommand());
}
else if(o == minusBtn)
{
handleTxt.setText(minusBtn.getActionCommand());
}
else if(o == multiplyBtn)
{
handleTxt.setText(multiplyBtn.getActionCommand());
}
else if(o == divisionBtn)
{
handleTxt.setText(divisionBtn.getActionCommand());
}
}
}
public static void main(String [] args)
{
JFrame f = new JFrame(\"Calculation\");
f.getContentPane().add(new Calculation());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}