java JButton打印一封信
如何打印用户单击的按钮的字母,然后禁用该按钮
我使用for循环生成每个字母的按钮
} for (int i = 65; i <= 90; i++) {
btnLetters = new JButton(" " + (char) i);
letterJPanel.add(btnLetters);
letterJPanel.setLayout(new FlowLayout());
btnLetters.addActionListener(this);
}
单击按钮时,应打印字母,然后禁用按钮
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == btnLetters) {
}
}
# 1 楼答案
这部分只适用于创建的最后一个按钮,因此我认为它是无意义的。
最好做那样的事
其中“打印”是实际打印(无论如何)
# 2 楼答案
创建一个新类
然后将其添加到每个按钮
# 3 楼答案
也许使用内部类会更容易
创建按钮时
# 4 楼答案
首先,我会这样做: (看起来比从整数中转换要好得多)
然后