2014-01-18 3 views
0

하나의 버튼을 눌러 버튼을 비활성화 할 수 있고 눌려진 버튼이 지정된 작업이 완료되면 모든 버튼을 활성화 할 수 있습니다. 예를 들면;자바, 이클립스에서 버튼을 사용/사용 중지하려면 어떻게해야하나요?

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 (false); 및 메서드 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 개체를 사용하는 것은 어떻습니까? 각 명령 개체의 메서드는 적어도 execute()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); 

    } 

}); 

, 다른 설정 버튼을위한 수신기.


희망이 있습니다.

관련 문제