Resize e font joptionpane

Can you change the font and size of the text JOptionPane? I tried and it only works if I "run the file" on this particular java class. if you run the whole project, this will not change the font. I would change only a specific JOptionPane not all

here is the code

 UIManager.put("OptionPane.messageFont", new FontUIResource(new Font(  
          "Arial", Font.BOLD, 18)));       
 JOptionPane.showMessageDialog(null,"MESSAGE","ERROR",JOptionPane.WARNING_MESSAGE);       

Thank you

+4
source share
3 answers

It is very simple. The JOption panel accepts not only strings, but also components. Thus, you can create a label, set its font and use it as a message.

JLabel label = new JLabel("MESSAGE");
label.setFont(new Font("Arial", Font.BOLD, 18));
JOptionPane.showMessageDialog(null,label,"ERROR",JOptionPane.WARNING_MESSAGE);

I do not understand why no one answered this question before

+6
source

So we will use:

UIManager.getLookAndFeelDefaults(). put ( "OptionPane.messageFont", ( "Arial", Font.BOLD, 14));   UIManager.getLookAndFeelDefaults(). Put ( "OptionPane.buttonFont", ( "Arial", Font.PLAIN, 12));    >

UIManager.put("OptionPane.messageFont", new Font("Arial", Font.BOLD, 14));
UIManager.put("OptionPane.buttonFont", new Font("Arial", Font.PLAIN, 12));

JOptionPane. main.

, , DOC UIManager.

UIManager UIDefaults. :

. Swing ; .

. ( setLookAndFeel()). getLookAndFeelDefaults().

. Swing. get , . , UIManager.getString( "Table.foreground" ) . "Table.foreground", , , . , getDefaults UIDefaults . , UIManager.getDefaults(). GetString ( "Table.foreground" ) UIManager.getString( "Table.foreground" ). . defaults UIDefaults , .

, . UIManager.put(Object key, Object value) - , .

public static Object put ( , )

. getDefaults(). Put (, ). , .

:

key - ,

value - ; UIDefaults , null

: , UIDefaults.put(java.lang.Object, java.lang.Object)

:

NullPointerException - null

, : , JOptionPane.

JOptionPane :

http://www.java2s.com/Tutorial/Java/0240__Swing/CustomizingaJOptionPaneLookandFeel.htm

Property String                                 Object Type

OptionPane.actionMap                            ActionMap
OptionPane.background                           Color
OptionPane.border                               Border
OptionPane.buttonAreaBorder                     Border
OptionPane.buttonClickThreshhold                Integer
OptionPane.buttonFont                           Font
OptionPane.buttonOrientation                    Integer
OptionPane.buttonPadding                        Integer
OptionPane.cancelButtonMnemonic                 String
OptionPane.cancelButtonText                     String
OptionPane.cancelIcon                           Icon
OptionPane.errorDialog.border.background        Color
OptionPane.errorDialog.titlePane.background     Color
OptionPane.errorDialog.titlePane.foreground     Color
OptionPane.errorDialog.titlePane.shadow         Color
OptionPane.errorIcon                            Icon
OptionPane.errorSound                           String
OptionPane.font                                 Font
OptionPane.foreground                           Color
OptionPane.informationIcon                      Icon
OptionPane.informationSound                     String
OptionPane.inputDialogTitle                     String
OptionPane.isYesLast                            Boolean
OptionPane.messageAnchor                        Integer
OptionPane.messageAreaBorder                    Border
OptionPane.messageFont                          Font
OptionPane.messageForeground                    Color
OptionPane.messageDialogTitle                   String
OptionPane.minimumSize                          Dimension
OptionPane.noButtonMnemonic                     String
OptionPane.noButtonText                         String
OptionPane.noIcon                               Icon
OptionPane.okButtonMnemonic                     String
OptionPane.okButtonText                         String
OptionPane.okIcon                               Icon
OptionPane.questionDialog.border.background     Color
OptionPane.questionDialog.titlePane.background  Color
OptionPane.questionDialog.titlePane.foreground  Color
OptionPane.questionDialog.titlePane.shadow      Color
OptionPane.questionIcon                         Icon
OptionPane.questionSound                        String
OptionPane.sameSizeButtons                      Boolean
OptionPane.separatorPadding                     Integer
OptionPane.setButtonMargin                      Boolean
OptionPane.titleText                            String
OptionPane.warningDialog.border.background      Color
OptionPane.warningDialog.titlePane.background   Color
OptionPane.warningDialog.titlePane.foreground   Color
OptionPane.warningDialog.titlePane.shadow       Color
OptionPane.warningIcon                          Icon
OptionPane.warningSound                         String
OptionPane.windowBindings                       Object[ ]
OptionPane.yesButtonMnemonic                    String
OptionPane.yesButtonText                        String
OptionPane.yesIcon                              Icon
OptionPaneUI                                    String
+2

JOptionPane. , html, , <font> CSS.

<font>.

JOptionPane.showMessageDialog(this, 
        "<html><font face='Calibri' size='15' color='red'>Hello");

font tag

CSS.

JOptionPane.showMessageDialog(this, 
        "<html><h1 style='font-family: Calibri; font-size: 36pt;'>Hello");

using css

+1

All Articles