2014-01-18 44 views
0

如何通過按下一個按鈕來禁用按鈕,以及當按下的按鈕被分配給的任務已完成時,我希望啓用所有按鈕。例如;如何在java,eclipse中啓用/禁用按鈕?

UpButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     DownButton.setEnabled(false); 
     LeftButton.setEnabled(false); 
     RightButton.setEnabled(false); 
     System.out.printline("Up Button"); 

    } 
}); 
DownButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     UpButton.setEnabled(false); 
     LeftButton.setEnabled(false); 
     RightButton.setEnabled(false); 
     System.out.printline("Down Button"); 


    } 
}); 
LeftButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     DownButton.setEnabled(false); 
     UpButton.setEnabled(false); 
     RightButton.setEnabled(false); 
     System.out.printline("Left Button"); 

    } 
}); 
RightButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     DownButton.setEnabled(false); 
     LeftButton.setEnabled(false); 
     UpButton.setEnabled(false); 
     System.out.printline("Right Button"); 

    } 
}); 

我試過這樣做,但它不起作用。我想要的是,例如,我的Upbutton正在打印(「向上按鈕」),所以當用戶按下此按鈕時,我希望所有其他按鈕都被禁用,直到它完成了它應該執行的命令爲止,在這種情況下打印出一個文本,但稍後我會添加諸如添加兩個用戶輸入的內容我會有一個按鈕,要求用戶鍵入數字1然後數字2,然後計算機將它們添加並在此過程中,我希望所有的按鈕被禁用,期望已被點擊的按鈕,直到用戶已經給出了所有的數字,並且計算機已經給出了輸出。

我希望我已經正確地解釋了自己,如果不是,請讓我知道,我會盡我所能提供更多信息。提前致謝。

+0

只是一個問題,請這個問題是關於.... – mKorbel

+0

點後不可能使用代碼標籤(從來沒有動手,也許有人這樣做與HTML格式) – mKorbel

+1

使用JRadioButtons添加到ButtonGroup,很少費力可以通過使用JToggleButtons – mKorbel

回答

0

這似乎是正確的。也許你想他們看不見的,而不是殘疾人...

在這種情況下:

LeftButton.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 

DownButton.setVisible(false); 
UpButton.setVisible(false); 
RightButton.setVisible(false); 
System.out.printline("Left Button"); 

Here's的作品的東西的例子:

private void jButtonChangeChampActionPerformed(java.awt.event.ActionEvent evt) {             
    jComboBoxPlayer.setEnabled(true); 
    jComboBoxChampion.setEnabled(true); 
    jButtonSelecionar.setVisible(true); 
    jButtonMagias.setVisible(false); 
} 
+0

嗨,拉斐爾與我的方法DownButton.setEnabled(假);和你的方法DownButton.setVisible(false);它表示按鈕無法解析。我希望按鈕是可見的,但如果按下一個按鈕則禁用。 –

+0

是否聲明瞭按鈕(private javax.swing.JButton UpButton;)?你有沒有導入javax.swing.JButton?可能是這樣吧? –

0

如果你試試這個:

UpButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

    UpButtonActionPerformed(evt); 

     } 
    }); 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    DownButton.setEnabled(false); 
    LeftButton.setEnabled(false); 
    RightButton.setEnabled(false); 
    System.out.printline("Up Button"); 
} 

它在這裏工作...

+0

它工作? ... –

0

如果要設置禁用按鈕,可以使用

btnExample.setDisable(true); //這將禁用按鈕

btnBreak.setVisible(false); //這將使按鈕消失僅視覺。

希望這能幫到你們。

0

如何使用Command對象?至少每個命令對象可能有​​和canExecute()方法。然後;

btnA.setEnabled(cmdA.canExecute()); 
btnA.addActionListener(event-> { 
    cmdA.execute(); 
    btnB.setEnabled(cmdA.canExecute()); 
}); 
0

您必須先禁用其他按鈕,完成您的任務,然後再次啓用按鈕。

例如:

UpButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 

     // first disable all other buttons 
     DownButton.setEnabled(false); 
     LeftButton.setEnabled(false); 
     RightButton.setEnabled(false); 

     System.out.println("Up Button"); 
     // do rest of your tasks here. 

     // now enable them again 
     DownButton.setEnabled(true); 
     LeftButton.setEnabled(true); 
     RightButton.setEnabled(true); 

    } 

}); 

類似地,設置偵聽器的其他按鈕。


希望這有助於!