Java GUI需要很长时间才能打开

时间:2014-04-28 09:43:25

标签: java user-interface

我有一个Java程序,目前只能通过控制台进行接口。我现在正致力于在Eclipse中创建一个简单的图形用户界面,以允许用户与程序进行交互。目前的GUI只是一个非常基本的GUI - 只是一个“示例”,可以在屏幕上显示和运行并显示。

然而,出于某种原因,当我在Eclipse中单击“运行”时打开它需要太长时间...我必须等待30秒到1分钟才能在点击“运行”后打开它',即使控制台似乎表明它正在直接运行(一旦我点击运行,控制台顶部的'停止'按钮变为'可点击')。当GUI最终打开时,显示的所有内容都是一个带有“hello”字样的文本区域,这正是我所期望的 - 因此它可以正常运行。

使用我之前编写的Java应用程序,点击“运行”后,GUI通常会打开不超过五秒钟。有没有人知道为什么这么长时间,或者我应该做些什么来让它更快地打开?我的Gui.java类目前看起来像这样:

package openDIS;

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Gui extends JFrame{

public String text = "";
public int rows = 20;
public int columns = 5;

public Gui(){
    setTitle("DIS Filter");
    setSize(1000, 500);
    setLocation (10, 10);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    initGui();
}

/*public quitButton(){
    initGui();
} */

private void initGui(){
    //JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    this.getContentPane().add(panel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("DIS Filter");
    this.setSize(1000, 500);

    panel.setLayout(null);
    /*Add a JTextArea to display the output DIS information */
    JTextArea displayOutput = new JTextArea(text, rows, columns);
    displayOutput.setBounds(10, 10, 100, 100);
    panel.add(displayOutput);
    displayOutput.setEditable(false);
    //displayOutput.setEditable(false);
    add(panel);
    displayOutput.append("hello");


    //String data = EspduReceiver.espdu;
//  EspduReceiver.receivePdu(); /*The code underneath here will never be reached unless I specify how long to perform this method call for- */
                                /*Since the receivePdu() method has no 'end' condition- it keeps looping continually until told to stop */
                                /*Try using a timer to specify how long it should be called for */
    long start = System.currentTimeMillis();
    long end = start + 60*1000; /* 60 seconds * 1000 ms/sec */
    while (System.currentTimeMillis() < end){

        int n = EspduReceiver.entitySite.size(); /*This is what to use, but set n to 10 for testing purposes. */
        //int n = 10;
        System.out.print(n);
        for (int i = 0; i < n; i++){
//          EspduReceiver.receivePdu();
            System.out.print("Entered 'for' loop. ");
            System.out.println(EspduReceiver.entitySite.get(i));
            System.out.println(EspduReceiver.entityApplication.get(i));
            System.out.println(EspduReceiver.entity.get(i));
            displayOutput.append(EspduReceiver.entitySite.get(i).toString());
            displayOutput.append(EspduReceiver.entityApplication.get(i).toString());
            displayOutput.append(EspduReceiver.entity.get(i).toString());
        }       
    }




    JButton quitButton = new JButton("Quit");
    panel.add(quitButton);
    quitButton.setBounds(875, 400, 80, 30); /*Set the location of the button in the window, and its size */


    quitButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    });


    panel.add(quitButton);
    //setTitle("Quit");
    //setSize(60,30); /*This line was overwriting the previously set values for the size of the window */
    setLocationRelativeTo(null);
    panel.repaint();
    setDefaultCloseOperation(EXIT_ON_CLOSE);        
}

public static void main(String[] args){ /* I probably don't need a main method here- I have one in EspduReceiver.java */
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run(){
            Gui gui = new Gui();
            gui.setVisible(true);
        }
    });
}




}

0 个答案:

没有答案