日期:2014-05-20 浏览次数:20695 次
import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Mycalculator extends JFrame { private String[] names = { "退格", "CE", "C", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "0", "+/-", ".", "=" }; private JButton[] jb = new JButton[names.length]; private double x = 0d; private double y = 0d; private int z;// 作为运算符的选择 StringBuffer str; JPanel p; JPanel p1; JTextField jt; MenuItem mi; public Mycalculator() { super("Followme_1987"); MenuBar mb = new MenuBar();// 设置菜单栏 setMenuBar(mb); Menu me = new Menu("编辑"); Menu me1 = new Menu("关于"); Menu me2 = new Menu("帮助"); mi = new MenuItem("请点我"); mi.addActionListener(new Mc()); me1.add(mi); mb.add(me); mb.add(me1); mb.add(me2); jt = new JTextField(19);// 创建文本框 用于显示 jt.setHorizontalAlignment(JTextField.RIGHT); jt.setText("0."); jt.setEditable(false); p = new JPanel(); // 创建一块板儿 并将文本框加入 p.setBounds(8, 2, 220, 30); p.setLayout(new FlowLayout(FlowLayout.LEFT)); p.add(jt); p1 = new JPanel(); // 创建另一块儿板儿 p1.setLayout(new GridLayout(5, 4, 5, 5)); p1.setBounds(13, 35, 210, 140); jt.setBackground(Color.getHSBColor(23, 25, 125)); for (int i = 0; i < jb.length; i++) { // 加入按钮 jb[i] = new JButton(names[i]); jb[i].setMargin(new Insets(4, 1, 4, 1)); p1.add(jb[i]); if (i == 1 || i == 2 || i == 0) { jb[i].setForeground(Color.BLUE); } else if (i == 3 || i == 7 || i == 11 || i == 15) { jb[i].setForeground(Color.RED); } else if (i == 19) { jb[i].setForeground(Color.YELLOW); jb[i].setBackground(Color.RED); } else { jb[i].setBackground(Color.DARK_GRAY); jb[i].setForeground(Color.YELLOW); } jb[i].addActionListener(new Mc()); }// 以上颜色带给人 轻松的感觉 避免视觉疲劳 setLayout(null); add(p1); add(p); str = new StringBuffer(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Mycalculator frame = new Mycalculator(); frame.setBounds(400, 200, 240, 230); // frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); } public class Mc implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == mi) { JOptionPane.showMessageDialog(null, "楼主要记得结贴......"); } if (e.getSource() == jb[0]) { // 按下退格键 if (!jt.getText().trim().equals("0.")) { if (str.length() != 1) { jt.setText(str.delete(str.length() - 1, str.length()) .toString()); } else { jt.setText("0."); str.setLength(0); } y = Double.parseDouble(jt.getText().trim()); } } else if (e.getSource() == jb[1] || e.getSource() == jb[2]) { // 按下CE键或者C键 jt.setText("0."); str.setLength(0); x = 0d; y = 0d; z = 0; } else if (e.getSource() == jb[3]) { // 按下除号键 并且设置z=1 x = Double.parseDouble(jt.getText().trim()); str.setLength(0); y = 0d; z = 1; } else if (e.getSource() == jb[7]) { // 按下乘号键并且设置z=2 x = Double.parseDouble(jt.getText().trim()); str.setLength(0); y = 0d; z = 2; } else if (e.getSource() == jb[11]) { // 按下减号键并且设置z=3 x = Double.parseDouble(jt.getText().trim()); str.setLength(0); y = 0d; z = 3; } else if (e.getSource() == jb[15]) { // 按下加号键并且设置z=4 x = Double.parseDouble(jt.getText().trim()); str.setLength(0); y = 0d; z = 4; } else if (e.getSource() == jb[19]) { // 按下=号键 str.setLength(0); switch (z) { case 1: jt.setText("" + (x / y)); break; case 2: jt.setText("" + (x * y)); break; case 3: jt.setText("" + (x - y)); break; case 4: jt.setText("" + (x + y)); break; } } else if (e.getSource() == jb[17]) { // 按下+/- if (jt.getText().trim().equals("0.")) { } else { // x = Double.parseDouble(jt.getText().trim()); // jt.setText("" + (-x)); if (jt.getText().trim().startsWith("-")) { jt.setText(jt.getText().trim().substring(1)); } else { jt.setText("-" + jt.getText().trim()); } } } else if (e.getSource() == jb[18]) { // 按下小数点 if (jt.getText().trim().indexOf(".") != -1) {// 字符串中有小数点 if (jt.getText().trim().equals("0.")) { // 为初始状态 str.setLength(0); jt.setText(str.append("0.").toString()); } else { } } else { jt.setText(str.append(e.getActionCommand()).toString()); } y = 0d; } else if (e.getSource() == jb[16]) {// 按下0键 if (!jt.getText().trim().equals("0.")) { jt.setText(str.append(e.getActionCommand()).toString()); y = Double.parseDouble(e.getActionCommand().trim()); } else { } } else {// 按下其他数字键 jt.setText(str.append(e.getActionCommand()).toString()); y = Double.parseDouble(jt.getText().trim()); } } } }