【发布时间】:2012-10-24 11:37:51
【问题描述】:
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);
这段代码产生了一个很好的计算器布局有没有办法在每个按钮上添加ActionListener,同时保持这个布局 我的意思不是像这样对每个按钮都这样做。
JButton button1 = new JButton("1");
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// interesting code for button1 goes here
}
});
【问题讨论】:
标签: java swing awt jbutton actionlistener