import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Labe extends JFrame
{
private Label l1 = new Label("red");
private Label l2 = new Label("yellow");
private Label l3 = new Label("blue");public void init()
{
JFrame j = new JFrame();
setLayout(new FlowLayout());
add(l1);add(l2);add(l3);
l1.setBackground(Color.red);
l2.setBackground(Color.yellow);
l3.setBackground(Color.blue);
/*j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
setSize(300,300);
setVisible(true);
}
public static void main(String[] args)
{
Labe l = new Labe();
l.init();
}
}
这个程序为什么把/*j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
注释掉窗口还是可以正常关闭
请高手可以帮忙解释一下
感激不尽...
import java.awt.event.*;
import javax.swing.*;
public class Labe extends JFrame
{
private Label l1 = new Label("red");
private Label l2 = new Label("yellow");
private Label l3 = new Label("blue");public void init()
{
JFrame j = new JFrame();
setLayout(new FlowLayout());
add(l1);add(l2);add(l3);
l1.setBackground(Color.red);
l2.setBackground(Color.yellow);
l3.setBackground(Color.blue);
/*j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
setSize(300,300);
setVisible(true);
}
public static void main(String[] args)
{
Labe l = new Labe();
l.init();
}
}
这个程序为什么把/*j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
注释掉窗口还是可以正常关闭
请高手可以帮忙解释一下
感激不尽...
javac Labe.java
java Labe
然后点窗口上的关闭
现在看看命令行是否能回到正常的提示符下,也可以用任务管理器看看,你运行的进程并没有关闭
这句是说,关闭j这个JFrame时,退出程序。
没有这句,只是关闭JFrame,没退出程序
程序加上也不能正常关闭,因为那个JFrame j = new JFrame();根本就是多于的!改成下面这样就OK了:
另外,引用JAVA API 文档的内容:(注意最后一行的说明!!)
-----------------------------------------------------------------------------
setDefaultCloseOperation
public void setDefaultCloseOperation(int operation)设置用户在此窗体上发起 "close" 时默认执行的操作。必须指定以下选项之一: DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何操作;要求程序在已注册的 WindowListener 对象的 windowClosing 方法中处理该操作。
HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener 对象后自动隐藏该窗体。
DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener 的对象后自动隐藏并释放该窗体。
EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit 方法退出应用程序。仅在应用程序中使用。
默认情况下,该值被设置为 HIDE_ON_CLOSE。 (意思是隐藏,但没有关闭!!)
----------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Labe extends JFrame
{
private Label l1 = new Label("red");
private Label l2 = new Label("yellow");
private Label l3 = new Label("blue"); public void init()
{
setLayout(new FlowLayout());
add(l1);add(l2);add(l3);
l1.setBackground(Color.red);
l2.setBackground(Color.yellow);
l3.setBackground(Color.blue);
setSize(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
Labe l = new Labe();
l.init();
}
}