|
| 1 | +import counter.ScientificCalculator; |
| 2 | +import counter.StandardCalculator; |
| 3 | + |
| 4 | +import javax.swing.JButton; |
| 5 | +import javax.swing.UnsupportedLookAndFeelException; |
| 6 | + |
| 7 | +import java.awt.Font; |
| 8 | +import java.awt.event.ActionEvent; |
| 9 | +import java.awt.event.ActionListener; |
| 10 | + |
| 11 | +public class Calculator implements ActionListener |
| 12 | +{ |
| 13 | + StandardCalculator standard_calculator = new StandardCalculator();//标准计算器 |
| 14 | + ScientificCalculator scientific_calculator = new ScientificCalculator();//科学计算器 |
| 15 | + |
| 16 | + private final JButton toggle_button_scientific = new JButton();//切换科学计算 |
| 17 | + private final JButton toggle_button_standard = new JButton();//切换标准计算器 |
| 18 | + |
| 19 | + //窗口坐标 |
| 20 | + private int x; |
| 21 | + private int y; |
| 22 | + private final int w = standard_calculator.getWidth(); |
| 23 | + private final int h = standard_calculator.getHeight(); |
| 24 | + |
| 25 | + Calculator() throws |
| 26 | + UnsupportedLookAndFeelException, |
| 27 | + ClassNotFoundException, |
| 28 | + InstantiationException, |
| 29 | + IllegalAccessException { |
| 30 | + inItUi(); |
| 31 | + } |
| 32 | + |
| 33 | + private void inItUi() { |
| 34 | + toggle_button_scientific.setBounds(13, 0, 54, 30); |
| 35 | + toggle_button_scientific.setFocusPainted(false); |
| 36 | + toggle_button_scientific.setFocusable(false); |
| 37 | + toggle_button_scientific.setFont(new Font("微软雅黑", Font.PLAIN, 11)); |
| 38 | + toggle_button_scientific.setText("科学"); |
| 39 | + toggle_button_scientific.addActionListener(this); |
| 40 | + standard_calculator.getContentPane().add(toggle_button_scientific); |
| 41 | + |
| 42 | + toggle_button_standard.setBounds(13, 0, 54, 30); |
| 43 | + toggle_button_standard.setFocusPainted(false); |
| 44 | + toggle_button_standard.setFocusable(false); |
| 45 | + toggle_button_standard.setFont(new Font("微软雅黑", Font.PLAIN, 11)); |
| 46 | + toggle_button_standard.setText("标准"); |
| 47 | + toggle_button_standard.addActionListener(this); |
| 48 | + scientific_calculator.getContentPane().add(toggle_button_standard); |
| 49 | + } |
| 50 | + |
| 51 | + private void recordCoordinates(int x_, int y_) { |
| 52 | + x = x_; |
| 53 | + y = y_; |
| 54 | + } |
| 55 | + |
| 56 | + public void exec(){ |
| 57 | + standard_calculator.exec(); |
| 58 | + scientific_calculator.exec(); |
| 59 | + recordCoordinates(standard_calculator.getX(), standard_calculator.getY()); |
| 60 | + scientific_calculator.setIsHidden(true); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void actionPerformed(ActionEvent e) { |
| 65 | + String button_text = ((JButton) e.getSource()).getText(); |
| 66 | + switch (button_text) { |
| 67 | + case "科学": |
| 68 | + recordCoordinates(standard_calculator.getX(), standard_calculator.getY()); |
| 69 | + standard_calculator.setIsHidden(true); |
| 70 | + scientific_calculator.setBounds(x, y, w, h); |
| 71 | + scientific_calculator.setIsHidden(false); |
| 72 | + break; |
| 73 | + case "标准": |
| 74 | + recordCoordinates(scientific_calculator.getX(), scientific_calculator.getY()); |
| 75 | + scientific_calculator.setIsHidden(true); |
| 76 | + standard_calculator.setBounds(x, y, w, h); |
| 77 | + standard_calculator.setIsHidden(false); |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments