JAVAFX
<github.com/leewyatt>
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
System.out.println('start...');
primaryStage.setTitle('java fx app');
primaryStage.show();
}
@Override
public void init()throws Exception{
super.init();
System.Out.println('init ...');
}
@Override
public void stop()throws Exception{
super.stop();
System.out.println('stop...');
}
}
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Button button = new Button('小');
BorderPane pane = new BorderPane(button);
button.setOnAction(e->{
getHostServices().showDocument('www.baidu.com')
});
Scene scene = new Scene(pane,300,300);
primaryStage.setScene(scene);
primaryStage.setTitle('javafx app');
primaryStage.show();
}
}
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Button button = new Button('hello');
button.setLayoutX(200);
button.setLayoutY(200);
AnchorPane pane = new AnchorPane();
pane.getChildren().add(button);
Scene scene = new Scene(pane,500,500);
primaryStage.setScene(scene);
primaryStage.setTitle('javafx');
parimaryStage.getIcons().add(new Image('image/logo.png'));
parimaryStage.setResizable(false);
parimaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
}
}
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Button button0 = new Button('hello0');
Button button1 = new Button('hello1');
button0.setLayoutX(200);
button0.setLayoutY(200);
button1.setLayoutX(200);
button1.setLayoutY(250);
AnchorPane pane = new AnchorPane();
pane.getChildren().addAll(button0,button1);
button0.setOnAction(event->{
Stage stage = new Stage();
stage.setHeight(200);
stage.setWidth(300);
stage.initOwner(primaryStage);
// stage.initModality(Modality.APPLICATION_MODAL);
stage.initModality(Modality.WINDOW_MODAL);
stage.show();
})
button1.setOnAction(event->{
Stage stage = new Stage();
stage.setHeight(200);
stage.setWidth(300);
// stage.initOwner(primaryStage);
// stage.initModality(Modality.APPLICATION_MODAL);
// stage.initModality(Modality.WINDOW_MODAL);
stage.show();
})
Scene scene = new Scene(pane,500,500);
primaryStage.setScene(scene);
primaryStage.setTitle('javafx');
parimaryStage.getIcons().add(new Image('image/logo.png'));
parimaryStage.setResizable(false);
parimaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
}
}
退出程序
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Button button0 = new Button('hello0');
Button button1 = new Button('hello1');
button0.setLayoutX(200);
button0.setLayoutY(200);
button1.setLayoutX(200);
button1.setLayoutY(250);
AnchorPane pane = new AnchorPane();
pane.getChildren().addAll(button0,button1);
// 阻止默认退出按钮
Platform.setImplicitExit(false);
primaryStage.setOnCloseRequest(event->{
event.consume();
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle('退出程序');
alert.setHeaderText(null);
alert.setContentText('是否退出程序');
Optional <ButtonType> result = alert.showAndWait();
if(result.get()== ButtonType.OK){
// Platform.exit();
primaryStage.close();
}
})
Scene scene = new Scene(pane,500,500);
primaryStage.setScene(scene);
primaryStage.setTitle('javafx');
parimaryStage.getIcons().add(new Image('image/logo.png'));
parimaryStage.setResizable(false);
parimaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
}
}
切换场景
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Button button0 = new Button('hello0');
Button button1 = new Button('hello1');
button0.setLayoutX(200);
button0.setLayoutY(200);
button1.setLayoutX(200);
button1.setLayoutY(250);
AnchorPane root = new AnchorPane();
root.getChildren().addAll(button0);
Scene scene = new Scene(root, 500,500);
Label label = new Label("abc");
label.setLayoutX(200);
label.setLayoutY(200);
Button button1 = new Button('返回场景');
button1.setLayoutX(200);
button1.setLayoutY(200);
AnchrPane root1 = new AnchorPane();
root1.getChildren().addAll(label,button1);
Scene scene1 = new Scene(root1,500,500);
scene1.setCursor(new ImageCursor(new Image('image/p.png')));
scene.setCursor(new ImageCursor(new Image('image/p2.png')));
button0.setOnAction(event->{
primaryStage.setScene(scene1);
});
button1.setOnAction(event->{
primaryStage.setScene(scene);
})
primaryStage.setScene(scene);
primaryStage.setTitle('javafx');
parimaryStage.getIcons().add(new Image('image/logo.png'));
parimaryStage.setResizable(false);
parimaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
}
}
javafx css 样式
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Label label = new Label("hello");
label.setLayoutX(200);
label.setLayoutY(200);
label.setStyle("-fx-background-color:red;-fx-border-color:blue;-fx-border-width:3px");
label.setPrefWidth(200);
label.setPrefHeight(50);
label.setAlignment(Post.CENTER);
AnchorPane root = new AnchorPane();
root.getChildren().add(label);
Scene scene = new Scene(root,500,500);
primaryStage.setScene(scene);
primaryStage.show();
}
}
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Label label = new Label("hello");
label.setLayoutX(200);
label.setLayoutY(200);
label.setStyle("-fx-background-color:red;-fx-border-color:blue;-fx-border-width:3px");
label.setPrefWidth(200);
label.setPrefHeight(50);
label.setAlignment(Post.CENTER);
label.setOpacity(0.5);
label.setRotate(70);
label.setTranslateX(60);
label.setTranslatey(100);
AnchorPane root = new AnchorPane();
root.getChildren().add(label);
Scene scene = new Scene(root,500,500);
primaryStage.setScene(scene);
primaryStage.show();
}
}
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Circle circle = new Circle();
circle.setCenterX(250);
circle.setCenterY(250);
circle.setRadius(100);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
AnchorPane root = new AnchorPane();
root.getChildren().add(label);
Scene scene = new Scene(root,500,500);
primaryStage.setScene(scene);
primaryStage.show();
}
}
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Scene scene = new Scene(root,500,500);
Circle circle = new Circle();
circle.setCenterX(250);
circle.setCenterY(250);
circle.setRadius(100);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
circle.centerXProperty().bind(scene.widthProperty().divide(2));
circle.centeryProperty().bind(scene.heightProperty().divide(2));
root.getChildren().add(circle);
primaryStage.setScene(scene);
primaryStage.show();
}
}
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
Scene scene = new Scene(root,500,500);
Circle circle = new Circle();
circle.setCenterX(250);
circle.setCenterY(250);
circle.setRadius(100);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
circle.centerXProperty().bind(scene.widthProperty().divide(2));
circle.centeryProperty().bind(scene.heightProperty().divide(2));
circle.centerXProperty().addListener(new ChangeListener<Number>(){
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue,Number newValue){
System.out.println();
}
})
root.getChildren().add(circle);
primaryStage.setScene(scene);
primaryStage.show();
}
}
public class Main extends Application{
public static void main(String[] args){
Application.launch(args);
}
@Override
public void start (Stage primaryStage) throws Exception{
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root,500,500);
Label label = new Label('Hello');
label.setLayoutX(200);
label.setLayoutY(200);
Button button = new Button('向上移动');
button.setLayoutX(300);
button.setLayoutY(200);
root.getChildren().addAll(label,button);
button.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event){
label.setLayoutY(label.getLayoutY()-5);
}
})
// button.setOnKeyReleased(new EventHandler<KeyEvent>(){
// @Override
// public void handle(KeyEvent event){
// }
// })
button.setOnKeyReleased(event->{
KeyCode keyCode = event.getCode();
if(keyCode.equals(KeyCode.DOWN)){
label.setLayoutY(label.getLayoutY() + 5)
}
})
primaryStage.setScene(scene);
primaryStage.show();
}
}