2011-04-27 77 views
1

如果用戶點擊右上角的X,我不希望發生任何事情。什麼是代碼行來實現這一點?JOptionPane如何禁用X?

Object [] options1 = {"Go Back", "Accept"}; 
int a3 =JOptionPane.showOptionDialog(null,"Mean arterial pressure restored.\nReassess all vitals STAT.", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, options1[0]); 

if(a3 == JOptionPane.CLOSED_OPTION) 
{ 
//what should i put here? if user X out, I want no response (DO_NOTHING_ON_CLOSE) 
} 

if(a3 == JOptionPane.YES_OPTION) 
{ 
// doing something else 
} 

if (a3 == JOptionPane.NO_OPTION) 
{ 
//doing something else 
} 

我想是這樣a3.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 但我得到一個錯誤INT無法提領

+1

你爲什麼要這麼做?從代碼片段看來,您可能會將其視爲與「NO_OPTION」相同。通常,當我關閉一個對話框時,我希望它將該操作視爲取消操作。 – 2011-04-27 23:59:55

回答

3

此外克里斯選項,看一看的javadoc(http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html )直接使用示例。

可以達到什麼樣的你有嘗試:

Object [] options1 = {"Go Back", "Accept"}; 
JOptionPane jop = new JOptionPane("Mean arterial pressure restored.\nReassess all vitals STAT.", JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_OPTION, null, options1, options1[0]); 
JDialog dialog = jop.createDialog(null, "Title"); 
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); 
// In real code, you should invoke this from AWT-EventQueue using invokeAndWait() or something 
dialog.setVisible(true); 
// and would cast in a safe manner 
String a3 = (String) jop.getValue(); 
if (a3.equals("Accept")) { 

} else if (a3.equals("Go Back")) { 

} 
// don't forget to dispose of the dialog 
dialog.dispose(); 
+0

謝謝,它的工作原理。但是有一個問題。即使JOptionPane設置爲PLAIN_MESSAGE,對話框也會獲取錯誤圖片。當我將JOptionPane.YES_NO_OPTION更改爲JOptionPane.NO_OPTION而不是錯誤圖標圖片時,我得到了信息圖標圖片。有沒有辦法讓圖標完全不出現? – razshan 2011-04-28 16:12:41

+0

@razshan。我的錯。我改變了參數順序。現在一切都應該按預期工作。 – 2011-04-28 16:19:11

0

此代碼可能做的伎倆(可以運行代碼來測試它):

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
class Testing 
{ 
    public void buildGUI() 
    { 
    JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame f = new JFrame(); 
    f.setResizable(false); 
    removeMinMaxClose(f); 
    JPanel p = new JPanel(new GridBagLayout()); 
    JButton btn = new JButton("Exit"); 
    p.add(btn,new GridBagConstraints()); 
    f.getContentPane().add(p); 
    f.setSize(400,300); 
    f.setLocationRelativeTo(null); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setVisible(true); 
    btn.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent ae){ 
     System.exit(0); 
     } 
    }); 
    } 
    public void removeMinMaxClose(Component comp) 
    { 
    if(comp instanceof AbstractButton) 
    { 
     comp.getParent().remove(comp); 
    } 
    if (comp instanceof Container) 
    { 
     Component[] comps = ((Container)comp).getComponents(); 
     for(int x = 0, y = comps.length; x < y; x++) 
     { 
     removeMinMaxClose(comps[x]); 
     } 
    } 
    } 
    public static void main(String[] args) 
    { 
    SwingUtilities.invokeLater(new Runnable(){ 
     public void run(){ 
     new Testing().buildGUI(); 
     } 
    }); 
    } 
} 
1

這個怎麼樣簡約的做法:

Object [] options1 = {"Go Back", "Accept"}; 

do { 
    a3 = JOptionPane.showOptionDialog(null,"Mean arterial pressure restored.\nReassess all vitals STAT.", "Title", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options1, options1[0]); 
} while(a3 == JOptionPane.CLOSED_OPTION); 

if (a3 == JOptionPane.YES_OPTION) { 
    // doing something else 
} 

if (a3 == JOptionPane.NO_OPTION) { 
    //doing something else 
}