JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4, 5, 5));
String[] buttons = {
"1", "2", "3", "/", "4",
"5", "6", "*", "7", "8", "9", "-", "0", ".", "=", "+"
};
for (int i = 0; i < buttons.length; i++) {
p.add(new JButton(buttons[i]));
add(p);
此代码产生了很好的计算器布局有没有办法addActionListener方法,每个按钮,而在同一时间保持这种布局 我的意思是不这样做的每每个按钮都是这样。是有办法addActionListener方法,该代码
JButton button1 = new JButton("1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// interesting code for button1 goes here
}
});
也考虑'列表