import java.awt.TextField; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; public class FXTest2 extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { // 实现文本框 TextField textField1 = new TextField(); TextField textField2 = new TextField(); // 添加事件处理器,将第一个文本框中的文本显示在第二个文本框中 textField1.setOnKeyPressed(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent event) { if (event.getCode() == KeyCode.ENTER) { textField2.setText(textField1.getText()); textField1.clear(); } } }); // 选择面板 FlowPane rootNode = new FlowPane(); rootNode.getChildren().addAll(textField1, textField2); // 搭建场景,设置宽高像素,根节点是rootNode Scene scene = new Scene(rootNode, 300, 100); // 设置舞台 primaryStage.setTitle(""); primaryStage.setScene(scene);// 将场景设置到舞台 primaryStage.show(); } }
时间: 2024-04-28 13:26:41 浏览: 127
这是一个使用JavaFX库开发的简单GUI程序,主要实现了两个文本框和一个场景,并给第一个文本框添加了一个事件处理器,当用户按下Enter键时,将第一个文本框中的文本显示在第二个文本框中。该程序使用了JavaFX库提供的TextField、FlowPane、Scene和Stage等类和方法,可以在JavaFX平台上运行。
相关问题
Java用高级控件制作注册页面
Java中常用的高级控件有Swing和JavaFX,下面分别介绍如何用这两个控件制作注册页面。
1. 使用Swing
Swing是Java中最经典的GUI库之一,下面是使用Swing实现注册页面的示例代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class RegisterFrame extends JFrame implements ActionListener {
private JTextField usernameField;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;
private JButton registerButton;
public RegisterFrame() {
super("注册页面");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4, 2, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel usernameLabel = new JLabel("用户名:");
panel.add(usernameLabel);
usernameField = new JTextField();
panel.add(usernameField);
JLabel passwordLabel = new JLabel("密码:");
panel.add(passwordLabel);
passwordField = new JPasswordField();
panel.add(passwordField);
JLabel confirmPasswordLabel = new JLabel("确认密码:");
panel.add(confirmPasswordLabel);
confirmPasswordField = new JPasswordField();
panel.add(confirmPasswordField);
registerButton = new JButton("注册");
registerButton.addActionListener(this);
panel.add(registerButton);
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == registerButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
String confirmPassword = new String(confirmPasswordField.getPassword());
if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
JOptionPane.showMessageDialog(this, "用户名和密码不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
} else if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this, "两次输入的密码不一致!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
// 执行注册操作
JOptionPane.showMessageDialog(this, "注册成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
}
}
}
public static void main(String[] args) {
new RegisterFrame();
}
}
```
2. 使用JavaFX
JavaFX是Java中比较新的GUI库,使用起来更加现代化和简洁。下面是使用JavaFX实现注册页面的示例代码:
```
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class RegisterApp extends Application implements EventHandler<ActionEvent> {
private TextField usernameField;
private PasswordField passwordField;
private PasswordField confirmPasswordField;
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("注册页面");
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
Label usernameLabel = new Label("用户名:");
grid.add(usernameLabel, 0, 0);
usernameField = new TextField();
grid.add(usernameField, 1, 0);
Label passwordLabel = new Label("密码:");
grid.add(passwordLabel, 0, 1);
passwordField = new PasswordField();
grid.add(passwordField, 1, 1);
Label confirmPasswordLabel = new Label("确认密码:");
grid.add(confirmPasswordLabel, 0, 2);
confirmPasswordField = new PasswordField();
grid.add(confirmPasswordField, 1, 2);
Button registerButton = new Button("注册");
registerButton.setOnAction(this);
grid.add(registerButton, 1, 3);
Scene scene = new Scene(grid, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void handle(ActionEvent event) {
if (event.getSource() instanceof Button) {
String username = usernameField.getText();
String password = passwordField.getText();
String confirmPassword = confirmPasswordField.getText();
if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText("用户名和密码不能为空!");
alert.showAndWait();
} else if (!password.equals(confirmPassword)) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("错误");
alert.setHeaderText("两次输入的密码不一致!");
alert.showAndWait();
} else {
// 执行注册操作
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("成功");
alert.setHeaderText("注册成功!");
alert.showAndWait();
}
}
}
public static void main(String[] args) {
launch(args);
}
}
```
以上就是使用Swing和JavaFX制作Java注册页面的示例代码,需要注意的是,实际开发中需要根据具体需求进行适当的修改和完善。
阅读全文