在我的JavaFX应用程序中,我需要更新它来处理窗口大小调整。我想做的是,当窗口变大时,保持所有东西都对齐,只是让纹理与窗口成比例,当它变小时类似。
我将设置一个最小和最大高度,以适应右手边的按钮(随着它变小,减小一点间距)。
我发现AnchorPane不是最好的选择,我希望我能得到一个更好的方法的一点指导。我使用的文本区域是自定义的,扩展了StyledTextArea(RichTextFX API)。
最后一个规范是,我希望文本下的文本字段和按钮与文本区域对齐,我认为窗格使文本字段和按钮之间的距离将是一个麻烦的方法(我计划不使其大小可调)。
图片供参考:文字app图片
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<?scenebuilder-stylesheet ../application.css?>
<AnchorPane fx:id="leftPane" minWidth="-1.0" prefHeight="582.0" prefWidth="855.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.TextProController">
<!-- TODO Add Nodes -->
<children>
<VBox alignment="CENTER_LEFT" layoutX="649.0" layoutY="0.0" prefHeight="582.0" prefWidth="205.0" spacing="55.0" styleClass="right-split">
<children>
<Button mnemonicParsing="false" onAction="#handleLoadText" prefHeight="21.0" prefWidth="160.0" text="Load Text">
<font>
<Font size="14.0" fx:id="x1" />
</font>
</Button>
<Button font="$x1" mnemonicParsing="false" onAction="#handleFleschIndex" prefWidth="160.0" text="Flesch Index" />
<Button font="$x1" mnemonicParsing="false" onAction="#handleEditDistance" prefHeight="21.0" prefWidth="160.0" text="Edit Distance" />
<Button mnemonicParsing="false" onAction="#handleMarkovText" prefHeight="25.0" prefWidth="160.0" text="Generate Markov Text">
<font>
<Font size="13.5" />
</font>
</Button>
<CheckBox fx:id="spellingBox" font="$x1" mnemonicParsing="false" onAction="#handleSpelling" text="Spelling Suggestions" />
<CheckBox fx:id="autocompleteBox" font="$x1" mnemonicParsing="false" onAction="#handleAutoComplete" text="AutoComplete" />
</children>
<padding>
<Insets bottom="20.0" left="5.0" />
</padding>
</VBox>
<AnchorPane layoutX="0.0" layoutY="0.0" minHeight="0.0" minWidth="0.0" prefHeight="582.0" prefWidth="650.0" styleClass="left-split">
<children>
<HBox alignment="CENTER_LEFT" layoutX="0.0" layoutY="525.0" prefHeight="32.0" prefWidth="642.0" spacing="15.0">
<children>
<TextField fx:id="fleschField" prefWidth="72.0" />
<Label text="Flesch Index">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="32.0" prefWidth="322.0" />
<Button font="$x1" mnemonicParsing="false" onAction="#handleClear" text="Clear" />
</children>
<padding>
<Insets left="39.0" />
</padding>
</HBox>
</children>
</AnchorPane>
</children>
</AnchorPane>
import java.io.IOException;
import java.util.List;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
public class MainApp extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private LaunchClass launch;
spelling.WordPath wp;
textgen.MarkovTextGenerator mtg;
// called at start of application
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("TextProApp");
//primaryStage.setResizable(false);
try {
// Load root layout from fxml
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
launch = new LaunchClass();
showTextProApp();
}
/**
* Shows the main TextApplication scene
*/
public void showTextProApp() {
try {
// Load the fxml file and set into the center of the main layout
FXMLLoader loader = new FXMLLoader(getClass().getResource("view/TextAppLayout.fxml"));
AnchorPane textProPage = (AnchorPane) loader.load();
rootLayout.setCenter(textProPage);
// Connect controller and main app
TextProController controller = loader.getController();
controller.setMainApp(this);
} catch (IOException e) {
// Exception gets thrown if the fxml file could not be loaded
e.printStackTrace();
}
}
}
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextProController {
private MainApp mainApp;
// UI Controls
private AutoSpellingTextArea textBox;
@FXML
private AnchorPane leftPane;
@FXML
private TextField fleschField;
@FXML
private CheckBox autocompleteBox;
@FXML
private CheckBox spellingBox;
@FXML
private VBox rightBox;
/**
* Initializes the controller class. This method is automatically called
* after the fxml file has been loaded.
*/
@FXML
private void initialize() {
// make field displaying flesch score read-only
fleschField.setEditable(false);
// instantiate and add custom text area
textBox = new AutoSpellingTextArea();
textBox.setPrefSize(570, 492);
textBox.setWrapText(true);
textBox.setLayoutX(40);
textBox.setLayoutY(25);
leftPane.getChildren().add(textBox);
}
/**
* Is called by the main application to give a reference back to itself.
* Also give reference to AutoSpellingTextArea
*
*
* @param mainApp
*/
public void setMainApp(MainApp mainApp) {
this.mainApp = mainApp;
textBox.setMainApp(mainApp);
textBox.setReferences();
}
}
我不知道我是否理解您的问题,如果您想调整窗格
的大小,您可以使用hgrow
属性,该属性控制水平增长,同时使用Anchorpane.left/rightanchor
,该属性将窗格
的左/右侧以所需的距离修复为父侧。试试这个基本的布局。您可以根据您的代码对其进行调整。
<HBox maxHeight="1080.0" maxWidth="1980.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<VBox prefHeight="400.0" prefWidth="402.0" AnchorPane.leftAnchor="0.0" HBox.hgrow="ALWAYS">
<children>
<TextArea prefHeight="341.0" prefWidth="368.0" />
<HBox prefHeight="100.0" prefWidth="200.0" />
</children>
</VBox>
<VBox layoutX="402.0" maxWidth="200.0" prefHeight="400.0" prefWidth="200.0" AnchorPane.rightAnchor="0.0" HBox.hgrow="NEVER">
<children>
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="114.0" text="Button">
<VBox.margin>
<Insets left="50.0" top="50.0" />
</VBox.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="117.0" text="Button">
<VBox.margin>
<Insets left="50.0" top="50.0" />
</VBox.margin>
</Button>
<Button mnemonicParsing="false" prefHeight="25.0" prefWidth="117.0" text="Button">
<VBox.margin>
<Insets left="50.0" top="50.0" />
</VBox.margin>
</Button>
</children>
</VBox>
</children>
</HBox>
我正在尝试构建一个包含6个窗格(作为父级添加到GridPane布局中)的简单Java项目。我必须在开始时设置窗口大小,并通过参考根布局的宽度和高度,设法将它们均匀地拆分。 但我想要他们调整大小,因为我改变了窗口的大小(使用鼠标,现在他们得到固定的大小)。 下面是我的代码:
如何在FXML(JavaFX)中获得用户调整大小后的新窗口大小?我已经搜索了一个“onResizeWindow”方法,但我什么也没有找到。
我的场景中有一个对象。我希望能够调整窗口的大小,并与它一起调整窗格的大小。我还想在窗格旁边放置一个。 到目前为止,我已经成功地生成了一个完全可调整大小的或一个,其中包含和对象,但当我调整窗口大小时,窗格不会随之调整大小。 以下是带有锚烷的FXML: 只有的版本基本上就是版本中使用的代码,但具有和属性。 如何制作一个场景,使其具有可调整大小的和?
然而,当我尝试在具有更大屏幕的工作站上运行时,JavaFX应用程序不会调整自身大小,并显示在屏幕的左上方当笔记本电脑/工作站屏幕大小不同时,我可以调整应用程序窗口的大小?
我想让JFrame上的组件随着窗口的大小调整而调整大小。 这怎么能做到。 当前,当我使JFrame变小时,组件会保留在相同的位置,然后随着框架变小而消失。 理想情况下,我希望设置一个最小的JFrame大小,它不允许表单变小,但当我将其变大时,组件应该随JFrame移动。 补充:如果我想使用绝对布局管理器,该怎么办。这是我老板比较喜欢的布局经理。 代码: 公共类TestFrame扩展JFrame{p
问题内容: 我正在使用Java编写一个简单的绘画程序,并且每当调整JFrame组件的大小时,我都希望调用某种方法。但是我找不到像windowResizedListener之类的任何方法或诸如windowResizedEvent之类的事件。我能做什么?! 问题答案: 实现一个具有: