加上上面的NumberAction
public class CalculatorPanel extends JPanel {
private static final long serialVersionUID = 188604265139101232L;
public CalculatorPanel(final JTextField input) {
super(new GridLayout(4, 3, 5, 5));
for (int i = 0; i < 10; i++) {
add(new JButton(new NumberAction(Integer.toString(i), input)));
}
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame("Calculator");
final JTextField input = new JTextField();
frame.add(new CalculatorPanel(input));
frame.add(input, BorderLayout.PAGE_START);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}