java 在 jdialog 框中格式化文本

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

formatting text in jdialog box

javafontscolorsawt

提问by whitewolfpgh

I have a JOptionPane:

我有一个 JOptionPane:

JOptionPane.showMessageDialog(null, text);

The text is a sting:

文字是刺痛:

String text = "Hello world."

What I want to do is change the color of the text, specifically a single word, lets say 'Hello'. SO what I've tried is:

我想要做的是改变文本的颜色,特别是一个单词,让我们说“你好”。所以我试过的是:

String t1 = "Hello";
String t2 = "world."
Font serifFont = new Font("Serif", Font.BOLD, 12);
AttributedString as = new AttributedString(t1);
as.addAttribute(TextAttribute.FONT, serifFont); 
as.addAttribute(TextAttribute.FOREGROUND, Color.red);


JOptionPane.showMessageDialog(null, as+t2);

I'm not familiar with attributedtext() and this wont work. It does this:

我不熟悉attributedtext(),这行不通。它这样做:

"java.text.AttributedString@479c479cworld"

“java.text.AttributedString@479c479cworld”

Is there a step I'm missing? Is this not the right way? Any suggestions?

有没有我遗漏的步骤?这不是正确的方法吗?有什么建议?

回答by Florian Minges

It should be possible to use html to solve this, ie

应该可以用html来解决这个问题,即

String t = "<html><font color=#ffffdd>Hello</font> world!";

See http://docs.oracle.com/javase/tutorial/uiswing/components/html.htmlfor more info.

有关更多信息,请参阅http://docs.oracle.com/javase/tutorial/uiswing/components/html.html

回答by MadProgrammer

You can pass a Componentto JOptionPane in the message parameter and will use that to display your message.

您可以Component在消息参数中将a 传递给 JOptionPane 并使用它来显示您的消息。

Something like a JLabelor a JPanelwith labels on it.

像 aJLabel或 a 之类的东西JPanel,上面有标签。

UPDATED

更新

JLabel, JPanel and HTML text examples

JLabel、JPanel 和 HTML 文本示例

public class TestOptionPane {

    public static void main(String[] args) {

        JLabel label = new JLabel("Hello");
        label.setForeground(Color.RED);

        JOptionPane.showMessageDialog(null, label);

        JPanel pnl = new JPanel(new GridBagLayout());
        pnl.add(createLabel("The quick"));
        pnl.add(createLabel(" brown ", Color.ORANGE));
        pnl.add(createLabel(" fox "));

        JOptionPane.showMessageDialog(null, pnl);

        String text = "<html>The Quick <span style='color:green'>brown</span> fox</html>";
        JOptionPane.showMessageDialog(null, text);

    }

    public static JLabel createLabel(String text) {

        return createLabel(text, UIManager.getColor("Label.foreground"));

    }

    public static JLabel createLabel(String text, Color color) {

        JLabel label = new JLabel(text);
        label.setForeground(color);

        return label;

    }

}

On the Mac-

在 Mac 上——

JOptionPane Example on Mac

JOptionPane Example on Mac

On Windows -

在 Windows 上 -

JOptionPane example on Windows

JOptionPane example on Windows