简体   繁体   中英

JPanel doesn't load when called after another JPanel

I'm building a GUI, my program runs a series of tests. I end up closing my first JPanel window, and opening another one. When I run the second GUI window's class, it works just great. But after closing the first one, the window will come up, but no components will display. Is this a memory issue? What can I do about it?

Code:

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);
            }
        });
    }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM