简体   繁体   中英

JFrame: How to display text without JTextArea/JTextField

I want to display text in between these boxes, so that each row looks something like:

{Image of a d20} {Input1} d20 + {Input2} {Roll button}.

This is an odd question, but this screenshot should make things clearer.

在此输入图像描述

The Graphics object for your JPanel actually can draw text on it's own by invoking the drawString command. I'll post an example, but I think it will be most helpful to read the full example that explains it here or here (I personally like the second one, but the first is a Stack Overflow question):

@Override
protected void paintComponent(Graphics g)
{
   super.paintComponent(g);
   g.drawString("abc", 25, 25);
}

I almost flagged this as a duplicate, but it is technically a distinct question. I would recommend just a little more searching before posting though.

EDIT:

To more thoroughly explain how this might be helpful, you could have a custom JPanel element that holds your

{Image of a d20} {Input1} d20 + {Input2} {Roll button}.

which draws the text, image, inputs, and buttons local to a JPanel holding all of them. This JPanel will have a paintComponent() method where you can use the above code. Then just include that in your JFrame. It makes your code a little more modular and easy to organize as you're grouping relevant components. I hope you find this helpful.

Happy coding! Leave a comment if you have any questions.

您也可以根据需要使用JLabel并设置它的文本,或者将JTextField定义为像JLabel一样

Use jLabel you can set the text like this:

yourlabel.setText("your text");

Or you can get the text from a jLabel like this:

String str = yourlabel.getText();

The String "str" would be "your text".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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