JOptionPane如何禁用X?

发布于 2024-11-04 02:20:49 字数 677 浏览 0 评论 0原文

如果用户单击右上角的 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 Can be dereferenced

If the user clicks X on the top right, I don't want anything to happen. What is the code line to make this happen?

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
}

I tried something like a3.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
but I get an error int cannot be dereferenced

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-11-11 02:20:49

除了 Chris 选项之外,请查看 javadocs (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();

Besides Chris option, take a look at the javadocs (http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html) Direct use example.

You can achieve what you have tried with:

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();
甜点 2024-11-11 02:20:49

这种简约的方法怎么样:

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
}

What about this minimalistic approach:

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
}
小姐丶请自重 2024-11-11 02:20:49

此代码可能会解决问题(您可以运行代码来测试它):

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();  
      }  
    });  
  }  
}  

This code might do the trick (you can run the code to test it):

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();  
      }  
    });  
  }  
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文