【发布时间】:2014-04-09 23:44:31
【问题描述】:
我的目标是向控制台显示一条消息,显示我按下了哪个按钮(按钮从 1 到 6)。这是我得到的最远距离。
代码:
public class excercise5_1 extends JFrame {
public excercise5_1() {
setLayout(new FlowLayout());
// Create two panels
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
// Add three buttons to each panel
panel1.add(new JButton(" Button 1 "));
panel1.add(new JButton(" Button 2 "));
panel1.add(new JButton(" Button 3 "));
panel2.add(new JButton(" Button 4 "));
panel2.add(new JButton(" Button 5 "));
panel2.add(new JButton(" Button 6 "));
// Add panels to frame
add(panel1);
add(panel2);
}
public static void main(String[] args) {
excercise5_1 frame = new excercise5_1();
frame.setTitle(" Exercise 12_1 ");
frame.setSize(600,75);
frame.setLocationRelativeTo(null); // center frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
如果我按下它,我只是不知道该怎么做才能让它显示“按钮 2”。
【问题讨论】:
标签: java swing jpanel jbutton actionlistener