我正在为我的一项任务制作一个简单的 GUI。我已经创建了一个裸骨界面,但我目前遇到了一些问题。该程序显示一切正常,只是我想将对象居中以便看起来更整洁。我尝试的是将按钮(属于底部)放在 borderPane 上,并使用 setCetner 方法将其与中心对齐,但这没有任何作用。还有另一种方法可以尝试将所有对象放在窗格中吗?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main extends Application {
private TextField startDate;
private Text start;
private Text end;
private TextField endDate;
private Button count;
@Override
public void start(Stage primaryStage) {
start = new Text("Start Date: ");
end = new Text("End Date: ");
startDate = new TextField("1/1/2000");
endDate = new TextField(getDate());
count = new Button("Count");
HBox startLine = new HBox(10);
startLine.getchildren().addAll(start,startDate);
HBox endLine = new HBox(10);
endLine.getchildren().addAll(end,endDate);
HBox button = new HBox();
button.getchildren().add(count);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(button);
VBox vbox = new VBox(10);
vbox.getchildren().addAll(startLine,endLine,button);
Pane root = new Pane();
root.getchildren().addAll(vbox);
Scene scene = new Scene(root,400,300);
primaryStage.setTitle("Date Counter");
primaryStage.setScene(scene);
primaryStage.show();
}
public String getDate(){
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
return dateFormat.format(date);
}
public static void main(String[] args) {
Application.launch(args);
}
}