如何打印用户点击按钮的字母,然后禁用该按钮
我用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) {
}
}
最佳答案
if (ae.getSource() == btnLetters) { } }
这部分只适用于最后创建的按钮,所以我认为它毫无意义。
最好做那样的事情
if (ae.getSource() instance of JButton &&
((JButton ) ae.getSource()).getText().length()==2) {
PRINT(((JButton ) ae.getSource()).getText().substring(1));
((JButton ) ae.getSource()).setEnabled(false);
}
其中 PRINT 是实际打印(无论您如何打印)
关于java - JButton打印一封信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12259020/