简体   繁体   English

如何从 Netbeans 中的 System.in (Java) 获取输入?

[英]How do I get input from System.in (Java) in Netbeans?

I have a small Java test app in Netbeans where the main() class reads input from System.in .我在 Netbeans 中有一个小型 Java 测试应用程序,其中main()类从System.in读取输入。 How can I open a window into which I can type input?如何打开一个可以输入输入的窗口? (I am using NB 6.7.1 on Windows 7). (我在 Windows 7 上使用 NB 6.7.1)。

It may not be obvious but in Netbeans the Output tab at the bottom also takes input if your main thread is waiting for input.这可能并不明显,但在 Netbeans 中,如果您的主线程正在等待输入,则底部的输出选项卡也会接受输入。 Just type under the last output line and hit enter.只需在最后一个输出行下输入并按回车键即可。 In other words, the Output tab is the same as a console window.换句话说,输出选项卡与控制台窗口相同。

I'm pretty confident the following worked in NB 6.5 Just type into the output window which happens to accept input我非常有信心以下内容在 NB 6.5 中工作只需输入恰好接受输入的输出窗口

InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(inputStreamReader);
System.out.println("Type name:");
String name = reader.readLine();
System.out.println("Hello "+name);

In Eclipse, you can just type in your console window.在 Eclipse 中,您只需在控制台窗口中键入即可。 I suppose Netbeans would have a similar option.我想 Netbeans 会有类似的选择。

If you just want a small window to type some input into, then the easiest way is to use JOptionPane.如果您只想在一个小窗口中输入一些输入,那么最简单的方法是使用 JOptionPane。 For example:例如:

import javax.swing.JOptionPane;

public class TestClass {
    public static void main(String[] args) {
        String answer;
        answer = JOptionPane.showInputDialog(null, "What number to multiply by 3?");
        int num = Integer.parseInt(answer);
        num = num * 3;
        JOptionPane.showMessageDialog(null, "The answer is " + num);
    }
}

Note that showInputDialog returns a String, so you'll have to convert the data to whatever format you need.请注意,showInputDialog 返回一个字符串,因此您必须将数据转换为您需要的任何格式。 If you have something more involved, then JOptionPane may not be the way to go.如果你有更多的事情,那么 JOptionPane 可能不是要走的路。

if you are asking for a visual input, NetBeans provides a very easy way to manage visual components, as easy as drag-and-drop如果您需要可视化输入,NetBeans 提供了一种非常简单的方法来管理可视化组件,就像拖放一样简单

how to do it:怎么做:

  • Create a JFrame by right-click on your package > New > JFrame form通过右键单击您的包 > 新建 > JFrame 表单来创建 JFrame
  • you can see a "source" tab and a "design" tab on top of the frame.您可以在框架顶部看到“源”选项卡和“设计”选项卡。
  • drag and drop your visual components (like Text Field from the Swing Control on the right menu)拖放您的视觉组件(例如右侧菜单上的 Swing 控件中的文本字段)
  • after placing your component(s) on the Frame, right-click > Events > (then choose the type of event you want to handle for each component)将组件放在框架上后,右键单击 > 事件 >(然后选择要为每个组件处理的事件类型)

it may look difficult and scary for first-timers, but once you start playing with it, few minutes and you will enjoy your experiments ;)对于初学者来说,它可能看起来既困难又可怕,但是一旦你开始玩它,几分钟,你就会享受你的实验;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM