user-interface - JAVA FX 类 GUI
问题描述
下面的程序利用 java fx 类创建一个图形用户界面来验证一个人是否有资格喝酒。问题是输出显示在控制台上而不是图形用户界面上 I. 我该如何解决这个问题?
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ValidateAge extends Application {
//Class Variables
Stage window;
Scene scene;
Button button;
//Main Methods
public static void main(String[] args) { // Main
launch(args);
}
@Override //Method that creates Graphical User Interface
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Validate Age For Drinking");// displays "Validate age" on top of window
TextField ageInput = new TextField();// Object for display text field
Label label1 = new Label();
button = new Button("Verify Age"); //Displays Verify Age Buttom
button.setOnAction( e -> isInt(ageInput, ageInput.getText() , label1));
//Parameters for Layouts
VBox layout = new VBox(10);
layout.setPadding(new Insets(20, 20, 20, 20)); //sets the size of the layout
layout.getChildren().addAll(ageInput, label1, button);
scene = new Scene(layout, 400, 550);//sets dimensions for scene
window.setScene(scene);
window.show();
}
//Validates user age
private int isInt(TextField input, String message, Label label){
// validates to see if user is above 18 to drink
try{
int age = Integer.parseInt(input.getText());
if(age< 18) {
label.setText("You are not allowed to drink");
}else {
System.out.println("You are allowed to drink");
}
}catch(NumberFormatException e){ // displays when user fails to type in a number
System.out.println("Error: " + message + " Tye in a number ");
}
return 0;
}
}
解决方案
代替
System.out.println("You are allowed to drink");
放
label.setText("You are allowed to drink");
你很高兴。
推荐阅读
- python - 如何在 Python 中搜索二维数组中的元素
- amazon-web-services - Terraform 0.12 问题。此对象没有名为“xyz”的属性
- sql - 简单的递归 SQL 查询
- ios - iOS MDM 配置文件是公开的吗?
- javascript - 向电报机器人发送消息
- javascript - 如何在 chrome devtools 中隐藏源代码?
- javascript - 如果在 Select Dropdown 插件上添加了类,则按钮事件不起作用
- python - WSL ubuntu 环境中的 PIP 安装错误
- react-native - 如何访问屏幕组件内的 NavigatorScreen > 选项?
- r - 如何在 mij 条形图中同时拥有实际计数和分组百分比?