我有一个 JPanel,其中包含一个带有 ActionListener 的 JButton。点击一定次数后,需要移除 JButton。但是,当我在其上调用 remove() 时,我得到一个不会消失的按下按钮。
这是我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class SetuPanel extends JPanel {
private JTextField user = new JTextField();
private JLabel label = new JLabel();
private JButton submit = new JButton("Submit");
private int phase = 0;
public SetuPanel() {
super(new FlowLayout());
user.setPreferredSize(new Dimension(120,22));
submit.addActionListener(new Submit());
label.setText("Enter Desired Username:");
add(label);
add(user);
add(submit);
}
private void remov(JComponent jc) {
remove(jc);
validate();
}
private class Submit implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println(user.getText());
if (phase == 2) {
phase++;
label.setText("Playing!");
remov(submit);
}
}
}
}
有任何想法吗?