如何从 Netbeans 中的 System.in (Java) 获取输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1653871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get input from System.in (Java) in Netbeans?
提问by peter.murray.rust
I have a small Java test app in Netbeans where the main()
class reads input from System.in
. How can I open a window into which I can type input? (I am using NB 6.7.1 on Windows 7).
我在 Netbeans 中有一个小型 Java 测试应用程序,main()
该类从System.in
. 如何打开一个可以输入输入的窗口?(我在 Windows 7 上使用 NB 6.7.1)。
采纳答案by jercra
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. Just type under the last output line and hit enter. In other words, the Output tab is the same as a console window.
这可能并不明显,但在 Netbeans 中,如果您的主线程正在等待输入,则底部的输出选项卡也会接受输入。只需在最后一个输出行下输入并按回车键即可。换句话说,输出选项卡与控制台窗口相同。
回答by Paul Wicks
If you just want a small window to type some input into, then the easiest way is to use JOptionPane. For example:
如果您只想在一个小窗口中输入一些输入,那么最简单的方法是使用 JOptionPane。例如:
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. If you have something more involved, then JOptionPane may not be the way to go.
请注意,showInputDialog 返回一个字符串,因此您必须将数据转换为您需要的任何格式。如果你有更多的事情,那么 JOptionPane 可能不是要走的路。
回答by Jorn
In Eclipse, you can just type in your console window. I suppose Netbeans would have a similar option.
在 Eclipse 中,您只需在控制台窗口中键入即可。我想 Netbeans 会有类似的选择。
回答by jitter
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);
回答by evilReiko
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
- 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)
- after placing your component(s) on the Frame, right-click > Events > (then choose the type of event you want to handle for each component)
- 通过右键单击您的包 > 新建 > JFrame 表单来创建 JFrame
- 您可以在框架顶部看到“源”选项卡和“设计”选项卡。
- 拖放您的视觉组件(如右侧菜单上的 Swing 控件中的文本字段)
- 将组件放在框架上后,右键单击 > 事件 >(然后选择要为每个组件处理的事件类型)
it may look difficult and scary for first-timers, but once you start playing with it, few minutes and you will enjoy your experiments ;)
对于初学者来说,它可能看起来既困难又可怕,但是一旦你开始玩它,几分钟,你就会享受你的实验;)