在另一个JPanel之后调用时,JPanel不会加载

时间:2014-01-16 23:36:11

标签: java swing jpanel

我正在构建一个GUI,我的程序运行了一系列测试。我最终关闭了我的第一个JPanel窗口,并打开另一个窗口。当我运行第二个GUI窗口的类时,它的效果非常好。但在关闭第一个窗口后,窗口会出现,但不会显示任何组件。这是内存问题吗?我该怎么办呢?

代码:

package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")

public class guiRun extends JFrame {
    public  guiRun() {
        JPanel Window = new JPanel();
        getContentPane().add(Window);


        JLabel greeting = new JLabel("Automated Module Tester-Running");

        JLabel Browser = new JLabel("Current Browser:");
        JLabel cFailures = new JLabel("Current Fails:");
        JLabel cSuccess = new JLabel("Current Successes:");
        JLabel RunTime = new JLabel("Total RunTime:");
        JLabel totalTests = new JLabel("Total Tests:");
        JLabel Username = new JLabel("Username Under Test:");
        JLabel Router = new JLabel("Router Under Test:"); 
        JLabel TestType = new JLabel("Test Type: ");
        final JLabel RunTimetime = new JLabel("00:00:00");
        final JLabel cBrowser = new JLabel(currentBrowser);
        final JLabel Failures = new JLabel(Integer.toString(tFails));
        final JLabel Successes = new JLabel(Integer.toString(tSuccesses));
        final JLabel Total = new JLabel (Integer.toString(tFails+tSuccesses));
        final JLabel cUsername = new JLabel(currentUser);
        final JLabel cRouter = new JLabel(currentRouter);
        final JLabel theTestType = new JLabel(currentTest);
        JButton End = new JButton("End Test");

        JLabel loopProgress = new JLabel("Loop Progress:");

        final JProgressBar PBar = new JProgressBar(currentProgress);

        Window.setLayout(null);

        greeting.setBounds(350,10,200,40);
        Browser.setBounds(670,150,150,20);
        cBrowser.setBounds(695,170,100,30);
        totalTests.setBounds(100,130,100,20);
        Total.setBounds(250,130,50,20);
        cFailures.setBounds(100,170,100,20);
        Failures.setBounds(250,170,50,20);
        cSuccess.setBounds(100,210,150,20);
        Successes.setBounds(250,210,50,20);
        RunTime.setBounds(100,250,150,20);
        RunTimetime.setBounds(250,250,100,20);
        TestType.setBounds(350,150,150,25);
        theTestType.setBounds(500,150,100,25);
        Username.setBounds(350,175,150,25);
        Router.setBounds(350,200,150,25);
        cUsername.setBounds(500,175,100,25);
        cRouter.setBounds(500,200,100, 25);
        End.setBounds(320, 320, 200, 60);
        loopProgress.setBounds(375,390,200,40);

        PBar.setValue(currentProgress);
        PBar.setMaximum(maxProgress);
        PBar.setBounds(100, 430, 700, 50);


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

        });
        ActionListener updateClockAction = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                PBar.setValue(currentProgress);
                PBar.setMaximum(maxProgress);
                theTestType.setText(currentTest);
                cRouter.setText(currentRouter);
                cUsername.setText(currentUser);
                Total.setText(Integer.toString(tFails+tSuccesses));
                Successes.setText(Integer.toString(tSuccesses));
                Failures.setText(Integer.toString(tFails));
                cBrowser.setText(currentBrowser);
                secs++;
                if (secs>=60){
                    mins++;
                    secs = 0;
                }
                if (mins >=60){
                    hours++;
                    mins = 0;
                }
                 RunTimetime.setText(hours+":"+mins+":"+secs); 
                }   
            };

        //ActionListener

        Timer time = new Timer(1000, updateClockAction);
        time.start();

        Window.add(Username);
        Window.add(cUsername);
        Window.add(Router);
        Window.add(cRouter);
        Window.add(theTestType);
        Window.add(TestType);
        Window.add(End);
        Window.add(totalTests);
        Window.add(Total);
        Window.add(cFailures);
        Window.add(cSuccess);
        Window.add(Successes);
        Window.add(Failures);
        Window.add(RunTimetime);
        Window.add(RunTime);
        Window.add(Browser);
        Window.add(cBrowser);
        Window.add(greeting);
        Window.add(loopProgress);
        Window.add(PBar);


        setTitle("Module Tester - Running ");
        setSize(900,600);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void setBrowser(String thebrowser){currentBrowser = thebrowser;}
    public void setUser(String theUser){currentUser = theUser;}
    public void setRouter(String theRouter){currentRouter = theRouter;}
    public void setProgress(int set){currentProgress = set;}
    public void setMaxProgress(int max){maxProgress = max;}
    public void addFail(){tFails++;}
    public void addSuccess(){tSuccesses++;}
    public void setCurrentTest(String test){currentTest = test;}


    private
     int hours = 0;
     int mins = 0;
     int secs = 0;
     String currentBrowser;
     String currentUser;
     String currentRouter;
     int currentProgress = 0;
     int maxProgress = 100;
     int tFails = 0;
     int tSuccesses = 0;
     String currentTest;

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                guiRun window = new guiRun();
                window.setVisible(true);
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

使用window.repaint()或jpanel.repaint()