package Demo3;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
public class Caculation extends JFrame {
private JPanel contentPane;
private JTextField jf;
int sum1 = 0, sum2 = 0;
int sign;
boolean key = false;
/**
* @author Mr.wen
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Caculation frame = new Caculation();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Caculation() {
setTitle("\u8BA1\u7B97\u5668");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 226, 317);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
jf = new JTextField();
jf.setText("0");
jf.setFont(new Font("宋体", Font.PLAIN, 28));
jf.setBounds(10, 26, 188, 53);
contentPane.add(jf);
jf.setColumns(10);
JButton bt9 = new JButton("9");
bt9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 9;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 9;
jf.setText(sum2+"");
}
}
});
bt9.setBounds(10, 121, 45, 35);
contentPane.add(bt9);
JButton bt6 = new JButton("6");
bt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 6;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 6;
jf.setText(sum2+"");
}
}
});
bt6.setBounds(10, 157, 45, 35);
contentPane.add(bt6);
JButton bt3 = new JButton("3");
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 3;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 3;
jf.setText(sum2+"");
}
}
});
bt3.setBounds(10, 194, 45, 35);
contentPane.add(bt3);
JButton bt8 = new JButton("8");
bt8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 8;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 8;
jf.setText(sum2+"");
}
}
});
bt8.setBounds(57, 121, 45, 35);
contentPane.add(bt8);
JButton bt7 = new JButton("7");
bt7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 7;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 7;
jf.setText(sum2+"");
}
}
});
bt7.setBounds(105, 121, 45, 35);
contentPane.add(bt7);
JButton bt5 = new JButton("5");
bt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 5;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 5;
jf.setText(sum2+"");
}
}
});
bt5.setBounds(57, 157, 45, 35);
contentPane.add(bt5);
JButton bt2 = new JButton("2");
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 2;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 2;
jf.setText(sum2+"");
}
}
});
bt2.setBounds(57, 194, 45, 35);
contentPane.add(bt2);
JButton bt4 = new JButton("4");
bt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 4;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 4;
jf.setText(sum2+"");
}
}
});
bt4.setBounds(105, 157, 45, 35);
contentPane.add(bt4);
JButton bt1 = new JButton("1");
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) {
sum1 = sum1 * 10 + 1;
jf.setText(sum1+"");
}
else {
sum2 = sum2 * 10 + 1;
jf.setText(sum2+"");
}
}
});
bt1.setBounds(105, 194, 45, 35);
contentPane.add(bt1);
JButton bt0 = new JButton("0");
bt0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(key == false) { //如果没进行加减乘除,则先输入数1
sum1 = sum1 * 10 ;
jf.setText(sum1+"");
}
else { //进行了加减乘数,输入数2
sum2 = sum2 * 10 ;
jf.setText(sum2+"");
}
}
});
bt0.setBounds(10, 235, 92, 35);
contentPane.add(bt0);
JButton bn0 = new JButton("C");
bn0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sum1 = 0;
sum2 = 0;
key = false;
jf.setText("0");
}
});
bn0.setBounds(105, 235, 45, 35);
contentPane.add(bn0);
JButton bn1 = new JButton("+");
bn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
key = true; //代表已进行加操作,下一步将输入数2
sign = 1; //做好标记,取结果是将进行标记判断
}
});
bn1.setBounds(153, 235, 45, 35);
contentPane.add(bn1);
JButton bn2 = new JButton("-");
bn2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
key = true;
sign = 2;
}
});
bn2.setBounds(153, 121, 45, 35);
contentPane.add(bn2);
JButton bn3 = new JButton("*");
bn3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
key = true;
sign = 3;
}
});
bn3.setBounds(153, 157, 45, 35);
contentPane.add(bn3);
JButton bn4 = new JButton("/");
bn4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
key = true;
sign = 4;
}
});
bn4.setBounds(153, 194, 45, 35);
contentPane.add(bn4);
JButton bn5 = new JButton("=");
bn5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(sign == 1) { //如果标记为1,则进行+
sum1 = sum1 + sum2; //重新赋值sum1,方便二次运算
jf.setText(sum1 +"");
sum2 = 0;
key = false;} //运算一次则置为相反
if(sign == 2) {
sum1 = sum1 - sum2;
jf.setText(sum1 +"");
sum2 = 0;
key = false;}
if(sign == 3) {
sum1 = sum1 * sum2;
jf.setText(sum1 +"");
sum2 = 0;
key = false;}
if(sign == 4) {
sum1 = sum1 / sum2;
jf.setText(sum1 +"");
sum2 = 0;
key = false;}
}
});
bn5.setFont(new Font("宋体", Font.PLAIN, 16));
bn5.setBounds(10, 83, 188, 35);
contentPane.add(bn5);
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 208, 23);
contentPane.add(menuBar);
JMenu mnNewMenu = new JMenu("\u67E5\u770B(V)");
menuBar.add(mnNewMenu);
JMenuItem menuItem = new JMenuItem("\u6807\u51C6\u578B");
mnNewMenu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("\u79D1\u5B66\u578B");
mnNewMenu.add(menuItem_1);
JMenuItem mntmNewMenuItem = new JMenuItem("\u5355\u4F4D\u8F6C\u6362");
mnNewMenu.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u5386\u53F2\u8BB0\u5F55");
mnNewMenu.add(mntmNewMenuItem_1);
JMenuItem menuItem_2 = new JMenuItem("\u5DE5\u4F5C\u8868");
mnNewMenu.add(menuItem_2);
JMenu mnNewMenu_1 = new JMenu("\u7F16\u8F91(E)");
menuBar.add(mnNewMenu_1);
JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u590D\u5236");
mnNewMenu_1.add(mntmNewMenuItem_2);
JMenuItem mntmNewMenuItem_3 = new JMenuItem("\u7C98\u8D34");
mnNewMenu_1.add(mntmNewMenuItem_3);
JMenu mnNewMenu_2 = new JMenu("\u5E2E\u52A9(H)");
menuBar.add(mnNewMenu_2);
JMenuItem mntmabout = new JMenuItem("\u5173\u4E8E(About)");
mnNewMenu_2.add(mntmabout);
}
}