0
我正在構建GUI,我的程序運行了一系列測試。我最終關閉了我的第一個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);
}
});
}
}
'null'佈局是不是你的朋友和失敗時的Swing通知它的成分作出迴應事情發生了變化。您應該嘗試使用[Java編程語言的代碼約定](http://www.oracle.com/technetwork/java/codeconv-138413.html),它將使您的代碼更易於閱讀 – MadProgrammer