如何将文本添加到JFrame?

时间:2022-09-11 18:43:26

So I am designing a JFrame using Eclipse WindowBuilder. This specific frame is an error message stating that the user provided invalid credentials. I have added a button to exit the frame and I now need to display the actual error message "The login credentials specified are invalid. Please provide valid credentials."

所以我正在使用Eclipse WindowBuilder设计一个JFrame。此特定帧是一条错误消息,指出用户提供了无效凭据。我添加了一个退出框架的按钮,现在我需要显示实际的错误消息“指定的登录凭据无效。请提供有效的凭据。”

I have done some searching and everyone says to use a JLabel, but when I create my JLabel and enter the text to it, there is no wordwrap or anything so I can't fit the label inside my frame.

我做了一些搜索,每个人都说使用JLabel,但是当我创建我的JLabel并输入文本时,没有文字包装或任何东西,所以我无法在我的框架内放置标签。

What is an easy way to simply display a message in the center of the JFrame?

简单地在JFrame的中心显示消息的简单方法是什么?

4 个解决方案

#1


7  

To create a label for text:

要为文本创建标签:

JLabel label1 = new JLabel("Test");

To change the text in the label:

要更改标签中的文字:

label1.setText("Label Text");

And finally to clear the label:

最后要清除标签:

label1.setText("");

And all you have to do is place the label in your layout, or whatever layout system you are using, and then just add it to the JFrame...

您所要做的就是将标签放在您的布局中,或者您正在使用的任何布局系统中,然后将其添加到JFrame中......

#2


4  

Instead of wasting your time to design a JFrame just to display a error message, you can use an JOptionPane which is by default modal:

您可以使用默认模式的JOptionPane,而不是浪费时间来设计JFrame以显示错误消息:

import javax.swing.JOptionPane;

public class Main {

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Your message goes here!","Message", JOptionPane.ERROR_MESSAGE);
    }
}

如何将文本添加到JFrame?

P.S. Stop using Windowbuilder if you want to learn Swing.

附:如果你想学习Swing,请停止使用Windowbuilder。

#3


3  

when I create my JLabel and enter the text to it, there is no wordwrap or anything

当我创建我的JLabel并输入文本时,没有wordwrap或任何东西

HTML formatting can be used to cause word wrap in any Swing component that offers styled text. E.G. as demonstrated in this answer.

HTML格式可用于在任何提供样式文本的Swing组件中导致自动换行。例如。正如这个答案所示。

如何将文本添加到JFrame?

#4


0  

You can add a multi-line label with the following:

您可以使用以下内容添加多行标签:

JLabel label = new JLabel("My label");

label.setText("<html>This is a<br>multline label!<br> Try it yourself!</html>");

From here, simply add the label to the frame using the add() method, and you're all set!

从这里开始,只需使用add()方法将标签添加到框架中,就可以了!

#1


7  

To create a label for text:

要为文本创建标签:

JLabel label1 = new JLabel("Test");

To change the text in the label:

要更改标签中的文字:

label1.setText("Label Text");

And finally to clear the label:

最后要清除标签:

label1.setText("");

And all you have to do is place the label in your layout, or whatever layout system you are using, and then just add it to the JFrame...

您所要做的就是将标签放在您的布局中,或者您正在使用的任何布局系统中,然后将其添加到JFrame中......

#2


4  

Instead of wasting your time to design a JFrame just to display a error message, you can use an JOptionPane which is by default modal:

您可以使用默认模式的JOptionPane,而不是浪费时间来设计JFrame以显示错误消息:

import javax.swing.JOptionPane;

public class Main {

    public static void main(String[] args) {
        JOptionPane.showMessageDialog(null, "Your message goes here!","Message", JOptionPane.ERROR_MESSAGE);
    }
}

如何将文本添加到JFrame?

P.S. Stop using Windowbuilder if you want to learn Swing.

附:如果你想学习Swing,请停止使用Windowbuilder。

#3


3  

when I create my JLabel and enter the text to it, there is no wordwrap or anything

当我创建我的JLabel并输入文本时,没有wordwrap或任何东西

HTML formatting can be used to cause word wrap in any Swing component that offers styled text. E.G. as demonstrated in this answer.

HTML格式可用于在任何提供样式文本的Swing组件中导致自动换行。例如。正如这个答案所示。

如何将文本添加到JFrame?

#4


0  

You can add a multi-line label with the following:

您可以使用以下内容添加多行标签:

JLabel label = new JLabel("My label");

label.setText("<html>This is a<br>multline label!<br> Try it yourself!</html>");

From here, simply add the label to the frame using the add() method, and you're all set!

从这里开始,只需使用add()方法将标签添加到框架中,就可以了!

相关文章