我设法解决了一段时间的代码玩弄后的问题。我所做的是使用Scene Builder创建GUI,并使用控制器将其与Java代码链接起来。 下面是FXML文件:
tingiGUI.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="202.0" prefWidth="325.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ting.TingiCont">
<children><Button fx:id="si" layoutX="137.0" layoutY="112.0" mnemonicParsing="false" onAction="#Onyesha" text="Show" /><Label fx:id="lbi" layoutX="100.0" layoutY="70.0" prefHeight="17.0" prefWidth="124.0" />
</children></Pane>
tinGUI.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="325.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ting.TinCont">
<children><Button fx:id="bb" layoutX="137.0" layoutY="88.0" mnemonicParsing="false" onAction="#Boom" text="BOOM" /><Label fx:id="lbx" layoutX="84.0" layoutY="147.0" prefHeight="17.0" prefWidth="157.0" />
</children></Pane>
tingGUI.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="219.0" prefWidth="323.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ting.TingCont">
<children><Button fx:id="cmb" layoutX="127.0" layoutY="126.0" mnemonicParsing="false" onAction="#clicked" text="Click me..." /><Label fx:id="lb" layoutX="93.0" layoutY="80.0" prefHeight="17.0" prefWidth="121.0" />
</children></AnchorPane>
下面是控制器。他们扩展了主要的java类。
TinCont.java
package ting;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import jfxtras.labs.scene.control.window.CloseIcon;
import jfxtras.labs.scene.control.window.Window;
public class TinCont extends Main implements Initializable{
@FXML
private Button bb;
@FXML
private Label lbx;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
void Boom(ActionEvent event) throws IOException {
lbx.setText("KAMIKAZE!!!!!!");
Pane coco = FXMLLoader.load(getClass().getResource("tingiGUI.fxml"));
Window x = new Window("TINGI WINDOW");
// set the window position to 10,10 (coordinates inside canvas)
x.setLayoutX(10);
x.setLayoutY(10);
// define the initial window size
x.setPrefSize(330, 210);
x.setResizableWindow(false);
// either to the left
x.getRightIcons().add(new CloseIcon(x));
// add some content
x.getContentPane().getChildren().add(coco);
anchor.getChildren().add(x);
}
}
TingiCont.java
package ting;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
/**
* Created by Udeman on 2/1/14.
*/
public class TingiCont {
@FXML
private Label lbi;
@FXML
private Button si;
@FXML
void Onyesha(ActionEvent event) {
lbi.setText("You made it...");
}
}
TingCont.java
package ting;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import jfxtras.labs.scene.control.window.CloseIcon;
import jfxtras.labs.scene.control.window.Window;
public class TingCont extends Main implements Initializable{
@FXML
private Label lb;
@FXML
private Button cmb;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
@FXML
void clicked(ActionEvent event) throws IOException {
lb.setText("I've been clicked...");
Pane balou = FXMLLoader.load(getClass().getResource("tinGUI.fxml"));
Window w = new Window("TIN WINDOW");
// set the window position to 10,10 (coordinates inside canvas)
w.setLayoutX(10);
w.setLayoutY(10);
// define the initial window size
w.setPrefSize(330, 210);
//w.setResizableWindow(false);
// either to the left
w.getRightIcons().add(new CloseIcon(w));
// add some content
w.getContentPane().getChildren().add(balou);
anchor.getChildren().add(w);
//((Node)(event.getSource())).getScene().getWindow().hide();
}
}
这是主类。 Main.java
package ting;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import jfxtras.labs.scene.control.window.CloseIcon;
import jfxtras.labs.scene.control.window.Window;
public class Main extends Application {
public static AnchorPane anchor = new AnchorPane();
@Override
public void start(Stage primaryStage) throws Exception{
Pane sunda = FXMLLoader.load(getClass().getResource("tingGUI.fxml"));
sunda.setLayoutX(130);
sunda.setLayoutY(60);
anchor.getChildren().add(sunda);
primaryStage.setTitle("TING");
primaryStage.setScene(new Scene(anchor, 600, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
为了能够正确对齐元素最好是使用场景生成器。由于我打算使用内部框架,我从JFxtras团队下载了JFXtras 8实验室jar文件。 JavaFx8目前不支持内部框架。
在主java类,我创建了一个静态anchorpane将其他控制器可以继承,因为他们延长主类。 从那里,我创建了一个JFXtras窗口,并在窗格上使用fxml加载器加载了我的fxml文件的内容。我将窗格添加到Jfxtras窗口,最后将窗口添加到从Main类创建的anchorpane。
总之,要正确对齐GUI元素一个是最好的使用场景建设者。为了共享相同的锚板,最好在一个类中创建一个静态锚板,并从剩下的控制器继承该类。
现在都可以自定义UI元素添加到当前场景生成器,但一个是如何从现场制造商使用他们的限制。你不仅可以拖放自定义控件而不是从场景生成器
修改它们快速的话,jfxtras窗口是目前在JavaFx8内部帧最接近的方式。 Oracle尚未在Javafx8中实现内部框架。但是,由于某种原因,jfxtras窗口会从fxml文件中剔除比例GUI元素。 那么,我们将暂停Javafx8的编码,直到引入内部框架。我非常依赖他们。我现在回到c#....