【问题标题】:Get user input in a JFrame Java file before main program starts在主程序启动之前在 JFrame Java 文件中获取用户输入
【发布时间】:2017-03-28 21:09:53
【问题描述】:

我需要使用下面附加的 KDTreeTester.java 文件进行各种测试运行。每次我完成运行并重新编译文件时更改值(w、h、n、x)会非常低效。

那时我认为在主程序打开之前输入值会非常方便。

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Main program that starts the KDTree visualization
 */
 public class KDTreeTester extends JFrame{

  int w=400;    //width of the window
  int h=400;    //height of the window
  int n=20;     //number of points
  int x=5;      //number of points to be searched (has to be smaller than n)
  KDTreeVisualization vis;  //the visualization

  public KDTreeTester(){

    //  Initialize the visualization and add it to the main frame. 
    vis = new KDTreeVisualization(w, h, n);
    add(vis);

代码继续,但我认为只有这才是相关部分。 我需要一个框来询问四个值(宽度、高度、点数、要搜索的点数)。我猜必须将其从 String 解析为 int。

实现这一点的最佳方法是什么?

【问题讨论】:

  • 在main方法开始之前你不能做任何事情,只有通过cmd启动。您可以使用自定义 JDialog
  • 如果您使用的是JFrame,那么为什么不使用JTextField 来收集您的输入?

标签: java jframe user-input


【解决方案1】:

正如@XtremeBaumer 告诉你的那样:

在 main 方法开始之前你不能做任何事情,只有当你开始 通过cmd

但是,如果您的意思是在显示主框架之前要求输入值,您可以使用JOptionPane 来收集值。

这是一个基于Multiple input in JOptionPane.showInputDialog 的示例,并且为了将格式限制为integer 类型,在How to make JFormattedTextField accept integer without decimal point (comma)?

只需在实例化 KDTreeVisualization 之前调用它,并在获得这些输入参数后调用它。

虽然我不会从 JFrame 的构造函数中调用它,而是在实例化 JFrame 之前调用它,或者甚至通过自定义菜单/按钮/您可以添加到框架中的任何内容。

NumberFormat integerFieldFormatter = NumberFormat.getIntegerInstance();

JFormattedTextField nField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField xField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField widthField = new JFormattedTextField(integerFieldFormatter);
JFormattedTextField heightField = new JFormattedTextField(integerFieldFormatter);

Object[] message = {

        "Width :", widthField,
        "Height :", heightField,
        "n :", nField,
        "x :", xField
};

int option = JOptionPane.showConfirmDialog(null, message, "Enter values", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {

    try {
        int n = Integer.parseInt(nField.getText());
        int x = Integer.parseInt(xField.getText());
        int width = Integer.parseInt(xField.getText());
        int height = Integer.parseInt(xField.getText());

        // do stuff with the values, like instantitate your object with those parameters
        // new KDTreeVisualization(width, height, n)

    } catch (NumberFormatException e) {

        JOptionPane.showMessageDialog(null, "At least one of the values is invalid", "Error",
                JOptionPane.ERROR_MESSAGE);
    }

} else {
    System.out.println("Input cancelled");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多