-1
我試圖設置一個按下的按鈕不可見,並設置其餘可見。我用getComponents()
方法來獲得三個按鈕,並改變其可見性狀態,但出現問題。組件陣列()方法可以被改變
@Override
public void actionPerformed(ActionEvent e) {
Component button = ((Component) e.getSource());
for (Component component : view.getComponents()) {
if (component instanceof JButton) {
if (component.getName().equals(button.getName())) {
System.out.format("Pressed button: %s%n", button.getName());
component.setVisible(false);
} else {
component.setVisible(true);
}
}
}
java.awt.Toolkit.getDefaultToolkit().beep();
view.revalidate();
for (Component component : view.getComponents()) {
System.out.format("%s is visible: %s%n", component.getName(), component.isVisible());
}
}
我用format()
方法來獲取組件的可見性狀態。啓動後程序按鈕不會消失,但狀態是正確的。
如果我使用button
變量來設置它的可見性狀態時,它成功運行,但我不能改變按鈕可見性狀態的其餘部分。
對不起。我不是那個意思。有三個按鈕,只有一個可能不可見。 – Bruno