每次启动时,我的GUI都会显示两个窗口

时间:2014-01-27 20:58:21

标签: java eclipse multithreading user-interface

我有一个GUI,每次单击运行按钮,它都会打开两个GUI窗口!我不知道为什么这样做! 这是我的代码:

package com.robot;

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;

public class GUI extends JFrame implements Runnable {

//start of the constructor method for GUI
public GUI() {

    //defines objects
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension dim = tk.getScreenSize();

    //sets the GUI to be visible
    this.setVisible(true);

    //sets the size of the GUI
    this.setSize(600, 400);

    //centers the GUI
    int xPos = (dim.width / 2) - (this.getWidth() /2);
    int yPos = (dim.height / 2) - (this.getHeight() /2);

    this.setLocation(xPos, yPos);

    //makes the program unable to be resized
    this.setResizable(false);

}

public void run() {
    new GUI();
}

}

提前感谢您的帮助!

以下是启动GUI的部分

//main method start
public static void main(String[] args) throws InterruptedException, IOException, AWTException {

    //opens up the GUI
    (new Thread(new GUI())).start();

    //possible methods
    //ScanMarket.scanMarket(); //scans market for data
    //FindPattern("Images"); //finds pattern among images in image folder labeled Images

}//end of main method

我也想知道如何为我的程序提供一个不在JFrame上的标题。在屏幕的左上角,它表示该程序是“com.robot.Main”,我想将其命名为“ROBOT”,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

(new Thread(new GUI())).start();

在那里

public void run() {
    new GUI();
}

您创建了GUI类的两个实例,这两个实例又创建了两个框架。我将删除第二个创建,它位于run()方法内,以便更好地解决此问题。