JOptionPane背景色

问题描述 投票:0回答:2

Here is the JOptionPane which i tried

这是我什至尝试使用UIManager的Panel.background属性尝试的代码,但问题是,它正在更改我在项目中使用的所有面板的背景。

msgLabel.setFont(StoreAction.STORE_FONT);
JPanel dialogPanel = new JPanel();
dialogPanel.setBackground(Color.lightGray);
dialogPanel.add(msgLabel);
JOptionPane.showOptionDialog(null, dialogPanel, moduleTitle, JOptionPane.NO_OPTION, msgType, null, StoreGUIConstants.OPTIONS, StoreGUIConstants.OPTIONS[0]);```

java swing user-interface joptionpane
2个回答
0
投票

[默认JOptionPane由名为JPanelOptionPane.messageAreaOptionPane.realBodyOptionPane.separatorOptionPane.bodyOptionPane.buttonArea组成,因此您可能希望将它们全部设置为setOpaque(false)

  • HierarchyListener添加msgLabel以获取显示事件。
  • 使用SwingUtilities.getAncestorOfClass(JOptionPane.class, msgLabel)方法获取作为JOptionPane父级的msgLabel
  • JPanel中查找子JOptionPane并设置setOpaque(false)
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.util.stream.Stream;
import javax.swing.*;

public class OptionPaneBackgroundTest {
  private Component makeUI() {
    UIManager.put("OptionPane.background", Color.LIGHT_GRAY);

    JLabel msgLabel = new JLabel("<html>Do you really want to delete<br>user(1/user) from your system.");
    msgLabel.addHierarchyListener(e -> {
      Component c = e.getComponent();
      if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && c.isShowing()) {
        stream(SwingUtilities.getAncestorOfClass(JOptionPane.class, c))
            .filter(JPanel.class::isInstance)
            .map(JPanel.class::cast) // TEST: .peek(cc -> System.out.println(cc.getName()))
            .forEach(pnl -> pnl.setOpaque(false));
      }
    });

    JButton button = new JButton("show");
    button.addActionListener(e -> JOptionPane.showMessageDialog(
        button.getRootPane(), msgLabel, "Remove User", JOptionPane.WARNING_MESSAGE));

    JPanel p = new JPanel(new GridBagLayout());
    p.add(button);
    return p;
  }

  private static Stream<Component> stream(Container parent) {
    return Stream.of(parent.getComponents())
        .filter(Container.class::isInstance)
        .map(c -> stream((Container) c))
        .reduce(Stream.of(parent), Stream::concat);
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new OptionPaneBackgroundTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

0
投票

我尝试了很多事情。 (aterai's solution)感谢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;
}

Here is my solution image

© www.soinside.com 2019 - 2024. All rights reserved.