如何在 Java GUI 中将消息打印到屏幕上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7974154/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:16:59  来源:igfitidea点击:

How do I print messages to the screen in my Java GUI?

javauser-interface

提问by Dave Fisher

I'm creating a GUI ATM machine. How can I print messages to the screen? I have buttons 0-9 as well as an enter and cancel set up, for the user to press.

我正在创建一个 GUI ATM 机。如何将消息打印到屏幕上?我有按钮 0-9 以及输入和取消设置,供用户按下。

回答by Griffin W.

You should put a JTextField or a JLabel into your program. These two components display text that the user can read. I suggest using a JTextField, mostly because I love using text boxes. Here's an example of how to use the text:

您应该将 JTextField 或 JLabel 放入您的程序中。这两个组件显示用户可以阅读的文本。我建议使用 JTextField,主要是因为我喜欢使用文本框。以下是如何使用文本的示例:

http://leepoint.net/notes-java/GUI/components/30textfield/11textfield.html

http://leepoint.net/notes-java/GUI/components/30textfield/11textfield.html

Basically you should do

基本上你应该做

JTextField myOutput = new JTextField(16);

Where myOutput is what you choose the JTextField's variable to be and 16 is the length of the JTextField in the GUI. To display a message:

其中 myOutput 是您选择的 JTextField 变量,16 是 GUI 中 JTextField 的长度。要显示消息:

myOutput.setText("some text");

You can also initialize the value of the JTextField while declaring the text field at the same time

您还可以在声明文本字段的同时初始化 JTextField 的值

JTextField myOutput = new JTextField("someInitialValue", 20);

The code speaks for itself

代码不言自明

Hope this helps

希望这可以帮助

回答by Charles Sprayberry

You would want to assign this value to some GUI component, such as a JTextFieldor JLabel, by using the setText(String text)method on said component.

您可能希望通过使用该组件上的方法将此值分配给某个 GUI 组件,例如 aJTextField或。JLabelsetText(String text)