我在 Java Eclipse 中引用了这段代码:
public class ClassWithButton extends JFrame{
private final JPanel Your_Panel_name;
public void enableButtons() {
for (Component c : Your_Panel_name.getComponents()) {
if (c instanceof JButton)
c.setEnabled(true);
}
}
}
然后有一个类实现了ActionListener.java
public class ActionListenerImpl implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}
我在扩展 JFrame 的类中有一个按钮,我有一个面板,我在其中禁用了两个按钮。我有另一个扩展 ActionListener 的类,当我按下面板上的另一个按钮时,我希望启用 2 个禁用的按钮,我该怎么做?
最佳答案
根据您提供的代码,这是一个引用如何工作的快速示例,但这不是我实现它的方式。
public class ClassWithButton extends JFrame{
private final JPanel Your_Panel_name;
ActionListenerImpl act;
JButton otherButton;
public ClassWithButton()
{
act = new ActionListenerImpl(this);
otherButton = new JButton("Click to enable");
otherButton.addActionListener(act);
}
public void enableButtons() {
for (Component c : Your_Panel_name.getComponents()) {
if (c instanceof JButton)
c.setEnabled(true);
}
}
}
public class ActionListenerImpl implements ActionListener{
ClassWithButton b;
public ActionListenerImpl(ClassWithButton b)
{
this.b = b;
}
public void actionPerformed(ActionEvent e){
b.enableButtons();
}
}
关于java - 如何在涉及 ActionListener 的另一个类中启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36269602/