使用多个监视器显示问题JavaFX

时间:2015-07-01 16:50:31

标签: java intellij-idea javafx fxml multiple-monitors

我的应用程序按预期运行,但运行时会出现显示问题。当它运行时,java窗口弹出正确的窗口大小,但窗口内的控件由于某种原因是巨大的。我能够解决这个问题并让它们以正常尺寸显示的唯一方法是,如果我将窗口拖到我的其他显示器之一,放下它(此时超大控件会转移一下),然后将其拖回笔记本电脑显示器。然后控件缩回并正确显示。

但是,如果我将其拖回到我的其他显示器之一,控件会再次爆炸。

我使用的是在MacOSX 10.10.3上运行的IntelliJ 14和Scene Builder 2.0。

FXML文件:

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

<?import javafx.scene.shape.*?>
<?import javafx.scene.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="332.0" prefWidth="404.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="408.0" minWidth="10.0" prefWidth="408.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Button id="btnTest" contentDisplay="CENTER" mnemonicParsing="false" onAction="#foo" prefHeight="74.0" prefWidth="275.0" text="Generate Encryption Key" GridPane.halignment="CENTER">
         <GridPane.margin>
            <Insets />
         </GridPane.margin>
      </Button>
      <Label fx:id="lblTop" alignment="TOP_CENTER" contentDisplay="TOP" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="TOP">
         <GridPane.margin>
            <Insets top="35.0" />
         </GridPane.margin>
      </Label>
      <TextField fx:id="txtKey" alignment="CENTER" editable="false" GridPane.rowIndex="1" />
   </children>
</GridPane>

控制器:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.beans.EventHandler;
import java.util.ResourceBundle;
import java.util.Random;
import java.awt.*;
import java.net.URL;



public class Controller implements Initializable{

    @FXML
    private TextField txtKey;
    @FXML
    private Label lblTop;
    @FXML
    private Button btnButton;

    Random rnd = new Random();
    private char[] pool = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources){
        assert btnButton != null : "fx:id=\"txtKey\" was not injected: check your FXML file 'sample.fxml'.";
    }


        public void foo (ActionEvent actionEvent){
            String key = "";

            for (int i = 0; i < 32; i++){
                key = key + pool[rnd.nextInt(36)];
            }

            lblTop.setText("Your Encryption Key is:");
            txtKey.setText(key);
    }

}

主:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Encryption Key Generator");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

UPADATE:

这个问题似乎是一个IntelliJ问题。我决定尝试构建我的应用程序,在运行JAR之后,它运行得很好。应用程序的逻辑运行,上述显示问题不再发生。

在监视器之间移动时,它确实会稍微调整抖动,但它从不会错误地显示元素。

0 个答案:

没有答案