JOptionPane如何更改标题框的颜色

时间:2018-12-02 06:34:03

标签: java swing joptionpane uimanager

public static void main(String[] args)  {

    UIManager.put("OptionPane.minimumSize",new Dimension(350,200)); 
    UIManager.put("OptionPane.background", Color.gray);
    UIManager.put("Panel.background", Color.lightGray);

    JOptionPane.showMessageDialog(null, "A basic JOptionPane message dialog", "myBox",0);

    System.exit(0);
}

当前,标题框上的背景颜色是白色,我想将其更改为浅灰色,例如Panel.background颜色。有可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码实现所需的内容:

JDialog.setDefaultLookAndFeelDecorated(true);
UIManager.put("OptionPane.minimumSize",new Dimension(350,200));
UIManager.put("OptionPane.background", Color.gray);
UIManager.put("Panel.background", Color.lightGray);
UIManager.put("OptionPane.errorDialog.titlePane.foreground", Color.lightGray);
UIManager.put("OptionPane.errorDialog.titlePane.background", Color.black);
UIManager.put("OptionPane.errorDialog.border.background", Color.white);

JOptionPane.showMessageDialog(null, "A basic JOptionPane message dialog", "myBox",0);

您还可以看到所有可能的UIManager选项。尝试查看此列表,然后选择最适合您的选项:

UIDefaults uiDefaults = UIManager.getDefaults();
Enumeration<Object> keys = uiDefaults.keys();
List<String> allUIManagerConfigs=  new ArrayList<>();

for(;keys.hasMoreElements();){
    Object key = keys.nextElement();
    String value = String.valueOf(key);
    allUIManagerConfigs.add(value);
}

Collections.sort(allUIManagerConfigs);

for(String keyName:allUIManagerConfigs){
    System.out.println(keyName);
}

这段代码的输出是这样的:

AATextInfoPropertyKey
AuditoryCues.allAuditoryCues
AuditoryCues.cueList
AuditoryCues.defaultCueList
AuditoryCues.noAuditoryCues
Button.background
Button.border
Button.darkShadow
Button.defaultButtonFollowsFocus
Button.disabledText
Button.disabledToolBarBorderBackground
Button.focus
Button.focusInputMap
Button.font
Button.foreground
Button.gradient
Button.highlight
Button.light
Button.margin
Button.rollover
Button.rolloverIconType
Button.select
Button.shadow
Button.textIconGap
Button.textShiftOffset
Button.toolBarBorderBackground
ButtonUI
CheckBox.background
CheckBox.border
CheckBox.disabledText
CheckBox.focus
CheckBox.focusInputMap
CheckBox.font
CheckBox.foreground
CheckBox.gradient
CheckBox.icon
CheckBox.margin
CheckBox.rollover
CheckBox.textIconGap
CheckBox.textShiftOffset
CheckBox.totalInsets
CheckBoxMenuItem.acceleratorFont
CheckBoxMenuItem.acceleratorForeground
CheckBoxMenuItem.acceleratorSelectionForeground
CheckBoxMenuItem.arrowIcon
CheckBoxMenuItem.background
CheckBoxMenuItem.border
CheckBoxMenuItem.borderPainted
CheckBoxMenuItem.checkIcon
CheckBoxMenuItem.commandSound
CheckBoxMenuItem.disabledForeground
CheckBoxMenuItem.font
CheckBoxMenuItem.foreground
CheckBoxMenuItem.gradient
CheckBoxMenuItem.margin
CheckBoxMenuItem.selectionBackground
CheckBoxMenuItem.selectionForeground
CheckBoxMenuItemUI
CheckBoxUI
Checkbox.select
ColorChooser.background
ColorChooser.font
ColorChooser.foreground
ColorChooser.swatchesDefaultRecentColor
ColorChooser.swatchesRecentSwatchSize
ColorChooser.swatchesSwatchSize
ColorChooserUI
ComboBox.ancestorInputMap
ComboBox.background
ComboBox.buttonBackground
ComboBox.buttonDarkShadow
ComboBox.buttonHighlight
ComboBox.buttonShadow
ComboBox.disabledBackground
ComboBox.disabledForeground
ComboBox.font
ComboBox.foreground
ComboBox.isEnterSelectablePopup
ComboBox.noActionOnKeyNavigation
ComboBox.selectionBackground
ComboBox.selectionForeground
ComboBox.timeFactor
ComboBoxUI
Desktop.ancestorInputMap
Desktop.background
Desktop.minOnScreenInsets
DesktopIcon.background
DesktopIcon.border
DesktopIcon.font
DesktopIcon.foreground
DesktopIcon.width
DesktopIconUI
DesktopPaneUI
EditorPane.background
EditorPane.border
EditorPane.caretBlinkRate
EditorPane.caretForeground
EditorPane.focusInputMap
EditorPane.font
EditorPane.foreground
EditorPane.inactiveForeground
EditorPane.margin
EditorPane.selectionBackground
EditorPane.selectionForeground
EditorPaneUI
FileChooser.ancestorInputMap
FileChooser.detailsViewIcon
FileChooser.homeFolderIcon
FileChooser.listViewIcon
FileChooser.newFolderIcon
FileChooser.readOnly
FileChooser.upFolderIcon
FileChooser.useSystemExtensionHiding
FileChooser.usesSingleFilePane
FileChooserUI
FileView.computerIcon
FileView.directoryIcon
FileView.fileIcon
FileView.floppyDriveIcon
FileView.hardDriveIcon
FormattedTextField.background
FormattedTextField.border
FormattedTextField.caretBlinkRate
FormattedTextField.caretForeground
FormattedTextField.focusInputMap
FormattedTextField.font
FormattedTextField.foreground
FormattedTextField.inactiveBackground
FormattedTextField.inactiveForeground
FormattedTextField.margin
FormattedTextField.selectionBackground
FormattedTextField.selectionForeground
FormattedTextFieldUI
InternalFrame.activeTitleBackground
InternalFrame.activeTitleForeground
InternalFrame.activeTitleGradient
InternalFrame.border
InternalFrame.borderColor
InternalFrame.borderDarkShadow
InternalFrame.borderHighlight
InternalFrame.borderLight
InternalFrame.borderShadow
InternalFrame.closeIcon
InternalFrame.closeSound
InternalFrame.icon
InternalFrame.iconifyIcon
InternalFrame.inactiveTitleBackground
InternalFrame.inactiveTitleForeground
InternalFrame.maximizeIcon
InternalFrame.maximizeSound
InternalFrame.minimizeIcon
InternalFrame.minimizeSound
InternalFrame.optionDialogBorder
InternalFrame.paletteBorder
InternalFrame.paletteCloseIcon
InternalFrame.paletteTitleHeight
InternalFrame.restoreDownSound
InternalFrame.restoreUpSound
InternalFrame.titleFont
InternalFrameTitlePane.closeButtonOpacity
InternalFrameTitlePane.iconifyButtonOpacity
InternalFrameTitlePane.maximizeButtonOpacity
InternalFrameUI
Label.background
Label.disabledForeground
Label.disabledShadow
Label.font
Label.foreground
LabelUI
List.background
List.cellRenderer
List.dropCellBackground
List.dropLineColor
List.focusCellHighlightBorder
List.focusInputMap
List.focusInputMap.RightToLeft
List.font
List.foreground
List.noFocusBorder
List.selectionBackground
List.selectionForeground
List.timeFactor
ListUI
Menu.acceleratorFont
Menu.acceleratorForeground
Menu.acceleratorSelectionForeground
Menu.arrowIcon
Menu.background
Menu.border
Menu.borderPainted
Menu.cancelMode
Menu.checkIcon
Menu.crossMenuMnemonic
Menu.disabledForeground
Menu.font
Menu.foreground
Menu.margin
Menu.menuPopupOffsetX
Menu.menuPopupOffsetY
Menu.opaque
Menu.preserveTopLevelSelection
Menu.selectionBackground
Menu.selectionForeground
Menu.shortcutKeys
Menu.submenuPopupOffsetX
Menu.submenuPopupOffsetY
MenuBar.background
MenuBar.border
MenuBar.borderColor
MenuBar.font
MenuBar.foreground
MenuBar.gradient
MenuBar.highlight
MenuBar.shadow
MenuBar.windowBindings
MenuBarUI
MenuItem.acceleratorDelimiter
MenuItem.acceleratorFont
MenuItem.acceleratorForeground
MenuItem.acceleratorSelectionForeground
MenuItem.arrowIcon
MenuItem.background
MenuItem.border
MenuItem.borderPainted
MenuItem.checkIcon
MenuItem.commandSound
MenuItem.disabledForeground
MenuItem.font
MenuItem.foreground
MenuItem.margin
MenuItem.selectionBackground
MenuItem.selectionForeground
MenuItemUI
MenuUI
OptionPane.background
OptionPane.border
OptionPane.buttonAreaBorder
OptionPane.buttonClickThreshhold
OptionPane.errorDialog.border.background
OptionPane.errorDialog.titlePane.background
OptionPane.errorDialog.titlePane.foreground
OptionPane.errorDialog.titlePane.shadow
OptionPane.errorIcon
OptionPane.errorSound
OptionPane.font
OptionPane.foreground
OptionPane.informationIcon
OptionPane.informationSound
OptionPane.messageAreaBorder
OptionPane.messageForeground
OptionPane.minimumSize
OptionPane.questionDialog.border.background
OptionPane.questionDialog.titlePane.background
OptionPane.questionDialog.titlePane.foreground
OptionPane.questionDialog.titlePane.shadow
OptionPane.questionIcon
OptionPane.questionSound
OptionPane.warningDialog.border.background
OptionPane.warningDialog.titlePane.background
OptionPane.warningDialog.titlePane.foreground
OptionPane.warningDialog.titlePane.shadow
OptionPane.warningIcon
OptionPane.warningSound
OptionPane.windowBindings
OptionPaneUI
Panel.background
Panel.font
Panel.foreground
PanelUI
PasswordField.background
PasswordField.border
PasswordField.caretBlinkRate
PasswordField.caretForeground
PasswordField.echoChar
PasswordField.focusInputMap
PasswordField.font
PasswordField.foreground
PasswordField.inactiveBackground
PasswordField.inactiveForeground
PasswordField.margin
PasswordField.selectionBackground
PasswordField.selectionForeground
PasswordFieldUI
PopupMenu.background
PopupMenu.border
PopupMenu.consumeEventOnClose
PopupMenu.font
PopupMenu.foreground
PopupMenu.popupSound
PopupMenu.selectedWindowInputMapBindings
PopupMenu.selectedWindowInputMapBindings.RightToLeft
PopupMenuSeparatorUI
PopupMenuUI
ProgressBar.background
ProgressBar.border
ProgressBar.cellLength
ProgressBar.cellSpacing
ProgressBar.cycleTime
ProgressBar.font
ProgressBar.foreground
ProgressBar.horizontalSize
ProgressBar.repaintInterval
ProgressBar.selectionBackground
ProgressBar.selectionForeground
ProgressBar.verticalSize
ProgressBarUI
RadioButton.background
RadioButton.border
RadioButton.darkShadow
RadioButton.disabledText
RadioButton.focus
RadioButton.focusInputMap
RadioButton.font
RadioButton.foreground
RadioButton.gradient
RadioButton.highlight
RadioButton.icon
RadioButton.light
RadioButton.margin
RadioButton.rollover
RadioButton.select
RadioButton.shadow
RadioButton.textIconGap
RadioButton.textShiftOffset
RadioButton.totalInsets
RadioButtonMenuItem.acceleratorFont
RadioButtonMenuItem.acceleratorForeground
RadioButtonMenuItem.acceleratorSelectionForeground
RadioButtonMenuItem.arrowIcon
RadioButtonMenuItem.background
RadioButtonMenuItem.border
RadioButtonMenuItem.borderPainted
RadioButtonMenuItem.checkIcon
RadioButtonMenuItem.commandSound
RadioButtonMenuItem.disabledForeground
RadioButtonMenuItem.font
RadioButtonMenuItem.foreground
RadioButtonMenuItem.gradient
RadioButtonMenuItem.margin
RadioButtonMenuItem.selectionBackground
RadioButtonMenuItem.selectionForeground
RadioButtonMenuItemUI
RadioButtonUI
RootPane.ancestorInputMap
RootPane.colorChooserDialogBorder
RootPane.defaultButtonWindowKeyBindings
RootPane.errorDialogBorder
RootPane.fileChooserDialogBorder
RootPane.frameBorder
RootPane.informationDialogBorder
RootPane.plainDialogBorder
RootPane.questionDialogBorder
RootPane.warningDialogBorder
RootPaneUI
ScrollBar.allowsAbsolutePositioning
ScrollBar.ancestorInputMap
ScrollBar.ancestorInputMap.RightToLeft
ScrollBar.background
ScrollBar.darkShadow
ScrollBar.foreground
ScrollBar.gradient
ScrollBar.highlight
ScrollBar.maximumThumbSize
ScrollBar.minimumThumbSize
ScrollBar.shadow
ScrollBar.thumb
ScrollBar.thumbDarkShadow
ScrollBar.thumbHighlight
ScrollBar.thumbShadow
ScrollBar.track
ScrollBar.trackHighlight
ScrollBar.width
ScrollBarUI
ScrollPane.ancestorInputMap
ScrollPane.ancestorInputMap.RightToLeft
ScrollPane.background
ScrollPane.border
ScrollPane.font
ScrollPane.foreground
ScrollPaneUI
Separator.background
Separator.foreground
Separator.highlight
Separator.shadow
SeparatorUI
Slider.altTrackColor
Slider.background
Slider.focus
Slider.focusGradient
Slider.focusInputMap
Slider.focusInputMap.RightToLeft
Slider.focusInsets
Slider.font
Slider.foreground
Slider.gradient
Slider.highlight
Slider.horizontalSize
Slider.horizontalThumbIcon
Slider.majorTickLength
Slider.minimumHorizontalSize
Slider.minimumVerticalSize
Slider.onlyLeftMouseButtonDrag
Slider.shadow
Slider.tickColor
Slider.trackWidth
Slider.verticalSize
Slider.verticalThumbIcon
SliderUI
Spinner.ancestorInputMap
Spinner.arrowButtonBorder
Spinner.arrowButtonInsets
Spinner.arrowButtonSize
Spinner.background
Spinner.border
Spinner.editorAlignment
Spinner.editorBorderPainted
Spinner.font
Spinner.foreground
SpinnerUI
SplitPane.ancestorInputMap
SplitPane.background
SplitPane.border
SplitPane.centerOneTouchButtons
SplitPane.darkShadow
SplitPane.dividerFocusColor
SplitPane.dividerSize
SplitPane.highlight
SplitPane.oneTouchButtonsOpaque
SplitPane.shadow
SplitPaneDivider.border
SplitPaneDivider.draggingColor
SplitPaneUI
TabbedPane.ancestorInputMap
TabbedPane.background
TabbedPane.borderHightlightColor
TabbedPane.contentAreaColor
TabbedPane.contentBorderInsets
TabbedPane.contentOpaque
TabbedPane.darkShadow
TabbedPane.focus
TabbedPane.focusInputMap
TabbedPane.font
TabbedPane.foreground
TabbedPane.highlight
TabbedPane.labelShift
TabbedPane.light
TabbedPane.selectHighlight
TabbedPane.selected
TabbedPane.selectedLabelShift
TabbedPane.selectedTabPadInsets
TabbedPane.selectionFollowsFocus
TabbedPane.shadow
TabbedPane.tabAreaBackground
TabbedPane.tabAreaInsets
TabbedPane.tabInsets
TabbedPane.tabRunOverlay
TabbedPane.tabsOpaque
TabbedPane.tabsOverlapBorder
TabbedPane.textIconGap
TabbedPane.unselectedBackground
TabbedPaneUI
Table.ancestorInputMap
Table.ancestorInputMap.RightToLeft
Table.ascendingSortIcon
Table.background
Table.descendingSortIcon
Table.dropCellBackground
Table.dropLineColor
Table.dropLineShortColor
Table.focusCellBackground
Table.focusCellForeground
Table.focusCellHighlightBorder
Table.font
Table.foreground
Table.gridColor
Table.scrollPaneBorder
Table.selectionBackground
Table.selectionForeground
Table.sortIconColor
TableHeader.ancestorInputMap
TableHeader.background
TableHeader.cellBorder
TableHeader.focusCellBackground
TableHeader.font
TableHeader.foreground
TableHeaderUI
TableUI
TextArea.background
TextArea.border
TextArea.caretBlinkRate
TextArea.caretForeground
TextArea.focusInputMap
TextArea.font
TextArea.foreground
TextArea.inactiveForeground
TextArea.margin
TextArea.selectionBackground
TextArea.selectionForeground
TextAreaUI
TextField.background
TextField.border
TextField.caretBlinkRate
TextField.caretForeground
TextField.darkShadow
TextField.focusInputMap
TextField.font
TextField.foreground
TextField.highlight
TextField.inactiveBackground
TextField.inactiveForeground
TextField.light
TextField.margin
TextField.selectionBackground
TextField.selectionForeground
TextField.shadow
TextFieldUI
TextPane.background
TextPane.border
TextPane.caretBlinkRate
TextPane.caretForeground
TextPane.focusInputMap
TextPane.font
TextPane.foreground
TextPane.inactiveForeground
TextPane.margin
TextPane.selectionBackground
TextPane.selectionForeground
TextPaneUI
TitledBorder.border
TitledBorder.font
TitledBorder.titleColor
ToggleButton.background
ToggleButton.border
ToggleButton.darkShadow
ToggleButton.disabledText
ToggleButton.focus
ToggleButton.focusInputMap
ToggleButton.font
ToggleButton.foreground
ToggleButton.gradient
ToggleButton.highlight
ToggleButton.light
ToggleButton.margin
ToggleButton.select
ToggleButton.shadow
ToggleButton.textIconGap
ToggleButton.textShiftOffset
ToggleButtonUI
ToolBar.ancestorInputMap
ToolBar.background
ToolBar.border
ToolBar.borderColor
ToolBar.darkShadow
ToolBar.dockingBackground
ToolBar.dockingForeground
ToolBar.floatingBackground
ToolBar.floatingForeground
ToolBar.font
ToolBar.foreground
ToolBar.highlight
ToolBar.isRollover
ToolBar.light
ToolBar.nonrolloverBorder
ToolBar.rolloverBorder
ToolBar.separatorSize
ToolBar.shadow
ToolBarSeparatorUI
ToolBarUI
ToolTip.background
ToolTip.backgroundInactive
ToolTip.border
ToolTip.borderInactive
ToolTip.font
ToolTip.foreground
ToolTip.foregroundInactive
ToolTip.hideAccelerator
ToolTipManager.enableToolTipMode
ToolTipUI
Tree.ancestorInputMap
Tree.background
Tree.changeSelectionWithFocus
Tree.closedIcon
Tree.collapsedIcon
Tree.drawsFocusBorderAroundIcon
Tree.dropCellBackground
Tree.dropLineColor
Tree.editorBorder
Tree.expandedIcon
Tree.focusInputMap
Tree.focusInputMap.RightToLeft
Tree.font
Tree.foreground
Tree.hash
Tree.leafIcon
Tree.leftChildIndent
Tree.line
Tree.lineTypeDashed
Tree.openIcon
Tree.paintLines
Tree.rightChildIndent
Tree.rowHeight
Tree.scrollsOnExpand
Tree.selectionBackground
Tree.selectionBorderColor
Tree.selectionForeground
Tree.textBackground
Tree.textForeground
Tree.timeFactor
TreeUI
Viewport.background
Viewport.font
Viewport.foreground
ViewportUI
activeCaption
activeCaptionBorder
activeCaptionText
control
controlDkShadow
controlHighlight
controlLtHighlight
controlShadow
controlText
desktop
html.missingImage
html.pendingImage
inactiveCaption
inactiveCaptionBorder
inactiveCaptionText
info
infoText
menu
menuText
scrollbar
text
textHighlight
textHighlightText
textInactiveText
textText
window
windowBorder
windowText

如果在这里找不到适合您问题的任何选项,则应获取JOption窗格的JDialog并编写UI hack代码以实现所需的功能。您可以使用以下代码从JOptionPane获取JDialog:

JOptionPane optionPane = new JOptionPane("A basic JOptionPane message dialog",JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = optionPane.createDialog(null, "myBox");