문제

So I have three buttons, let's say 1, 2 and 3. I want, when I click button1, button2 and button3 to be deselected. After, when I click button2, I want button1 and button3 to be deselected. Same for button3.

Basically, when I click one button, I want the other 2 to be deselected. So far I have the following code but I'm stuck somewhere and I can't see the mistake.

if(smallbuttonpage1.isSelected()){
    normalbuttonpage1.setSelected(false);
    bigbuttonpage1.setSelected(false);
    textpage1.setFont(new Font("ComicSansMS", Font.PLAIN, 8));
}
if(normalbuttonpage1.isSelected()){
    smallbuttonpage1.setSelected(false);
    bigbuttonpage1.setSelected(false);
    textpage1.setFont(new Font("ComicSansMS", Font.PLAIN, 12));
}
if(bigbuttonpage1.isSelected()){
    smallbuttonpage1.setSelected(false);
    normalbuttonpage1.setSelected(false);
    textpage1.setFont(new Font("ComicSansMS", Font.PLAIN, 20));
}
도움이 되었습니까?

해결책

You simply add all buttons to a newly created ButtonGroup. This automatically takes care for deselecting the other buttons.

Have a look into the Swing tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#radiobutton

다른 팁

I figured it out in the end. I used hasFocus() instead of isSelected() and it works just fine, hope it helps anyone in the future.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top