如何集中JavaFX控件 [英] How do I center JavaFX controls

查看:186
本文介绍了如何集中JavaFX控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更具体地说,为什么我的JavaFX控件没有居中?这是两个截图,第一个刚开始后(我将窗口移动到一个更明显的位置,但还没有调整大小),第二个是在调整它以显示我的问题之后。奖励积分,如果你帮我确保它的大小(在所有DPI上)首次显示:






方便地,相关代码包含在这些屏幕截图中。如果您仍然需要它作为文本,请转到:

  private void initJFXPanel(JFXPanel holder) 
{
{
{
rootGrid = new GridPane();
rootGrid.setAlignment(Pos.CENTER);
rootGrid.setPadding(new Insets(16));
rootGrid.setHgap(16);
rootGrid.setVgap(8);
}

interior = holder.getScene();
if(interior == null)
holder.setScene(interior = new Scene(rootGrid));
interior.setRoot(rootGrid);

}
{
statusLabel = new Label(Checking for Updates ...);
statusLabel.setAlignment(Pos.CENTER);
statusLabel.setTextAlignment(TextAlignment.CENTER);
rootGrid.add(statusLabel,0,0);
}
{
progressBar = new ProgressBar();
progressBar.setProgress(-1);
progressBar.setPrefWidth(Constants.MAX_WIN_BOUNDS.width / 5d); //屏幕宽度的1/5
rootGrid.add(progressBar,0,1);
}
{
downloadButton = new Button(Get it!);
downloadButton.setAlignment(Pos.CENTER);
rootGrid.add(downloadButton,0,2);
}
holder.setMinimumSize(new Dimension((int)(rootGrid.getPrefWidth()+ .5),(int)(rootGrid.getPrefHeight()+ .5)));
setMinimumSize(holder.getMinimumSize());
}


解决方案

解决方案



将控件放在VBox(或其他类似的根布局窗格)和中加载以下内容轻松显示它:

 <?xml version =1.0encoding =UTF-8?> 

<?import javafx.geometry。*?>
<?import javafx.scene.control。*?>
<?import java.lang。*?>
<?import javafx.scene.layout。*?>

< VBox alignment =CENTERmaxHeight = - InfinitymaxWidth = - InfinityminHeight = - InfinityminWidth = - Infinityspacing =8.0xmlns =http: //javafx.com/javafx/8xmlns:fx =http://javafx.com/fxml/1>
< children>
< Label fx:id =updateStatustext =正在检查更新.../>
< ProgressBar fx:id =updateProgressprefWidth =200.0progress =0.0/>
< Button fx:id =updateActionmnemonicParsing =falsetext =Get it! />
< / children>
< padding>
< Insets bottom =16.0left =16.0right =16.0top =16.0/>
< / padding>
< / VBox>


More specifically, why are my JavaFX controls not being centered? Here are two screenshots, the first just after starting (I moved the window into a more visible spot but have not yet resized it), and the second is just after resizing it to show off my problem. Bonus points if you help me ensure it's properly sized (on all DPIs) when it first shows:

Conveniently, the relevant code is included in those screenshots. If you still need it as text, here you go:

private void initJFXPanel(JFXPanel holder)
{
    {
        {
            rootGrid = new GridPane();
            rootGrid.setAlignment(Pos.CENTER);
            rootGrid.setPadding(new Insets(16));
            rootGrid.setHgap(16);
            rootGrid.setVgap(8);
        }

        interior = holder.getScene();
        if (interior == null)
            holder.setScene(interior = new Scene(rootGrid));
        interior.setRoot(rootGrid);

    }
    {
        statusLabel = new Label("Checking for Updates...");
        statusLabel.setAlignment(Pos.CENTER);
        statusLabel.setTextAlignment(TextAlignment.CENTER);
        rootGrid.add(statusLabel, 0, 0);
    }
    {
        progressBar = new ProgressBar();
        progressBar.setProgress(-1);
        progressBar.setPrefWidth(Constants.MAX_WIN_BOUNDS.width / 5d); // 1/5 the width of the screen
        rootGrid.add(progressBar, 0, 1);
    }
    {
        downloadButton = new Button("Get it!");
        downloadButton.setAlignment(Pos.CENTER);
        rootGrid.add(downloadButton, 0, 2);
    }
    holder.setMinimumSize(new Dimension((int)(rootGrid.getPrefWidth() + .5), (int)(rootGrid.getPrefHeight() + .5)));
    setMinimumSize(holder.getMinimumSize());
}

解决方案

Solution

Place your controls in a VBox (or other similar root layout pane) and set the VBox alignment to center.

Layout Advice

This is my personal advice on starting with layout in JavaFX (it's just advice and not applicable to everybody, you can take it or leave it):

  • Your window and all controls and layouts will automatically size to their preferred size.
  • Let the in-built layout managers and layout system do as many calculations for you as possible with you just providing the minimum layout hints necessary to get your result.
  • Stick to using JavaFX only or Swing only code until you are familar with both styles of code development and really need to mix them.
  • Use the SceneBuilder tool to play around with different layouts and get familiar with JavaFX layout mechanisms.
  • Study the JavaFX layout tutorial.
  • Review a presentation on JavaFX layout.
  • To center a stage the screen call stage.centerOnScreen().
  • Consider using the built-in dialog support of Java 8u40 (when it is released).

Hi-dpi support

See the answer to:

FXML based sample code for your dialog

You can load the following up in SceneBuilder to easily display it:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="8.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label fx:id="updateStatus" text="Checking for Updates..." />
      <ProgressBar fx:id="updateProgress" prefWidth="200.0" progress="0.0" />
      <Button fx:id="updateAction" mnemonicParsing="false" text="Get it!" />
   </children>
   <padding>
      <Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
   </padding>
</VBox>

这篇关于如何集中JavaFX控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆