怎样用JAVA编程编写题目:设计一个窗体,在窗体中有个按钮,单击退出按钮,窗体消失
2013-09-24
展开全部
/** 希望 对你有用 同学 一个简单而 但 很 实用的例子 */import java.awt. * ;
import java.awt.event. * ;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;public class swing extends JFrame { /**
* @param args
*/
JFrame j;
public swing() {
j = new JFrame("close windows");
j.setBounds(100, 100, 100, 100); // 设置 窗体 大小
j.getContentPane().setLayout(new FlowLayout(ABORT)); //设置 布局
JButton jbtn = new JButton();
jbtn.setText("退出");
jbtn.addMouseListener(new MouseAdapter(){ // 鼠标 监听 按下
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.exit(0);
super.mouseClicked(e);
}
});
j.add(jbtn); // 按钮添加到 窗体
j.setVisible(true); // 设置窗体 可见
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
// TODO Auto-generated method stub
new swing();
}
});
}
import java.awt.event. * ;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;public class swing extends JFrame { /**
* @param args
*/
JFrame j;
public swing() {
j = new JFrame("close windows");
j.setBounds(100, 100, 100, 100); // 设置 窗体 大小
j.getContentPane().setLayout(new FlowLayout(ABORT)); //设置 布局
JButton jbtn = new JButton();
jbtn.setText("退出");
jbtn.addMouseListener(new MouseAdapter(){ // 鼠标 监听 按下
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.exit(0);
super.mouseClicked(e);
}
});
j.add(jbtn); // 按钮添加到 窗体
j.setVisible(true); // 设置窗体 可见
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
// TODO Auto-generated method stub
new swing();
}
});
}
2013-09-24
展开全部
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JFrame;
import javax.swing.JOptionPane;public class SimpleFrame {public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize( 300, 200);
this.setResizable( false);
this.addWindowListener(new MyListener(this));
}
private class MyListener extends WindowAdapter
{
JFrame myFrame;
public MyListener( JFrame f)
{
myFrame = f;
}
@Override
public void windowClosing(WindowEvent arg0) {
int test = JOptionPane.showConfirmDialog( myFrame, "确定要退出吗?","退出确认",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if( test == JOptionPane.YES_OPTION)
System.exit(0);
else
return;
}
}
}
import java.awt.event.WindowEvent;import javax.swing.JFrame;
import javax.swing.JOptionPane;public class SimpleFrame {public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame
{
public MyFrame()
{
this.setSize( 300, 200);
this.setResizable( false);
this.addWindowListener(new MyListener(this));
}
private class MyListener extends WindowAdapter
{
JFrame myFrame;
public MyListener( JFrame f)
{
myFrame = f;
}
@Override
public void windowClosing(WindowEvent arg0) {
int test = JOptionPane.showConfirmDialog( myFrame, "确定要退出吗?","退出确认",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if( test == JOptionPane.YES_OPTION)
System.exit(0);
else
return;
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询