英文:
How to align graphic and text to opposite sides on a RadioButton
问题
如何左对齐这些按钮的图标并右对齐文本(Java8)?
<dependencies>
<dependency>
<groupId>de.jensd</groupId>
<artifactId>fontawesomefx-materialdesignfont</artifactId>
<version>1.7.22-4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>de.jensd</groupId>
<artifactId>fontawesomefx-commons</artifactId>
<version>8.15</version>
</dependency>
</dependencies>
应该更像这样:
[![enter image description here][2]][2]
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
RadioButton memberButton = new RadioButton("Members");
memberButton.setGraphic(new MaterialDesignIconView(MaterialDesignIcon.WORKER));
RadioButton companyButton = new RadioButton("Companies");
companyButton.setGraphic(new MaterialDesignIconView(MaterialDesignIcon.CITY));
RadioButton settingsButton = new RadioButton("Settings");
settingsButton.setGraphic(new MaterialDesignIconView(MaterialDesignIcon.WRENCH));
stylizeAndSetButtons(memberButton, companyButton, settingsButton);
VBox root = new VBox();
root.getChildren().addAll(memberButton, companyButton, settingsButton);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private static void stylizeAndSetButtons(RadioButton... buttons) {
ToggleGroup group = new ToggleGroup();
for (RadioButton button : buttons) {
button.setPrefWidth(150);
button.setMinHeight(50);
button.getStyleClass().remove("radio-button");
button.getStyleClass().add("toggle-button");
button.getStyleClass().add("default");
button.setToggleGroup(group);
}
}
}
<details>
<summary>英文:</summary>
How do I left align the icon graphic and right align the text of these buttons (Java8)?
<dependencies>
<dependency>
<groupId>de.jensd</groupId>
<artifactId>fontawesomefx-materialdesignfont</artifactId>
<version>1.7.22-4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>de.jensd</groupId>
<artifactId>fontawesomefx-commons</artifactId>
<version>8.15</version>
</dependency>
</dependencies>
[![enter image description here][1]][1]
Should be more like this:
[![enter image description here][2]][2]
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Hello World!");
RadioButton memberButton = new RadioButton("Members");
memberButton.setGraphic(new MaterialDesignIconView(MaterialDesignIcon.WORKER));
RadioButton companyButton = new RadioButton("Companies");
companyButton.setGraphic(new MaterialDesignIconView(MaterialDesignIcon.CITY));
RadioButton settingsButton = new RadioButton("Settings");
settingsButton.setGraphic(new MaterialDesignIconView(MaterialDesignIcon.WRENCH));
stylizeAndSetButtons(memberButton, companyButton, settingsButton);
VBox root = new VBox();
root.getChildren().addAll(memberButton, companyButton, settingsButton);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private static void stylizeAndSetButtons(RadioButton... buttons)
{
ToggleGroup group = new ToggleGroup();
for (RadioButton button : buttons)
{
button.setPrefWidth(150);
button.setMinHeight(50);
button.getStyleClass().remove("radio-button");
button.getStyleClass().add("toggle-button");
button.getStyleClass().add("default");
button.setToggleGroup(group);
}
}
}
[1]: https://i.stack.imgur.com/vUMeE.png
[2]: https://i.stack.imgur.com/SUrDz.png
</details>
# 答案1
**得分**: 3
这是实现你想要的方法之一。
不要使用默认的 `Label`。使用一个包含你的图标、一个占位面板以及一个新的 `Label` 的 `HBox`。
```java
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.kordamp.ikonli.javafx.FontIcon;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
RadioButton companyButton = new RadioButton();
FontIcon icon = new FontIcon("bi-alarm");
Pane placeHolder = new Pane();
Label label = new Label("Company");
placeHolder.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(placeHolder, Priority.ALWAYS);
HBox hbox = new HBox(icon, placeHolder, label);
hbox.setAlignment(Pos.CENTER);
companyButton.setGraphic(hbox);
stylizeAndSetButtons(companyButton);
VBox root = new VBox();
root.getChildren().addAll(companyButton);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private static void stylizeAndSetButtons(RadioButton... buttons) {
ToggleGroup group = new ToggleGroup();
for (RadioButton button : buttons) {
button.setPrefWidth(150);
button.setMinHeight(50);
button.getStyleClass().remove("radio-button");
button.getStyleClass().add("toggle-button");
button.getStyleClass().add("default");
button.setToggleGroup(group);
}
}
}
请注意,这里使用了 Ikonli
,但思想应该是相同的。
模块信息:
module sed.home.testonesimplefx {
requires javafx.controls;
requires org.kordamp.ikonli.core;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.ikonli.bootstrapicons;
exports sed.home.testonesimplefx;
}
依赖项:
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>12.3.1</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-bootstrapicons-pack</artifactId>
<version>12.3.1</version>
</dependency>
注意:这里使用了 Ikonli
,但思想应该是相同的。
英文:
Here is one way to do what you want.
Don't use the default Label
. Use an HBox
with your icon, a spaceholder pane, and a new Label
.
Main
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.kordamp.ikonli.javafx.FontIcon;
public class App extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Hello World!");
RadioButton companyButton = new RadioButton();
FontIcon icon = new FontIcon("bi-alarm");
Pane placeHolder = new Pane();
Label label = new Label("Company");
placeHolder.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(placeHolder, Priority.ALWAYS);
HBox hbox = new HBox(icon, placeHolder, label);
hbox.setAlignment(Pos.CENTER);
companyButton.setGraphic(hbox);
stylizeAndSetButtons( companyButton);
VBox root = new VBox();
root.getChildren().addAll( companyButton);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private static void stylizeAndSetButtons(RadioButton... buttons)
{
ToggleGroup group = new ToggleGroup();
for (RadioButton button : buttons)
{
button.setPrefWidth(150);
button.setMinHeight(50);
button.getStyleClass().remove("radio-button");
button.getStyleClass().add("toggle-button");
button.getStyleClass().add("default");
button.setToggleGroup(group);
}
}
}
Module-Info
module sed.home.testonesimplefx {
requires javafx.controls;
requires org.kordamp.ikonli.core;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.ikonli.bootstrapicons;
exports sed.home.testonesimplefx;
}
Dependencies
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>12.3.1</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-bootstrapicons-pack</artifactId>
<version>12.3.1</version>
</dependency>
Output
Note: This uses Ikonli
but the ideas should be the same.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论