【发布时间】:2014-01-27 20:58:21
【问题描述】:
我有一个 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”,但我不知道如何命名。
【问题讨论】:
-
请显示
main方法,或任何调用GUI代码的方法。 -
我刚刚添加了启动 GUI 的主要方法
-
你刚刚违反了 Swing 的单线程规则。所有 UI 创建和修改都应在 EDT 的上下文中执行。而不是使用
new Thread,您应该使用EventQueue.invokeLater(Runnable)
标签: java eclipse multithreading user-interface