JAVAswing怎么写一个按钮提交数据后关闭本窗口? 100
2个回答
2016-03-28
展开全部
用到JOptionPane.showConfirmDialog方法,showConfirmDialog 的返回类型是int,定义一个int类型的变量接收返回值,然后根据业务逻辑判断,代码看不懂,可以自行百度搜索或问同事,编程注重思想。
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class JButtonTest extends JFrame
{
public static void main ( String[] args )
{
JButtonTest b = new JButtonTest ();
b.addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
int exi = JOptionPane.showConfirmDialog (null, "要退出该程序吗?", "友情提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (exi == JOptionPane.YES_OPTION)
{
System.exit (0);
}
else
{
return;
}
}
});
b.setSize (300, 300);
b.setLocationRelativeTo (null);
b.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);
b.setVisible (true);
}
}
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class JButtonTest extends JFrame
{
public static void main ( String[] args )
{
JButtonTest b = new JButtonTest ();
b.addWindowListener (new WindowAdapter ()
{
@Override
public void windowClosing ( WindowEvent e )
{
int exi = JOptionPane.showConfirmDialog (null, "要退出该程序吗?", "友情提示", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (exi == JOptionPane.YES_OPTION)
{
System.exit (0);
}
else
{
return;
}
}
});
b.setSize (300, 300);
b.setLocationRelativeTo (null);
b.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);
b.setVisible (true);
}
}
追问
点关闭时还提示要退出程序,我是想直接退出程序
2016-03-28
展开全部
swing里有个dispose方法调用即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询