1 import java.awt.Color; 2 import java.awt.Container; 3 import java.awt.Font; 4 import java.awt.GridBagConstraints; 5 import java.awt.GridBagLayout; 6 import java.awt.Insets; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import javax.swing.*; 10 11 public class Desktop extends JFrame { 12 private Container container = this.getContentPane(); 13 private GridBagLayout gridBagLayout = new GridBagLayout(); 14 private GridBagConstraints gridBagConstraints = new GridBagConstraints(); 15 private JTextField displayField; 16 private String lastCommand = "="; 17 private double result = 0.0d; 18 private boolean start = true; 19 20 public Desktop() { 21 super("JAVA图形化界面计算器"); 22 container.setLayout(gridBagLayout); 23 initDisplayArea(); 24 initButtons(); 25 initFrame(); 26 } 27 28 private void initFrame() { 29 setSize(900, 900); 30 setVisible(true); 31 setResizable(false); 32 setForeground(Color.MAGENTA); 33 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 34 } 35 36 private void initButtons() { 37 ActionListener insert = new Desktop.InsertAction(); 38 ActionListener command = new Desktop.CommandAction(); 39 addButton("Back", 0, 1, 2, 1, insert); 40 addButton("清零", 2, 1, 2, 1, insert); 41 addButton("7", 0, 2, 1, 1, insert); 42 addButton("8", 1, 2, 1, 1, insert); 43 addButton("9", 2, 2, 1, 1, insert); 44 addButton("+", 3, 2, 1, 1, command); 45 addButton("4", 0, 3, 1, 1, insert); 46 addButton("5", 1, 3, 1, 1, insert); 47 addButton("6", 2, 3, 1, 1, insert); 48 addButton("-", 3, 3, 1, 1, command); 49 addButton("1", 0, 4, 1, 1, insert); 50 addButton("2", 1, 4, 1, 1, insert); 51 addButton("3", 2, 4, 1, 1, insert); 52 addButton("*", 3, 4, 1, 1, command); 53 addButton("0", 0, 5, 1, 1, insert); 54 addButton("-", 1, 5, 1, 1, insert); 55 addButton(".", 2, 5, 1, 1, insert); 56 addButton("/", 3, 5, 1, 1, command); 57 addButton("=", 0, 6, 4, 1, command); 58 } 59 60 private void initDisplayArea() { 61 displayField = new JTextField(20); 62 displayField.setHorizontalAlignment(4); 63 displayField.setBackground(Color.gray); 64 displayField.setDisabledTextColor(Color.MAGENTA); 65 displayField.setEnabled(false); 66 displayField.setFont(new Font("宋体", Font.BOLD, 50)); 67 gridBagConstraints.gridx = 0; 68 gridBagConstraints.gridy = 0; 69 gridBagConstraints.gridwidth = 4; 70 gridBagConstraints.gridheight = 1; 71 gridBagConstraints.fill = 1; 72 gridBagConstraints.weightx = 1; 73 gridBagConstraints.weighty = 1; 74 gridBagLayout.setConstraints(displayField, gridBagConstraints); 75 container.add(displayField); 76 } 77 78 private void addButton(String label, int column, int row, int with, int height, ActionListener listener) { 79 JButton button = new JButton(label); 80 button.setForeground(Color.MAGENTA); 81 button.setFont(new Font("楷体", Font.BOLD, 36)); 82 button.addActionListener(listener); 83 gridBagConstraints.gridx = column; 84 gridBagConstraints.gridy = row; 85 gridBagConstraints.gridwidth = with; 86 gridBagConstraints.gridheight = height; 87 gridBagConstraints.fill = GridBagConstraints.BOTH; 88 gridBagConstraints.insets = new Insets(1, 1, 1, 1); 89 gridBagLayout.setConstraints(button, gridBagConstraints); 90 container.add(button); 91 } 92 93 public void calculate(double x) { 94 if (this.lastCommand.equals("+")) { 95 this.result += x; 96 } else if (this.lastCommand.equals("-")) { 97 this.result -= x; 98 } else if (this.lastCommand.equals("*")) { 99 this.result *= x; 100 } else if (this.lastCommand.equals("/")) { 101 this.result /= x; 102 } else if (this.lastCommand.equals("=")) { 103 this.result = x; 104 } 105 106 this.displayField.setText("" + this.result); 107 } 108 109 public static void main(String[] args) { 110 new Desktop(); 111 } 112 113 private class CommandAction implements ActionListener { 114 private CommandAction() { 115 } 116 117 public void actionPerformed(ActionEvent evt) { 118 String command = evt.getActionCommand(); 119 if (Desktop.this.start) { 120 Desktop.this.lastCommand = command; 121 } else { 122 Desktop.this.calculate(Double.parseDouble(Desktop.this.displayField.getText())); 123 Desktop.this.lastCommand = command; 124 Desktop.this.start = true; 125 } 126 } 127 } 128 129 private class InsertAction implements ActionListener { 130 private InsertAction() { 131 } 132 133 public void actionPerformed(ActionEvent event) { 134 String input = event.getActionCommand(); 135 if (Desktop.this.start) { 136 Desktop.this.displayField.setText(""); 137 Desktop.this.start = false; 138 if (input.equals("-")) { 139 Desktop.this.displayField.setText(Desktop.this.displayField.getText() + "-"); 140 } 141 } 142 143 if (!input.equals("-")) { 144 if (input.equals("Back")) { 145 String str = Desktop.this.displayField.getText(); 146 if (str.length() > 0) { 147 Desktop.this.displayField.setText(str.substring(0, str.length() - 1)); 148 } 149 } else if (input.equals("清零")) { 150 Desktop.this.displayField.setText("0"); 151 result = 0.0d; 152 Desktop.this.start = true; 153 } else { 154 if (input.equals(".")) { 155 if (Desktop.this.displayField.getText().trim().indexOf(".") == -1) { 156 Desktop.this.displayField.setText(Desktop.this.displayField.getText() + input); 157 } 158 } else { 159 Desktop.this.displayField.setText(Desktop.this.displayField.getText() + input); 160 } 161 } 162 } 163 164 } 165 } 166 }