在JavaFX中未以指定大小打开的新窗口

时间:2014-04-16 14:37:24

标签: button javafx javafx-2 stage

点击javafx中的按钮时,我试图打开一个新窗口。单击按钮时,新窗口打开,但其大小不符合我的预期。我会提供我的代码。以下代码是按钮 -

b2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            setEffect(new BoxBlur(5, 10, 10));
            Stage usrpagestage = new Stage();
            usrpagestage.setMaxHeight(300);
            usrpagestage.setMaxWidth(210);
            usrpagestage.setResizable(false);
            usrpagestage.initStyle(StageStyle.UTILITY);
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connect = DriverManager
                        .getConnection("jdbc:mysql://localhost:3306/project?"
                                + "user=root&password=virus");
                statement = connect.createStatement();
                preparedStatement = connect
                        .prepareStatement("SELECT count(*)FROM information_schema.tables\n" +
                                "WHERE table_schema = 'project' AND table_name = 'subject'");
                rs=preparedStatement.executeQuery();
                rs.next();
                int chk ;
                chk = rs.getInt(1);

              if(chk!=1)
              {
                  usrpagestage.setScene(new Scene(new SubWarning()));
                  usrpagestage.show();
              }

              else
              {
                  usrpagestage.setScene(new Scene(new AddStaff()));
                  usrpagestage.show();
              }
            }
            catch (ClassNotFoundException | SQLException e1) {
                try {
                    throw e1;
                } catch ( ClassNotFoundException | SQLException ex) {
                    Logger.getLogger(TutorPage.class.getName()).log(Level.SEVERE, null, ex);
                }
            } 
            finally {
               close2();
            }

            usrpagestage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent t) {
                    setEffect(new BoxBlur(0, 0, 0));
                }
            });
        }
    });

以下代码是要打开的新窗口 -

package subwarning;

import javafx.geometry.Insets;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class SubWarning extends BorderPane{

public SubWarning() {
    setCenter(addVBox());        
}

private VBox addVBox() {

    VBox vb1 = new VBox();
    vb1.setPadding(new Insets(15, 20, 25, 20));
    vb1.setSpacing(15);
    vb1.setStyle("-fx-background-color: #333333;");

    Text t1 = new Text("Add Subjects first.\n Then add Staff.");
    t1.setFont(Font.font("Arial", FontWeight.BOLD, 20));
    t1.setFill(Color.RED);

    return vb1;
}

}

单击按钮时,会打开一个小按钮。它太小了,我甚至看不到里面的内容。如何以指定大小打开窗口?

2 个答案:

答案 0 :(得分:1)

你的窗户很小。您只需指定最大尺寸。

可能的解决方案:

  1. 使用setWidth中的setHeightWindow API(在您的情况下为Stage

  2. 致电setMaxHeightsetMaxWidth致电setMinHeightsetMinWidth


  3. 编辑1

    • 设置Scene
    • 后使用解决方案#1
    • 使用解决方案#2和setResizable(true)

答案 1 :(得分:0)

我找到了答案。我忘记将文本添加到VBox。当我添加它时,窗口以指定的大小显示。