formatting text in jdialog box

后端 未结 2 720
醉话见心
醉话见心 2020-12-01 23:29

I have a JOptionPane:

JOptionPane.showMessageDialog(null, text);

The text is a sting:

String text = \"Hello world.\"
         


        
2条回答
  •  悲哀的现实
    2020-12-01 23:38

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

    Something like a JLabel or a JPanel with labels on it.

    UPDATED

    JLabel, JPanel and HTML text examples

    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 = "The Quick brown fox";
            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-

    JOptionPane Example on Mac

    On Windows -

    JOptionPane example on Windows

提交回复
热议问题