简体   繁体   English

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

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

I'm creating a GUI ATM machine. 我正在创建一个GUI ATM机器。 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. 我有按钮0-9以及输入和取消设置,供用户按。

You should put a JTextField or a JLabel into your program. 您应该将JTextField或JLabel放入您的程序中。 These two components display text that the user can read. 这两个组件显示用户可以阅读的文本。 I suggest using a JTextField, mostly because I love using text boxes. 我建议使用JTextField,主要是因为我喜欢使用文本框。 Here's an example of how to use the text: 以下是如何使用文本的示例:

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. 其中myOutput是您选择的JTextField变量,16是GUI中JTextField的长度。 To display a message: 要显示消息:

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 希望这可以帮助

您可能想通过在该组件上使用setText(String text)方法将此值分配给某些GUI组件,例如JTextFieldJLabel

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

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