2015-05-09 69 views
1

最近我一直在努力使用Nimbus外觀和感覺的Swing項目。我想要設置JOptionPane中的所有按鈕具有相同的大小,但徒勞無功。JOptionPane按鈕大小(Nimbus LAF)

import javax.swing.*; 

public class NimbusTest { 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() {  
       try { 
        for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { 
         if (("Nimbus").equals(info.getName())) { 
          UIManager.setLookAndFeel(info.getClassName()); 
          break; 
         } 
        } 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
       UIManager.put("OptionPane.sameSizeButtons", true);  
       String[] options = new String[]{"--------------------","short","1"}; 
       int option = JOptionPane.showOptionDialog(null, "Nimbus problem", "JOptionPane", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);  
      } 
     }); 
    } 
} 

我想要的是一個帶有三個按鈕的JOptionPane具有相同的大小。但是,我得到了以下結果:

enter image description here

我的代碼UIManager.put("OptionPane.sameSizeButtons", true);似乎被忽略。如果我不想重新創建類似JOptionPane的對話框,我應該如何創建具有相同大小按鈕的JOptionPane?

回答

3

更換

UIManager.put("OptionPane.sameSizeButtons", true); 

UIManager.getLookAndFeelDefaults().put("OptionPane.sameSizeButtons", true); 

它就像一個魅力

enter image description here

+0

謝謝!它現在=) – tony200910041

+0

如何設置像素的大小? –

+0

@EthanJCRubik如果需要,擴展jbutton並覆蓋getpreferedsize –