2012-01-30 80 views
10

Tôi đã thêm JOptionPane vào ứng dụng của mình nhưng tôi không biết cách thay đổi màu nền thành trắng?Làm thế nào để thay đổi màu nền của JOptionPane?

`int option = JOptionPane.showConfirmDialog(bcfiDownloadPanel, 
      new Object[]{"Host: " + source, panel}, 
      "Authorization Required", 
      JOptionPane.OK_CANCEL_OPTION, 
      JOptionPane.PLAIN_MESSAGE 
    ); 

    if (option == JOptionPane.OK_OPTION) {     }` 

Trả lời

14

Bằng việc sử dụng lớp UIManager

import javax.swing.UIManager; 

UIManager UI=new UIManager(); 
UI.put("OptionPane.background",new ColorUIResource(255,0,0)); 
UI.put("Panel.background",new ColorUIResource(255,0,0)); 

hoặc

UIManager UI=new UIManager(); 
UI.put("OptionPane.background", Color.white); 
UI.put("Panel.background", Color.white); 

JOptionPane.showMessageDialog(null,"Text","SetColor",JOptionPane.INFORMATION_MESSAGE); 
+0

+1 cho cách đơn giản – mKorbel

+5

Lưu ý rằng điều này sẽ thay đổi nền cho tất cả các trường hợp. – mre

+0

Có thể thêm nút tùy chỉnh của tôi vào đó không? – itro

1

Sử dụng một cái gì đó như thế này để thay đổi màu nền chỉ dành riêng cho một hiển thị thông điệp này và không phải là toàn bộ hệ thống ...

Object paneBG = UIManager.get("OptionPane.background"); 
    Object panelBG = UIManager.get("Panel.background"); 
    UIManager.put("OptionPane.background", new Color(...)); 
    UIManager.put("Panel.background", new Color(...)); 

    int ret = messageBox(msg, null, (short)type); 

    UIManager.put("OptionPane.background", paneBG); 
    UIManager.put("Panel.background", panelBG); 
+0

Phương thức "MessageBox()" là gì? –

3

JOptionPane image

Đối với bất kỳ ai gặp vấn đề trong hình ảnh, tôi đã tìm thấy/điều chỉnh một giải pháp. Trên hệ thống của tôi, tôi nhận được kết quả đó, cho dù tôi đã sử dụng giải pháp UIManager như những người khác đã đăng hay đã tạo ra một JDialog và đã sử dụng jd.getContentPane(). SetBackground (Color.white). Vì vậy, đây là công việc xung quanh tôi đã đưa ra, nơi bạn có vòng lặp đệ quy qua từng thành phần trong JOptionPane, và thiết lập màu nền của mỗi JPanel:

private void getComponents(Container c){ 

    Component[] m = c.getComponents(); 

    for(int i = 0; i < m.length; i++){ 

     if(m[i].getClass().getName() == "javax.swing.JPanel") 
      m[i].setBackground(Color.white); 

     if(c.getClass().isInstance(m[i])); 
      getComponents((Container)m[i]); 
    } 
} 

Trong mã của bạn, nơi bạn muốn có được thông báo pop-up , một cái gì đó dọc theo các dòng:

pane = new JOptionPane("Your message here", 
       JOptionPane.PLAIN_MESSAGE ,JOptionPane.DEFAULT_OPTION); 
     getComponents(pane); 
     pane.setBackground(Color.white); 
     jd = pane.createDialog(this, "Message"); 
     jd.setVisible(true); 

Nơi trước đây JOptionPane paneJDialog jd trước đây đã được tạo. Hy vọng điều này sẽ giúp bất cứ ai có vấn đề đó.

+0

Cảm ơn bạn rất nhiều –

+0

Đây là phản hồi tốt nhất –

0

Sử dụng mã này nếu bạn gặp vấn đề tương tự như erik k atwood. Điều này giải quyết được vấn đề:

UIManager.put("OptionPane.background", Color.WHITE); 
UIManager.getLookAndFeelDefaults().put("Panel.background", Color.WHITE); 
Các vấn đề liên quan