JavaFX基础与图形处理实战
立即解锁
发布时间: 2025-08-18 02:24:38 阅读量: 2 订阅数: 13 

# JavaFX 基础与图形处理实战
## 1. JavaFX TabPane 控件使用
### 1.1 TabPane 简介
TabPane 控件用于创建选项卡界面,它与 Java Swing 的 JTabbedPanel 类似,但使用方式有所不同。在 TabPane 中,我们添加的是 `javafx.scene.control.Tab` 实例,而非 JPanels。
### 1.2 设置 Tab 方向
可以通过菜单选项来设置 Tab 的方向,包括左、右、上、下。以下是设置 Tab 方向的代码示例:
```java
public void handle(ActionEvent event) {
MenuItem mItem = (MenuItem) event.getSource();
String side = mItem.getText();
if ("left".equalsIgnoreCase(side)) {
tabPane.setSide(Side.LEFT);
} else if ("right".equalsIgnoreCase(side)) {
tabPane.setSide(Side.RIGHT);
} else if ("top".equalsIgnoreCase(side)) {
tabPane.setSide(Side.TOP);
} else if ("bottom".equalsIgnoreCase(side)) {
tabPane.setSide(Side.BOTTOM);
}
}
```
### 1.3 添加 Tab 到 TabPane
以下代码展示了如何将 Tab 添加到 TabPane 中:
```java
TabPane tabPane = new TabPane();
Tab tab = new Tab();
tab.setText("Tab" + i);
tabPane.getTabs().add(tab);
```
## 2. 开发 JavaFX 对话框
### 2.1 问题描述
需要创建一个模拟更改密码的对话框应用程序,该应用包含菜单选项以弹出对话框,并且用户可以设置对话框的模态状态。
### 2.2 解决方案
使用 JavaFX 的 `javafx.stage.Stage` 和 `javafx.scene.Scene` API 来创建对话框。以下是完整的代码示例:
```java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.util.Duration;
import javafx.scene.shape.Circle;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.paint.CycleMethod;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.geometry.HPos;
import javafx.stage.Platform;
public class DevelopingADialog extends Application {
static Stage LOGIN_DIALOG;
static int dx = 1;
static int dy = 1;
public static void main(String[] args) {
Application.launch(args);
}
private static Stage createLoginDialog(Stage parent, boolean modal) {
if (LOGIN_DIALOG != null) {
LOGIN_DIALOG.close();
}
return new MyDialog(parent, modal, "Welcome to JavaFX!");
}
@Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("Developing a Dialog");
Group root = new Group();
Scene scene = new Scene(root, 433, 312, Color.WHITE);
MenuBar menuBar = new MenuBar();
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
Menu menu = new Menu("Home");
MenuItem newItem = new MenuItem("Change Password", null);
newItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
if (LOGIN_DIALOG == null) {
LOGIN_DIALOG = createLoginDialog(primaryStage, true);
}
LOGIN_DIALOG.sizeToScene();
LOGIN_DIALOG.show();
}
});
menu.getItems().add(newItem);
menu.getItems().add(new SeparatorMenuItem());
ToggleGroup modalGroup = new ToggleGroup();
RadioMenuItem nonModalItem = RadioMenuItemBuilder.create()
.toggleGroup(modalGroup)
.text("Non Modal")
.selected(true)
.build();
nonModalItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
LOGIN_DIALOG = createLoginDialog(primaryStage, false);
}
});
menu.getItems().add(nonModalItem);
RadioMenuItem modalItem = RadioMenuItemBuilder.create()
.toggleGroup(modalGroup)
.text("Modal")
.selected(true)
.build();
modalItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
LOGIN_DIALOG = createLoginDialog(primaryStage, true);
}
});
menu.getItems().add(modalItem);
menu.getItems().add(new SeparatorMenuItem());
MenuItem exitItem = new MenuItem("Exit", null);
exitItem.setMnemonicParsing(true);
exitItem.setAccelerator(new KeyCodeCombination(KeyCode.X,
KeyCombination.CONTROL_DOWN));
exitItem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
Platform.exit();
}
});
menu.getItems().add(exitItem);
menuBar.getMenus().add(menu);
root.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
addBouncyBall(scene);
}
private void addBouncyBall(final Scene scene) {
final Circle ball = new Circle(100, 100, 20);
RadialGradient gradient1 = new RadialGradient(0,
.1,
100,
100,
20,
false,
CycleMethod.NO_CYCLE,
new Stop(0, Color.RED),
new Stop(1, Color.BLACK));
ball.setFill(gradient1);
final Group root = (Group) scene.getRoot();
root.getChildren().add(ball);
Timeline tl = new Timeline();
tl.setCycleCount(Animation.INDEFINITE);
KeyFrame moveBall = new KeyFrame(Duration.seconds(.0200),
new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
double xMin = ball.getBoundsInParent().getMinX();
double yMin = ball.getBoundsInParent().getMinY();
double xMax = ball.getBoundsInParent().getMaxX();
double yMax = ball.getBoundsInParent().getMaxY();
if (xMin < 0 || xMax > scene.getWidth()) {
dx = dx * -1;
}
if (yMin < 0 || yMax > scene.getHeight()) {
dy = dy * -1;
}
ball.setTranslateX(ball.getTranslateX() + dx);
ball.setTranslateY(ball.getTranslateY() + dy);
}
});
tl.getKeyFrames().add(moveBall);
tl.play();
}
}
class MyDialog extends Stage {
public MyDialog(Stage owner, boolean modality, String title) {
super();
initOwner(owner);
Modality m = modality ? Modality.APPLICATION_MODAL : Modality.NONE;
```
0
0
复制全文
相关推荐









