如何在RadioButton上将图形和文本对齐到相对的两侧

huangapple go评论71阅读模式
英文:

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)?
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;de.jensd&lt;/groupId&gt;
&lt;artifactId&gt;fontawesomefx-materialdesignfont&lt;/artifactId&gt;
&lt;version&gt;1.7.22-4&lt;/version&gt;
&lt;type&gt;jar&lt;/type&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;de.jensd&lt;/groupId&gt;
&lt;artifactId&gt;fontawesomefx-commons&lt;/artifactId&gt;
&lt;version&gt;8.15&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
[![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(&quot;Hello World!&quot;);
RadioButton memberButton = new RadioButton(&quot;Members&quot;);
memberButton.setGraphic(new MaterialDesignIconView(MaterialDesignIcon.WORKER));
RadioButton companyButton = new RadioButton(&quot;Companies&quot;);
companyButton.setGraphic(new MaterialDesignIconView(MaterialDesignIcon.CITY));
RadioButton settingsButton = new RadioButton(&quot;Settings&quot;);
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(&quot;radio-button&quot;);
button.getStyleClass().add(&quot;toggle-button&quot;);
button.getStyleClass().add(&quot;default&quot;);
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(&quot;Hello World!&quot;);
RadioButton companyButton = new RadioButton();        
FontIcon icon = new FontIcon(&quot;bi-alarm&quot;);
Pane placeHolder = new Pane();
Label label = new Label(&quot;Company&quot;);
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(&quot;radio-button&quot;);
button.getStyleClass().add(&quot;toggle-button&quot;);
button.getStyleClass().add(&quot;default&quot;);
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

        &lt;dependency&gt;
&lt;groupId&gt;org.kordamp.ikonli&lt;/groupId&gt;
&lt;artifactId&gt;ikonli-javafx&lt;/artifactId&gt;
&lt;version&gt;12.3.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.kordamp.ikonli&lt;/groupId&gt;
&lt;artifactId&gt;ikonli-bootstrapicons-pack&lt;/artifactId&gt;
&lt;version&gt;12.3.1&lt;/version&gt;
&lt;/dependency&gt;

Output

如何在RadioButton上将图形和文本对齐到相对的两侧

Note: This uses Ikonli but the ideas should be the same.

huangapple
  • 本文由 发表于 2023年7月13日 21:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定