2011-04-27 36 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 
}