天天看点

java gui 设计计算器_JavaGUI设计——计算器

java gui 设计计算器_JavaGUI设计——计算器

JavaGUI设计——计算器

Java GUI设计 运用Java Swing技术实现一个简单的“计算器”,界面如下: 以上测试6/3=2.0 主要实现代码如下: package domain; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class calculator extends JFrame implements ActionListener { JFrame frame; private JButton jia = new JButton(“+“); private JButton jian = new JButton(“-“); private JButton cheng = new JButton(“*“); private JButton chu = new JButton(“/“); private JButton qiuyi = new JButton(“%“); private JButton deng = new JButton(“=“); private JButton fu = new JButton(“+/-“); private JButton dian = new JButton(“.“); private JButton kai = new JButton(“sqrt“); private JButton diao = new JButton(“1/x“); private TextField k1 = new TextField(); private objConversion convert = new objConversion(); JMenuItem copy, paste, help, about; JButton backspace, ce, c, num0, num1, num2, num3, num4, num5, num6, num7, num8, num9; Container cp; JTextField text; String copycontent = ““; boolean clickable = true, clear = true; int all = 0; double qian; String fuhao; int first = 1; public calculator() { setTitle(“计算器“); setSize(400, 300); setLocation(250, 200); text = new JTextField(25); text.setText(“0.“); text.setHorizontalAlignment(JTextField.RIGHT);// 从右到左 JPanel cp1 = new JPanel(); JPanel cp2 = new JPanel(); JPanel cp3 = new JPanel(); cp = getContentPane(); cp.add(cp1, “North“); cp.add(cp2, “Center“); cp.add(cp3, “South“); cp1.setLayout(new GridLayout(1, 6)); cp2.setLayout(new GridLayout(1, 3)); cp3.setLayout(new GridLayout(6, 6)); ButtonGroup btg = new ButtonGroup(); JTextField t3 = new JTextField(25); cp1.add(text); text.setEditable(false); text.setBackground(new Color(255, 255, 255)); backspace = new JButton(“Backspace“); backspace.setForeground(new Color(255, 0, 0)); backspace.addActionListener(this); ce = new JButton(“CE“); ce.setForeground(new Color(255, 0, 0)); ce.addActionListener(this); c = new JButton(“C“); c.setForeground(new Color(255, 0, 0)); c.addActionListener(this); k1.setVisible(false); cp2.add(k1); cp2.add(backspace); cp2.add(ce); cp2.add(c); num0 = new JButton(“0“); num1 = new JButton(“1“); num2 = new JButton(“2“); num3 = new JButton(“3“); num4 = new JButton(“4“); num5 = new JButton(“5“); num6 = new JButton(“6“); num7 = new JButton(“7“); num8 = new JButton(“8“); num9 = new JButton(“9“); cp3.add(num7); num7.addActionListener(this); cp3.add(num8); num8.addActionListener(this); cp3.add(num9); num9.addActionListener(this); cp3.add(num6); num6.addActionListener(this); cp3.add(num5); num5.addActionListener(this); cp3.add(num4); num4.addActionListener(this); cp3.add(num3); num3.addActionListener(this); cp3.add(num2); num2.addActionListener(this); cp3.add(num1); num1.addActionListener(this); diao.addActionListener(this); cp3.add(num0); num0.addActionListener(this); cp3.add(chu); chu.setForeground(new Color(255, 0, 0)); chu.addActionListener(this); cp3.add(kai); kai.addActionListener(this); cp3.add(cheng); cheng.setForeground(new Color(255, 0, 0)); cheng.addActionListener(this); cp3.add(qiuyi); qiuyi.addActionListener(this); cp3.add(jian); jian.setForeground(new Color(255, 0, 0)); jian.addActionListener(this); cp3.add(diao); cp3.add(fu); fu.addActionListener(this); cp3.add(dian); dian.addActionListener(this); cp3.add(jia); jia.setForeground(new Color(255, 0, 0)); jia.addActionListener(this); cp3.add(deng); deng.setForeground(new Color(255, 0, 0)); deng.addActionListener(this); JMenuBar mainMenu = new JMenuBar(); setJMenuBar(mainMenu); JMenu editMenu = new JMenu(“编辑“); JMenu helpMenu = new JMenu(“帮助“); mainMenu.add(editMenu); mainMenu.add(helpMenu); copy = new JMenuItem(“ 复制“); paste = new JMenuItem(“ 粘贴“); KeyStroke copyks = KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK); copy.setAccelerator(copyks);// 设置退出菜单选项加上快捷键 KeyStroke pasteks = KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK); paste.setAccelerator(pasteks);// 设置退出菜单选项加上快捷键 editMenu.add(copy); editMenu.add(paste); copy.addActionListener(this); paste.addActionListener(this); help = new JMenuItem(“ 帮助主题“); about = new JMenuItem(“ 关于计算器“); helpMenu.add(help); helpMenu.add(about); help.addActionListener(this); about.addActionListener(this); addWindowListener(new WindowDestroyer());// 结束窗口 } public void actionPered(ActionEvent e) {// 响应动作代码 if (first == 1) text.setText(““); first = 0;// 第一次把文本框0.清空 Object temp = e.getSource(); if (temp == copy) { copycontent = text.getText(); } if (temp == paste) { text.setText(text.getText() + copycontent); } if (temp == help) { // 打开系统帮助文件 要查资料 try { String filePath = “C:/WINDOWS/Help/calc.chm“; Runtime.getRuntime().c(“. /c “ + filePath); } catch (Exception eeee) { System.out.println(“打开系统的计算器出错“); } } if (temp == about) { JOptionPane.showMessageDialog(frame, “ Java计算器\n \n07计算机3班 20072481 刘学龙“, “关于计算器“, JOptionPane.INATION_MESSAGE); } try { String dec = text.getText(); num2.setEnabled(true); num3.setEnabled(true); num4.setEnabled(true); num5.setEnabled(true); num6.setEnabled(true); num7.setEnabled(true); num8.setEnabled(true); num9.setEnabled(true); } catch (Exception ee) { System.out.println(“转换出错,可能你没有输入任何字符“); text.setText(“转换出错“); clear = false; } if (temp == backspace) {// 退格 String s = text.getText(); text.setText(““); for (int i = 0; i 0; i--) { answer += strNum.charAt(i - 1); } return answer; } } public static void main(String arg[])// 产生窗口 { calculator win = new calculator(); win.setVisible(true); } }