日期:2014-05-20  浏览次数:20589 次

新手提问 请教高手
//我想要的结果是:启动一个test,窗口显示的名字就是"test"+"该窗口的标号num".结果却不是,请指教
import java.awt.*;
import java.awt.event.*;

public class test extends Frame {
static int num;

test() {
super("Test"+(++num));
this.setVisible(true);
this.setBounds(100,100,40,40);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public static void main(String[] args) {
new test();
}
}

------解决方案--------------------
Java code
//:TestFrame.java
import java.awt.*;
import java.awt.event.*;

public class TestFrame extends Frame {
    static int num;//存储窗口大小

    TestFrame() {
        super("Test "+(++num));//这里字符串"Test "中Test后加了一个空格,这只是为了测试,可以把这个空格删去。
        //setBounds()的第3个、第4个参数是为了调节窗后的大小,把第3个数设置得稍大些,这样才能够看清楚标题。
        this.setBounds(300,300,240,50);
        this.setVisible(true);//这一句最好放在setBounds的后面
        //this.setSize(300,200);
        //this.setLocation(240,100);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        int nums=0;//存储窗口的个数
        try{
            if(args!=null){
                nums=Integer.parseInt(args[0]);
            }
        }catch(Exception e){
            System.err.println("请在参数中输入窗口的个数(int型):");
        }
        for(int i=1;i<=nums;i++){
            new TestFrame();
        }
    }
}