2014-01-16 3 views
0

Я строю графический интерфейс, моя программа запускает серию тестов. Я заканчиваю закрытие своего первого окна JPanel и открытие другого. Когда я запускаю второй класс окна GUI, он работает просто отлично. Но после закрытия первого окна появится окно, но компоненты не будут отображаться. Это проблема памяти? Что я могу сделать с этим?JPanel не загружается при вызове после другого JPanel

Код:

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

}

+2

'макеты null' не ваш друг и не реагировать, когда свинг оповещает это компонент иерархия, что все изменилось. Вы должны попытаться использовать [Кодовые соглашения для языка программирования Java] (http://www.oracle.com/technetwork/java/codeconv-138413.html), это упростит чтение кода – MadProgrammer

ответ

0

использование window.repaint() или jpanel.repaint()

+0

Это не работает. – Ronin

Смежные вопросы