2014-09-26 139 views
1

我已經使用UIManager設置修改了JOptionPane的字體,但我無法找到屬性來更改按鈕屬性。 例如。我想刪除文本邊框,如下所示。另外我想刪除Y和N下的下劃線,如下所示。更改JOptionPane按鈕屬性和背景顏色

enter image description here

而且,我可以改變的JOptionPane的背景顏色從這個灰白色別的東西?

+0

實際上,這可以很容易,你可以仔細看看['JOptionPane.showOptionDialog(Component parentComponent,Object message,String title,int optionType,int messageType,Icon icon, Object [] options,Object initialValue)'](http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang。對象,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object [],%20java.lang.Object)),這將允許您傳遞自定義按鈕'選項'參數 – MadProgrammer 2014-09-26 12:13:44

+0

但是你會負責管理對話狀態,這不是一件容易的事情 – MadProgrammer 2014-09-26 12:14:04

+0

作爲[示例](http://stackoverflow.com/questions/14591089/joptionpane-passing-custom-按鈕/ 14591165#14591165) – MadProgrammer 2014-09-26 12:15:03

回答

0

你可以改變個人的對話:

你需要一個customJDialog和一個自定義的JOptionPane。按鈕強調匹配助記符的第一個字母。將其更改爲未使用的內容。矩形是關於按鈕焦點。

JDialog dialog = new JDialog(); 

JOptionPane pane = new JOptionPane("Some Message", 
JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_OPTION); 

List<JButton> list = SwingUtils.getDescendantsOfType(JButton.class, pane); 
//this method is part of the SwingUtils. It is free to use. You can download it from the link. 

pane.setBackground(Color.green); //this does not completely change the background. I am not sure how you can change it completely 

for (JButton b : list) { 
    b.setMnemonic(0); //call it with an int or char that does not exist in your button. 
    b.setFocusable(false); //removes the rectangle 
    b.setBackground(Color.red); 
    //you can do more since you have direct access to the button 
} 

如果你有Java自定義沒有經驗的JOptionPane檢查了這一點的按鈕操作:Pressing Yes/No on embedded JOptionPane has no effect

SwingUtil鏈接:http://tips4java.wordpress.com/2008/11/13/swing-utils/

如果您想將代碼放到圖書館閱讀:http://www.programcreek.com/2011/07/build-a-java-library-for-yourself/

+0

如果它有效,這將是非常尷尬的!謝謝..我會盡快嘗試這個:) – Gagan93 2014-12-27 15:01:13