And*_*raz 0 java swing jbutton actionlistener
我正在用java构建一个程序,我想知道是否有任何函数可以在按下时删除一个JButton列表?
这是我到目前为止:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button[0]) {
for(int x = 0; x < 19; x++) {
button[x].remove(this);
}
}
}
Run Code Online (Sandbox Code Playgroud)
ActionListener已经配置,它工作正常.提前感谢谁给了我一个解决方案.
您当前的代码看起来是想删除的东西,你的this,不管它代表,从JButton的,这是bassackwards.
要告诉的关键信息是 - 从什么中移除按钮?如果是JPanel,那么你必须这样做,调用remove(...)包含JPanel,传入你想要删除的组件(JButton).
即
public void actionPerformed(ActionEvent e) {
containingJPanel.remove((AbstractButton) e.getSource());
}
Run Code Online (Sandbox Code Playgroud)
具体的代码解决方案将取决于您当前程序的结构.