2014-01-25 46 views
0

我最近從c#轉移到嘗試Javafx2。我也是這個論壇的新手。 我一直試圖在Javafx中實現內部框架。 我偶然發現了這個鏈接: Internal Frames in JavaFX 我已經設法將jfxtras 8 jar文件添加到我的項目以及場景構建器2中。 但是,我一直在對齊窗口上的控件。Javafx8在內部框架對齊控件

這是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窗口。在這種情況下,我嘗試通過場景構建器向jfxtras窗口中添加一個javafx按鈕,但它從未工作,因爲它已添加到錨窗格而不是窗口,從而導致jfxtras窗口和javafx按鈕均爲錨窗格的子項。

回答

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> 

下面是控制器。他們擴展了主要的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#....