我目前正在尝试使用Java中的MVP模型创建一个登录屏幕。
我尝试制作一个网格(javafx.scene.layout.GridPane),然后将其放在BorderPane中以制作漂亮的布局。但在运行代码时,屏幕仍为空。我只得到一个带标题的屏幕。
这是我的代码: 1.查看
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class View extends BorderPane{
GridPane grid = new GridPane();
Text scenetitle = new Text();
Label userName = new Label();
TextField userTextField = new TextField();
Label pw = new Label();
PasswordField pwBox = new PasswordField();
Button btn = new Button();
HBox hbBtn = new HBox();
final Text actiontarget = new Text();
public View() {
initialiseNodes();
layoutNodes();
}
private void initialiseNodes() {
scenetitle = new Text("Welcome");
userName = new Label("User Name: ");
pw = new Label("Password:");
btn = new Button("Sign in");
scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
hbBtn = new HBox(10);
}
private void layoutNodes() {
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25,25,25,25));
grid.add(scenetitle, 0, 0, 2, 1);
grid.add(userName, 0, 1);
grid.add(userTextField, 1, 1);
grid.add(pw, 0, 2);
grid.add(pwBox, 1, 2);
grid.add(hbBtn, 1, 6);
grid.add(actiontarget, 1, 6);
BorderPane.setAlignment(grid, Pos.CENTER);
}
}
主讲人:
public class Presenter {
private Model model;
private View view;
public Presenter(Model model, View view) {
this.model = model;
this.view = view;
updateView();
handleEvents();
}
private void updateView() {
}
private void handleEvents() {
}
}
主要:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class Login extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Model model = new Model();
View view = new View();
new Presenter(model, view);
Scene scene = new Scene(view);
primaryStage.setTitle("JavaFX Welcome");
primaryStage.setScene(scene);
primaryStage.show();
}
}
答案 0 :(得分:0)
您永远不会将网格添加到边框窗格:
private void layoutNodes() {
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25,25,25,25));
grid.add(scenetitle, 0, 0, 2, 1);
grid.add(userName, 0, 1);
grid.add(userTextField, 1, 1);
grid.add(pw, 0, 2);
grid.add(pwBox, 1, 2);
grid.add(hbBtn, 1, 6);
grid.add(actiontarget, 1, 6);
BorderPane.setAlignment(grid, Pos.CENTER);
this.setCenter(grid);
}