【问题标题】:Javafx8 align controls in internal frameJavafx8在内部框架中对齐控件
【发布时间】:2014-01-25 22:15:26
【问题描述】:

我最近从 c# 转向尝试 Javafx2。我也是这个论坛的新手。 我一直试图在 Javafx 中实现内部框架。 我偶然发现了这个链接: Internal Frames in JavaFX 我已经设法将 jfxtras 8 jar 文件添加到我的项目以及场景构建器 2 中。 但是,我一直在对齐窗口上的控件。

这是fxml文件代码:

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

<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import jfxtras.labs.scene.control.window.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="trials.MamaCont">
<children><Window fx:id="wini" layoutX="122.0" layoutY="105.0" prefHeight="190.0" prefWidth="313.0" title="Window" />
</children></AnchorPane>

这是控制器类代码:

package trials;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import jfxtras.labs.scene.control.window.CloseIcon;
import jfxtras.labs.scene.control.window.MinimizeIcon;
import jfxtras.labs.scene.control.window.Window;

/**
 * FXML Controller class
 *
 * @author smoothie
 */
public class MamaCont implements Initializable {

    /**
     * Initializes the controller class.
     */


    /*@FXML
    private Button pb;

    @FXML
    private Label lb;*/

    @FXML
    private Window wini;

    /*@FXML
    void pressed(ActionEvent event) {
         lb.setText("Gotcha!!!....");
    }*/

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        wini.getLeftIcons().add(new CloseIcon(wini));
        wini.getRightIcons().add(new MinimizeIcon(wini));
        //wini.setVisible(false);

        Button butt = new Button("Enter");
        /*butt.setLayoutX(100);
        butt.setLayoutY(100);*/

        Label lab = new Label();
        /*lab.setLayoutX(261);
        lab.setLayoutY(192);*/



        butt.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                lab.setText("I've been pressed!!!");
            }
        });

        wini.getContentPane().getChildren().add(butt);
        wini.getContentPane().getChildren().add(lab);
    }    

}

现在,有谁知道我如何能够对齐标签,使其文本显示在按钮下方?当单击按钮时,标签中的文本会出现在按钮上方而不是下方。

有没有人能够使用 javafx2 实现内部框架,您能否分享一下您是如何设法将排列良好的控件放入内部框架中的?

最后,有谁知道如何让场景构建器控制场景构建器中自定义控件的子控件?

我设法将 jfxtras 控件添加到场景构建器,但不幸的是我无法将 javafx 控件添加到 jfxtras 窗口。在这种情况下,我尝试通过场景构建器将 javafx 按钮添加到 jfxtras 窗口,但它从未起作用,因为它被添加到锚窗格而不是导致 jfxtras 窗口和 javafx 按钮都是锚窗格的子窗口的窗口.

【问题讨论】:

    标签: alignment javafx jinternalframe scenebuilder jfxtras


    【解决方案1】:

    在玩弄代码一段时间后,我设法解决了这个问题。我所做的是使用 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>
    

    以下是控制器。它们扩展了 Main 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();
    
        }
    }
    

    这是主类。 主.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 labs jar 文件。 JavaFx8 目前不支持内部框架。

    在 Main java 类中,我创建了一个静态锚窗格,其他控制器可以继承它,因为它们扩展了 Main 类。 从那里,我创建了一个 JFXtras 窗口并使用窗格上的 fxml 加载器加载了我的 fxml 文件的内容。我将窗格添加到 Jfxtras 窗口,最后将窗口添加到从 Main 类创建的锚窗格中。

    总之,要正确对齐 GUI 元素,最好使用场景构建器。为了共享同一个锚窗格,最好在一个类中创建一个静态锚窗格,其余控制器从该类继承。

    现在可以将自定义 UI 元素添加到当前的 Scene Builder 中,但是限制如何从 Scene Builder 中使用它们。您不仅可以拖放自定义控件,也不能从 Scene Builder 修改它们

    快速说明一下,jfxtras 窗口是目前在 JavaFx8 中拥有内部框架的最接近的方式。 Oracle 尚未在 Javafx8 中实现内部框架。但是,出于某种原因,jfxtras 窗口与 fxml 文件中的 GUI 元素不成比例。 好吧,在引入内部框架之前,我将暂时停止使用 Javafx8 编码。我非常依赖他们。我现在回到 c#....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 2017-06-14
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多