JOPTIONPANE Wie deaktiviert man das X?
-
25-10-2019 - |
Frage
Wenn der Benutzer oben rechts klickt, möchte ich nicht, dass etwas passiert. Was ist die Codezeile, damit dies geschieht?
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
}
Ich habe so etwas wie a3.setDefaultCloseOperation (windowConstants.do_nothing_on_close) ausprobiert; Aber ich bekomme einen Fehler int, in dem nicht derenferenziert werden kann
Lösung
Schauen Sie sich neben Chris Option die Javadocs an (siehe Javadocs (http://download.oracle.com/javase/6/docs/api/javax/swing/joptionpane.html) Beispiel für direktes Gebrauch.
Sie können das erreichen, was Sie ausprobiert haben:
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();
Andere Tipps
Was ist mit diesem minimalistischen Ansatz:
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
}
Dieser Code kann den Trick ausführen (Sie können den Code ausführen, um ihn zu testen):
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();
}
});
}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow