Я пробовал так много вещей. ( решение aterai ) спасибо aterai за хорошее решение. Он отлично работает для фона. Но все же у меня есть некоторые требования, чтобы выполнить это, я использовал другой подход. в котором я могу изменить все отображаемые на JoptionPane.
Для одного варианта. (Диалог сообщений)
public static void messagePane(Component parentComponent, String msg, String moduleTitle, int msgType) {
JDialog dialog;
JPanel panel = new JPanel();
panel.setBackground(new Color(184, 174, 245));
JDialog.setDefaultLookAndFeelDecorated(true);
UIManager.put("OptionPane.background", new Color(168, 156, 240));
GroupLayout groupLayout = new GroupLayout(panel);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
panel.setLayout(groupLayout);
JLabel label = new JLabel("<html>" + msg.replaceAll("\n", "<br>"));
label.setFont(PANE_FONT);
ImageIcon image = getImageIcon(msgType);
label.setIcon(image);
MyButton button = new MyButton("OK");
button.setFont(PANE_BTN_FONT);
button.setBackground(new Color(50, 79, 107));
button.setHoverColor(new Color(147, 170, 191));
button.setPressedColor(new Color(255, 255, 255));
button.setMaximumSize(new Dimension(60, 40));
groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.CENTER).addComponent(label).addComponent(button)));
groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(label))
.addGroup(groupLayout.createParallelGroup(Alignment.CENTER).addComponent(button)));
JOptionPane optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.NO_OPTION, null,
new Object[] {}, null);
dialog = optionPane.createDialog(moduleTitle);
dialog.setBackground(new Color(29, 83, 99));
button.addActionListener(e -> dialog.dispose());
dialog.setVisible(true);
}
Для нескольких вариантов. (Диалог параметров)
public static int optionPane(Component parentComponent, String msg, String moduleTitle, Object[] options) {
JDialog dialog;
JPanel panel = new JPanel();
panel.setBackground(new Color(184, 174, 245));
JDialog.setDefaultLookAndFeelDecorated(true);
UIManager.put("OptionPane.background", new Color(168, 156, 240));
GroupLayout groupLayout = new GroupLayout(panel);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
panel.setLayout(groupLayout);
JLabel label = new JLabel("<html>" + msg.replaceAll("\n", "<br>"));
label.setFont(PANE_FONT);
ImageIcon image = getImageIcon(JOptionPane.WARNING_MESSAGE);
label.setIcon(image);
ParallelGroup parallelGroup1 = groupLayout.createParallelGroup(Alignment.LEADING);
SequentialGroup sequentialGroup1 = groupLayout.createSequentialGroup();
LinkedList<MyButton> btnList = new LinkedList<MyButton>();
if (options instanceof String[] || options instanceof Integer[]) {
MyButton button;
for (Object option : options) {
button = new MyButton((String) option);
button.setFont(PANE_BTN_FONT);
button.setMaximumSize(new Dimension(60, 40));
button.setBackground(new Color(82, 122, 161));
button.setHoverColor(new Color(147, 170, 191));
button.setPressedColor(new Color(255, 255, 255));
btnList.add(button);
parallelGroup1.addComponent(button);
sequentialGroup1.addComponent(button);
}
}
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.CENTER)
.addGroup(groupLayout.createSequentialGroup().addComponent(label))
.addGroup(sequentialGroup1))));
groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
groupLayout.createSequentialGroup().addComponent(label).addGap(15, 20, 25).addGroup(parallelGroup1)));
JOptionPane optionPane = new JOptionPane(panel, JOptionPane.PLAIN_MESSAGE, JOptionPane.NO_OPTION, null,
new Object[] {}, null);
dialog = optionPane.createDialog(moduleTitle);
dialog.setBackground(new Color(29, 83, 99));
int i = 0;
for (MyButton button : btnList) {
Integer in = new Integer(i);
button.addActionListener(e -> {
selected = in;
dialog.dispose();
});
i++;
}
dialog.setVisible(true);
return selected;
}
Вот мое решение image