1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| package Test;
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
public class MainWindow extends JFrame {
int firstnum; int secondnum; char func; boolean isFirstnum=true; TextField textField;
MainWindow(String title) { super(title); initializeWindow(); initializeControls(); this.setVisible(true); }
private void initializeWindow() { this.setSize(400,200); this.setLocation(400,400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
private void initializeControls() { JButton[] buttons=new JButton[10]; JButton[] order = new JButton[16]; JPanel mainjp = new JPanel(new BorderLayout()); JPanel buttonjp = new JPanel(new GridLayout(4, 4)); textField = new TextField(); textField.setText(""); mainjp.add(textField, BorderLayout.NORTH); for (int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(Integer.toString(i)); buttons[i].addActionListener(new NumEventListener()); } for (int i = 0; i < buttons.length; i++) order[i] = buttons[buttons.length - i - 1]; JButton addition = new JButton("+"); addition.addActionListener(new FuncEventListener()); order = insert(order, addition, 3); JButton subtraction = new JButton("-"); subtraction.addActionListener(new FuncEventListener()); order = insert(order, subtraction, 7); JButton multiplication = new JButton("*"); multiplication.addActionListener(new FuncEventListener()); order = insert(order, multiplication, 11); JButton clear = new JButton("AC"); clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { firstnum=0; secondnum=0; isFirstnum=true; textField.setText(""); } }); order = insert(order, clear, 13); JButton equal = new JButton("="); equal.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { switch (func) { case '+' :textField.setText(String.valueOf(firstnum+secondnum));break; case '-' :textField.setText(String.valueOf(firstnum-secondnum));break; case '*' :textField.setText(String.valueOf(firstnum*secondnum));break; case '/' :textField.setText(String.valueOf(firstnum/secondnum));break; } isFirstnum=true; } }); order = insert(order, equal, 14); JButton division = new JButton("/"); division.addActionListener(new FuncEventListener()); order = insert(order, division, 15); for (JButton button : order) buttonjp.add(button); mainjp.add(buttonjp, BorderLayout.CENTER); this.add(mainjp); }
private JButton[] insert(JButton[] order, JButton newbutton, int index) { for (int i = order.length - 1; i > index; i--) { order[i] = order[i - 1]; } order[index] = newbutton; return order; }
class NumEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JButton button=(JButton)e.getSource(); textField.setText(textField.getText() + button.getText()); if (isFirstnum) firstnum = Integer.parseInt(textField.getText()); else secondnum = Integer.parseInt(textField.getText()); } }
class FuncEventListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JButton button=(JButton)e.getSource(); func=button.getText().charAt(0); isFirstnum=false; firstnum=Integer.parseInt(textField.getText()); textField.setText(""); } } }
|