【问题标题】:JavaFX : how to make a button with two (2) or more icons?JavaFX:如何制作带有两 (2) 个或更多图标的按钮?
【发布时间】:2015-01-12 12:53:05
【问题描述】:

有很多关于如何将图形添加到 JavaFX 按钮的在线资源。

但是我想知道是否有办法添加第二张(或任意数量,但多于 2 张图片在大多数情况下可能没有多大意义)图片。

我的用例:我有一个左侧带有方形图标的按钮,后跟一个文本标签。该图像是按钮所链接的一些现实生活概念的表示(可以是汽车或人)。我想在一些按钮的右侧添加一个小图标,一个“右 V 形”来指示交互的性质。

我在想也许可以使用全宽的 HBox 作为按钮的图形节点,并将 2 个图像添加到其中,但我认为不可能将文本放在图形节点的顶部.

有什么想法吗?

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    使用图标和文本创建您自己的自定义节点并将其设置为图形。当然,您不会显示按钮文本,因为它已经在您的自定义图形节点中。

    这是一个简单的例子。您需要提供文件 icon1.png 和 icon2.png。

    public class ButtonWithMultipleIcons extends Application {
        @Override
        public void start(Stage primaryStage) {
            try {
    
                Group group = new Group();
    
                Button button = new Button();
                button.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    
                HBox hBox = new HBox();
    
                ImageView icon1 = new ImageView( getClass().getResource( "icon1.png").toExternalForm());
                ImageView icon2 = new ImageView( getClass().getResource( "icon2.png").toExternalForm());
    
                Label label = new Label("Text");
    
                //make the button grow if you want the right icon to always be on the right of the button :
                label.setMaxWidth(Long.MAX_VALUE);
    
                HBox.setHgrow(label, Priority.ALWAYS);
    
    
                hBox.getChildren().addAll( icon1, label, icon2);
    
                button.setGraphic(hBox);
    
                group.getChildren().add( button);
    
                Scene scene = new Scene(group,400,400);
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

    • 这就是要走的路。我编辑了答案并添加了代码以使标签变大,以便正确的图标始终位于按钮的右端。
    【解决方案2】:

    您可以尝试在按钮中使用 html 格式,并在文本前后添加带有标签的图像。

    您可以在此处找到有关它的更多信息:http://docs.oracle.com/javase/tutorial/uiswing/components/html.html

    【讨论】:

    • 是的,那是 Swing 而不是 JavaFX。
    【解决方案3】:

    我也有同样的事情要相处。您使用 HBox 描述解决方案。我认为这也是最好的方式。

    执行以下操作: 使用您自己的特定布局创建一个新的 CustomButton,可能使用 HBox 并从 JavaFX 扩展 Button 类。按照您的需要设置样式,您可以在自定义按钮中添加任意数量的图像。也许它看起来像这样:

    public class CustomButton extends Button
    {
       private HBox hbox;
       private ImageView image1;
       private ImageView image2;
    
       public CustomButton()
       {
          super();
          // Here add your specific images to global or local variables and then add all the children    (images) to your HBox layout and this one to the button.
       }
    }
    

    问题在于,JavaFX 提供的是普通按钮,而不是深度定制的组件。如果要设置样式,无论如何都可以使用 CSS。

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 2019-07-03
      • 2023-01-12
      • 1970-01-01
      相关资源
      最近更新 更多