The button click event code is as follows, there are three buttons
If (buttonA. GetStyleClass (.) indexOf (" button - unselected2 ")!=1) { ButtonA. GetStyleClass (). Remove (buttonA getStyleClass (). The indexOf (" button - unselected2 ")); } ButtonA. GetStyleClass (). The add (" button - selected2 "); If (buttonB. GetStyleClass (.) indexOf (" button - selected2 ")!=1) { ButtonB. GetStyleClass (). Remove (buttonB getStyleClass (). The indexOf (" button - selected2 ")); ButtonB. GetStyleClass (). The add (" button - unselected2 "); } If (buttonC. GetStyleClass (.) indexOf (" button - selected2 ")!=1) { ButtonC. GetStyleClass (). Remove (buttonC getStyleClass (). The indexOf (" button - selected2 ")); ButtonC. GetStyleClass (). The add (" button - unselected2 "); } Started with the add directly to three buttons to add corresponding style, but after clicking on a few times more, can appear all button into the style of the button - unselected2,,,,
Interviews after the first remove the add, is that, behind the output under getStyleClass () found around 3-4 times, can appear
The button button - unselected2 button - unselected2 button - selected2
This kind of situation
CodePudding user response:
Now if the first if solved for a while, but feel this is not the normal way,,,,
CodePudding user response:
The
2020 response: refer to the original poster Cykapunk button click event code is as follows, there are three buttons Started with the add directly to three buttons to add corresponding style, but after clicking on a few times more, can appear all button into the style of the button - unselected2,,,, Interviews after the first remove the add, is that, behind the output under getStyleClass () found around 3-4 times, can appear The button button - unselected2 button - unselected2 button - selected2 This kind of circumstance Java is not have any radio button! JRadioButton understand CodePudding user response:
refer to the second floor qq_39936465 response: Quote: refer to the original poster Cykapunk 2020 response: The button click event code is as follows, there are three buttons Started with the add directly to three buttons to add corresponding style, but after clicking on a few times more, can appear all button into the style of the button - unselected2,,,, Interviews after the first remove the add, is that, behind the output under getStyleClass () found around 3-4 times, can appear The button button - unselected2 button - unselected2 button - selected2 This kind of circumstance Java is not have any radio button! JRadioButton understand I know a radiobutton, but in front of the circle can't get rid of too ugly,,, CodePudding user response:
The original poster you want ToggleButton and ToggleGroup, here is a example, besides the original poster had better find another article is familiar with, import deployment headaches. Application. The application; The import deployment headaches. Geometry. Insets; The import deployment headaches. The scene. The scene; The import deployment headaches. Scene. Control. ToggleButton; The import deployment headaches. Scene. Control. ToggleGroup; The import deployment headaches. Scene. Layout. HBox; The import deployment headaches. Stage. Stage; Public class StyleButtonDemo extends Application { Public static void main (String [] args) { Launch (args); } @ Override Public void start (Stage primaryStage) throws the Exception { PrimaryStage. SetScene (new Scene (root ())); PrimaryStage. The show (); } Private HBox root () { HBox HBox=new HBox (); HBox. SetPadding (new Insets (50)); HBox. SetSpacing (10); ToggleGroup group=new ToggleGroup (); for (int i=0; i <5; I++) { ToggleButton button=new ToggleButton (" button "+ I); Button. SetToggleGroup (group); HBox. GetChildren (). The add (button). } Group. SelectedToggleProperty (.) addListener ((observables, oldValue, newValue) - & gt; { If (newValue instanceof ToggleButton) { System. The out. Println (" you chose "+ ((ToggleButton) newValue). GetText ()); } }); Return hBox; } } CodePudding user response:
reference 4 floor fabricated beliefs response: the original poster is ToggleButton and ToggleGroup you want, here is a example, besides the original poster had better find another article is familiar with, import deployment headaches. Application. The application; The import deployment headaches. Geometry. Insets; The import deployment headaches. The scene. The scene; The import deployment headaches. Scene. Control. ToggleButton; The import deployment headaches. Scene. Control. ToggleGroup; The import deployment headaches. Scene. Layout. HBox; The import deployment headaches. Stage. Stage; Public class StyleButtonDemo extends Application { Public static void main (String [] args) { Launch (args); } @ Override Public void start (Stage primaryStage) throws the Exception { PrimaryStage. SetScene (new Scene (root ())); PrimaryStage. The show (); } Private HBox root () { HBox HBox=new HBox (); HBox. SetPadding (new Insets (50)); HBox. SetSpacing (10); ToggleGroup group=new ToggleGroup (); for (int i=0; i <5; I++) { ToggleButton button=new ToggleButton (" button "+ I); Button. SetToggleGroup (group); HBox. GetChildren (). The add (button). } Group. SelectedToggleProperty (.) addListener ((observables, oldValue, newValue) - & gt; { If (newValue instanceof ToggleButton) { System. The out. Println (" you chose "+ ((ToggleButton) newValue). GetText ()); } }); Return hBox; } } null