import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** * GUI计算器 * @author Administrator * */ public class Calculater { static float sum = 0f; private JFrame frame; private JTextField textField; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Calculater window = new Calculater(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public Calculater() { initialize(); } /** * Initialize the contents of the frame. */ private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 730, 619); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); JButton btnNewButton = new JButton("1"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setText(textField.getText()+1); } }); btnNewButton.setBounds(70, 265, 50, 26); frame.getContentPane().add(btnNewButton); JButton button = new JButton("2"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+2); } }); button.setBounds(144, 265, 50, 26); frame.getContentPane().add(button); JButton button_1 = new JButton("3"); button_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+3); } }); button_1.setBounds(221, 265, 50, 26); frame.getContentPane().add(button_1); JButton button_2 = new JButton("4"); button_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+4); } }); button_2.setBounds(70, 304, 50, 26); frame.getContentPane().add(button_2); JButton button_3 = new JButton("5"); button_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+5); } }); button_3.setBounds(144, 304, 50, 26); frame.getContentPane().add(button_3); JButton button_4 = new JButton("6"); button_4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+6); } }); button_4.setBounds(221, 304, 50, 26); frame.getContentPane().add(button_4); JButton button_5 = new JButton("7"); button_5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+7); } }); button_5.setBounds(70, 339, 50, 26); frame.getContentPane().add(button_5); JButton button_6 = new JButton("8"); button_6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+8); } }); button_6.setBounds(144, 339, 50, 26); frame.getContentPane().add(button_6); JButton button_7 = new JButton("9"); button_7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+9); } }); button_7.setBounds(221, 339, 50, 26); frame.getContentPane().add(button_7); //加法/// JButton button_8 = new JButton("+"); button_8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+" + "); //加 String str = textField.getText(); String [] stringArr= str.split((" ")); if(stringArr.length == 2) { float i = Float.valueOf(stringArr[0]); sum = i; } else if(stringArr[stringArr.length-3].equals("+")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum+i; } else if(stringArr[stringArr.length-3].equals("-")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum-i; } else if(stringArr[stringArr.length-3].equals("*")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum*i; } else if(stringArr[stringArr.length-3].equals("/")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum/i; } } }); button_8.setBounds(293, 265, 50, 26); frame.getContentPane().add(button_8); //减法// JButton button_9 = new JButton("-"); button_9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+" - "); //减 String str = textField.getText(); String [] stringArr= str.split((" ")); if(stringArr.length == 2) { float i = Float.valueOf(stringArr[0]); sum = i; } else if(stringArr[stringArr.length-3].equals("+")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum+i; } else if(stringArr[stringArr.length-3].equals("-")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum-i; } else if(stringArr[stringArr.length-3].equals("*")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum*i; } else if(stringArr[stringArr.length-3].equals("/")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum/i; } } }); button_9.setBounds(293, 304, 50, 26); frame.getContentPane().add(button_9); //乘法// JButton button_10 = new JButton("*"); button_10.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+" * "); String str = textField.getText(); String [] stringArr= str.split((" ")); if(stringArr.length == 2) { float i = Float.valueOf(stringArr[0]); sum = i; } else if(stringArr[stringArr.length-3].equals("+")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum+i; } else if(stringArr[stringArr.length-3].equals("-")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum-i; } else if(stringArr[stringArr.length-3].equals("*")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum*i; } else if(stringArr[stringArr.length-3].equals("/")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum/i; } } }); button_10.setBounds(293, 339, 50, 26); frame.getContentPane().add(button_10); JButton button_11 = new JButton("0"); button_11.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+0); } }); button_11.setBounds(144, 380, 50, 26); frame.getContentPane().add(button_11); //除法// JButton button_12 = new JButton("/"); button_12.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+" / "); String str = textField.getText(); String [] stringArr= str.split((" ")); if(stringArr.length == 2) { float i = Float.valueOf(stringArr[0]); sum = i; } else if(stringArr[stringArr.length-3].equals("+")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum+i; } else if(stringArr[stringArr.length-3].equals("-")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum-i; } else if(stringArr[stringArr.length-3].equals("*")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum*i; } else if(stringArr[stringArr.length-3].equals("/")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum/i; } } }); button_12.setBounds(293, 380, 50, 26); frame.getContentPane().add(button_12); JButton button_13 = new JButton("="); button_13.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(textField.getText()+" = "); String str = textField.getText(); String [] stringArr= str.split((" ")); if(stringArr.length == 2) { float i = Float.valueOf(stringArr[0]); sum = i; textField.setText(textField.getText()+sum); } else if(stringArr[stringArr.length-3].equals("+")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum+i; textField.setText(textField.getText()+sum); } else if(stringArr[stringArr.length-3].equals("-")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum-i; textField.setText(textField.getText()+sum); } else if(stringArr[stringArr.length-3].equals("*")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum*i; textField.setText(textField.getText()+sum); } else if(stringArr[stringArr.length-3].equals("/")) { float i = Float.valueOf(stringArr[stringArr.length-2]); sum = sum/i; textField.setText(textField.getText()+sum); } } }); button_13.setBounds(364, 265, 50, 26); frame.getContentPane().add(button_13); textField = new JTextField(); textField.setBounds(70, 148, 344, 79); frame.getContentPane().add(textField); textField.setColumns(10); JButton btnC = new JButton("C"); btnC.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textField.setText(""); sum = 0; } }); btnC.setBounds(364, 304, 50, 26); frame.getContentPane().add(btnC); JButton button_14 = new JButton("."); button_14.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { textField.setText(textField.getText()+"."); } }); button_14.setBounds(364, 341, 50, 26); frame.getContentPane().add(button_14); } }
运行后是这个样子的,只能进行加减乘除运算(多位数、连续加减乘除。。)