How do I display something I enter in a JOptionPane on the JFrame?

送分小仙女□ 提交于 2019-12-12 04:46:32

问题


What I want to do is when I input something in JOptionPane, say, APPLES. I want to display it as APPLES in the JFrame. Now, if I want to input CATS next, it'll appear in the JFrame together with the APPLES.

It should look like this: APPLES CATS

And when I input more, it just displays and displays. The only way I know how to do it is to use setText for JLabel, but it's only displaying ONE word. How do I display ALL of the words I input?


回答1:


but it's only displaying ONE word

Hoping that you are getting word written in JOptionPane in JFrame, try this:

jLabel.setText(jLabel.getText() + " " + strGotFromJOptionPane);

Here strGotFromJOptionPane is the text you got from JOptionPane.




回答2:


Try this: myLabel.setText(myLabel.getText() + " " + myTextField.getText());




回答3:


JTextArea has an append(String) method. It is a multi-line component that might be more appropriate for displaying a list of strings. Or, for that matter, a JList, or JTable might be better suited to displaying the user input.



来源:https://stackoverflow.com/questions/8323137/how-do-i-display-something-i-enter-in-a-joptionpane-on-the-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!