请java高手帮忙解答: dispose()与setVisible()的问题

我知道dispose()一个窗体后,setVisible(true)能够将此窗体再次显示出来,但是问题是: s etVisible(true)再次显示窗体时,窗体的构造函数是否会被调用.如果不会被调用,那么有什么办法可以强制调用?
还有一个问题,有什么办法可以防止窗体的重复实例化
比如说我现在有个Button ,响应事件是new一个窗体,我要达到的效果是,如果再次点击这个Button,如果这个窗体正在屏幕上显示,则不实例新的,如果这个窗体已经被dispose(),则实例一个新的窗体. 注意:我的所有窗体都只有两个状态 未被dispose()的和被dispose()的,这里根本没有setVisible()的事,请回答者不要把setVisible拿进来说,希望我说清楚问题了,谢谢! 回答的符合题意的,50分奉上;照搬API描述的,讲这几个方法的作用的 就不用回答了 谢谢

这个问题很简单 。 。、

1.首先 一个窗体 如果dispose()以后,如果,用窗体对象.setVisible(true);能够显示的话,
说明你没设置窗体的默认关闭方式 。。 具体你可以看看 setDefaultCloseOperation();这个方法的API说明 。 如果,你没有设置窗体关闭方式 。 点击关闭窗体,或是程序调用dispose() 内部实现方式,都只是把窗体隐藏 == setVisible (false);

2. 构造方法,只能是通过 new 窗体对象来调用 。。
引申上面的问题 。 。 。 如果你改变窗体默认关闭方式 手动设置 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
在窗体里 调用 dispose(); 就会完全关闭窗体 ,并释放窗体资源 (不能通过setVisible重新显示)

3. 这个很简单 。 。 你考虑单利模式 就行了 。。 把窗体类做成单利模式 。 。
单利模式的意义就是 ,在程序的运行时,类实例只会存在一个。 也就是说,只要你没释放这个类资源 。 它只存在一个。 。

写的太多了。 。 单利模式,你自己百度一下吧。。总的来说很简单

私有构造方法。。
定义静态内部实例。
提供公有静态 创建并返回实例的方法

以下资料,可以掠过。 以下为 setDefaultCloseOperation ()方法API解释。
-----------------------------------------------------------------------------------
DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何操作;要求程序在已注册的 WindowListener 对象的 windowClosing 方法中处理该操作。
HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener 对象后自动隐藏该窗体。
DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener 的对象后自动隐藏并释放该窗体。
EXIT_ON_CLOSE(在 JFrame 中定义):
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-02-05
setVisible()可以重复调用!
不用那么麻烦,用JDialog就可以,利用JDialog对象的setModal(true)方法就可以达到你要的效果,而且你也不用考虑dispose,因为它非常好用,不信看看API
第2个回答  2011-02-06
给你改好了,试试吧。
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class project1_1 extends Dialog //implements ActionListener
{
Label L1 = new Label("It must be 4 figure");
Label L2 = new Label("Congratulation, you are right! ");
Label L3 = new Label("It must be figure");
Label L4 = new Label("There should not be the same number");
Button b0 = new Button("sure");

public void success() {
setTitle("success");

add(L2,BorderLayout.CENTER);
this.validate();
this.setVisible(true);
}

public void error() {
setTitle("Error");
add(L1,BorderLayout.CENTER);
this.validate();
this.setVisible(true);
}

public void flase() {
setTitle("False");
add(L3,BorderLayout.CENTER);
this.validate();
this.setVisible(true);
}

public void same() {
setTitle("Same");
add(L4,BorderLayout.CENTER);
this.validate();
this.setVisible(true);
}

public project1_1(Frame owner, TextArea ta1, String a) {

super(owner, "");
this.setLayout(new BorderLayout());
//add(b0);
//setLayout(null);
setResizable(false);
this.setSize(200, 200);
this.setLocation(owner.getLocationOnScreen().x+50,owner.getLocation().y+150);
setVisible(true);
//b0.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
project1_1.this.dispose();
}
});

}
/*public void actionPerformed(ActionEvent e){
project1_1.this.dispose();
}*/

}

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JOptionPane;
class caishuzi extends Frame implements ActionListener {

String templateStr=this.getRandomNumString();//原始数字串
private Label L1 = new Label("please input your guess");
TextArea tf1 = new TextArea("");
Label L2 = new Label("Here will show the result");
TextArea tf2 = new TextArea("");
Button b1 = new Button("Yes");
Button b2 = new Button("Exit");
Button b3 = new Button("clean");
Panel p1 = new Panel(new GridLayout(1, 3));
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();

public caishuzi(int a, int b, int c, int d) {
super("The small game of guess number");
setLayout(new GridLayout(5, 1));
setBounds(a, b, c, d);
add(L1);
add(tf1);
add(L2);
add(tf2);
p2.add(b1);
p3.add(b2);
p4.add(b3);
p1.add(p2);

p1.add(p4);
p1.add(p3);
add(p1);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);

}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
// 输入数字个数不等于4个
if (tf1.getText().length() != 4) {
new project1_1(this, tf1, templateStr).error();
//JOptionPane.showMessageDialog(null, "");
return;
}
// 输入值非数字时
try {
int x1 = Integer.parseInt(tf1.getText());
} catch (Exception y) {
new project1_1(this, tf1, templateStr).flase();
return;
}
// 输入相同数字
int m = 0;
String text=tf1.getText();
for (int i = 0; i <3; i++) {
if(text.indexOf(text.charAt(i), i+1)>0)
m++;
/*for (int p = i + 1; p <= 3; p++)
if (tf1.getText().charAt(i) == tf1.getText().charAt(i + p)) {
m++;
}*/
}

if (m > 0) {
new project1_1(this, tf1, templateStr).same();
return;
}
// 输入正确数字时
if (tf1.getText().equals(templateStr)) {
new project1_1(this, tf1, templateStr).success();
}

else {
// int n=tf1.getText().compareTo(tf2.getText());
int count = 0;
int index = 0;

//System.out.println(templateStr);
for(int i=0;i<4;i++){
if(templateStr.indexOf(text.charAt(i))>=0){
count++;
if(templateStr.charAt(i)==text.charAt(i)){
count--;
index++;
}

}
}
tf2.append(index+"A"+count+"B ");
}
}

if (e.getSource() == b2) {
this.dispose(); // public void dispose()释放由此
// Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。
}
if (e.getSource() == b3) {
tf1.setText("");
tf2.setText("");

}
}
private String getRandomNumString(){
StringBuffer sb=new StringBuffer();
Random rand=new Random();
sb.append(rand.nextInt(9));
for(int i=0;i<3;i++){

while(true){
int temp=rand.nextInt(9);
if(sb.toString().indexOf(temp+"")<0){
sb.append(temp);
break;
}
}
}
return sb.toString();
}
}

public class project1 {
public static void main(String agrs[]) {
new caishuzi(500, 50, 300, 500);

}

}

老实说,搞不懂你这程序怎么来的,感觉是改别人的,好多逻辑很乱。 还有一点就是,干嘛非要自己弄一个dialog呢,JOptionPane.showMessageDialog(null,"msg");多好,简单方便还好看点。本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网