JavaFX基础
程序员文章站
2024-02-05 12:05:16
...
设置一个场景
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Myjava extends Application {
@Override
//重写
public void start(Stage primaryStage) {
Button btOK = new Button("OK");
//创键一个OK按钮
Scene scene = new Scene(btOK,300,450);
//将按钮放入场景中并设置场景大小
primaryStage.setTitle("MyjavaFX");
//设置舞台标题
primaryStage.setScene(scene);
//将场景放入舞台
primaryStage.show();
//展示舞台
}
public static void main(String[] args) {
launch(args);
}
}
同时设置两个场景
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Myjava extends Application {
@Override
//重写
public void start(Stage primaryStage) {
Scene scene = new Scene((new Button("OK")),300,450);
//将按钮放入场景中并设置场景大小
primaryStage.setTitle("MyjavaFX");
//设置舞台标题
primaryStage.setScene(scene);
//将场景放入舞台
primaryStage.show();
//展示舞台
Stage stage = new Stage();
stage.setTitle("Second Stage");
//设置场景标题
stage.setScene(new Scene(new Button("new Stage"),300,450));
//在舞台总设置一个带有按钮的场景
stage.show();
//展示舞台
}
}
将按钮置于面板中
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.layout.StackPane;
public class Myjava extends Application {
@Override
//重写
public void start(Stage primaryStage) {
StackPane pane = new StackPane();
//创建一个面板
pane.getChildren().add(new Button("OK"));
//在面板中添加按钮
Scene scene = new Scene(pane,200,50);
primaryStage.setTitle("Button in a pane");
//设置标题
primaryStage.setScene(scene);
//将场景放入舞台中
primaryStage.show();
//展示舞台
}
}
在面板*显示圆的比例
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
public class Myjava extends Application {
@Override
//重写
public void start(Stage primaryStage) {
Circle circle = new Circle();
circle.setCenterX(100);
circle.setCenterY(100);
//设置圆的中心位置
circle.setRadius(50);
//设置圆的半径
circle.setStroke(Color.RED);
//设置边缘的颜色
circle.setFill(Color.YELLOW);
//设置填充颜色
Pane pane = new Pane();
//
pane.getChildren().add(circle);
Scene scene = new Scene(pane,200,200);
primaryStage.setTitle("ShowCircle");
//设置标题
primaryStage.setScene(scene);
//将场景放入舞台中
primaryStage.show();
//展示舞台
}
}
package application;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5,12.5, 13.5,14.5));
pane.setHgap(5.5);
pane.setVgap(5.5);
pane.add(new Label("First Name"),0,0);
pane.add(new TextField(),1,0);
pane.add(new Label("MI:"),0,1);
pane.add(new TextField(),1,1);
pane.add(new Label("Last Name:"),0,2);
pane.add(new TextField(),1,2);
Button btAdd = new Button("Add Name");
pane.add(btAdd,1,3);
GridPane.setHalignment(btAdd,HPos.RIGHT);
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowGridPane");
primaryStage.setScene(scene);
primaryStage.show();
}
}
留言以鼓励自己和看我博客的人:
你不是我,怎知我走过的路,心中的苦与乐。