2012-09-26 25 views
2

我是新來的鞦韆,並想知道爲什麼有時我的應用程序出現空白,有時顯示組件。它似乎是零星的。沒有例外拋出或類似的東西。它只是經常出現一個空白的JFrame。有時,當我關閉應用程序並再次運行它時,它會正確顯示組件,但它主要是空白的。我在代碼中做錯了什麼?如果有問題,我正在使用Eclipse IDE。下面的代碼:Java Swing Blank JFrame出現了嗎?

import java.applet.Applet; 
import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.ArrayList; 
import java.util.Scanner; 

import javax.swing.*; 


public class Main extends JFrame implements ActionListener { 
    private static final long serialVersionUID = 1L; 
    JRadioButton randomRadioButton; 
    JRadioButton uniqueRadioButton; 
    JRadioButton participationRadioButton; 
    ArrayList<Student> allStudents; 
    JFrame mainFrame; 

    public Main(){ 
     allStudents = new ArrayList<Student>(); 
     processAllStudents(); 
     mainFrame = new JFrame(); 
     mainFrame.setVisible(true); 
     mainFrame.setSize(250, 400); 
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel componentHolder = new JPanel(); 
     componentHolder.setLayout(new GridLayout(5,1)); 

     JLabel titleText = new JLabel("      Randomizer"); 
     componentHolder.add(titleText); 
     JButton picker = new JButton("Pick a Student"); 
     JFileChooser filePick = new JFileChooser(); 
     filePick.addActionListener(this); 

     ButtonGroup allRadioButtons = new ButtonGroup(); 
     randomRadioButton = new JRadioButton("Completely Random"); 
     uniqueRadioButton = new JRadioButton("Unique"); 
     participationRadioButton = new JRadioButton("Complete Participation"); 

     allRadioButtons.add(randomRadioButton); 
     allRadioButtons.add(uniqueRadioButton); 
     allRadioButtons.add(participationRadioButton); 

     componentHolder.add(randomRadioButton); 
     componentHolder.add(uniqueRadioButton); 
     componentHolder.add(participationRadioButton); 

     picker.addActionListener(this); 

     componentHolder.add(picker); 
     componentHolder.add(filePick); 
     mainFrame.add(componentHolder); 
    } 

    public void actionPerformed(ActionEvent e){ 
     if(e.getActionCommand().equals("Pick a Student")){ 
      if(randomRadioButton.isSelected()){ 
       Student result = getStudentRandom(); 
       result.increment(); 
       String resultString = new String(result.getName() + ", " + result.getFrequency()); 
       System.out.println(resultString); 
       JLabel resultLabel = new JLabel(resultString); 
       JOptionPane.showMessageDialog(mainFrame, resultLabel); 
      } 
      else if(uniqueRadioButton.isSelected()){ 
       Student firstStudent = getStudentRandom(); 
       Student secondStudent = getStudentRandom(); 
       Student result; 
       if(firstStudent.getName().equals(secondStudent.getName())){ 
        result = secondStudent; 
       } 
       else{ 
        result = firstStudent; 
       } 
       result.increment(); 
       String resultString = new String(result.getName() + ", " + result.getFrequency()); 
       System.out.println(resultString); 
       JLabel resultLabel = new JLabel(resultString); 
       JOptionPane.showMessageDialog(mainFrame, resultLabel); 
      } 
      else if(participationRadioButton.isSelected()){ 
       Student result = selectStudentParticipant(); 
       result.increment(); 
       JOptionPane.showMessageDialog(mainFrame, result.getName() + ", " + result.getFrequency()); 
      } 

     } else System.out.println("Error."); 


    } 
    public void processAllStudents(){ 
     File f = new File("Test.txt"); 
     Scanner scanFile = null; 
     try { 
      scanFile = new Scanner(f); 
     } catch (FileNotFoundException e) { 
      System.out.println("File Not Found"); 
     } 
     while(scanFile.hasNext()){ 
      String name = scanFile.next(); 
      int frequency = scanFile.nextInt(); 
      Student s = new Student(name, frequency); 
      allStudents.add(s); 
     } 
    } 
    public Student getStudentRandom(){ 
     int result = (int) (Math.random() * allStudents.size()); 
     return allStudents.get(result); 
    } 
    public Student selectStudentParticipant(){ 
     Student temp = null; //Start of bubble sort algorithm 

     for(int i = 0; i < allStudents.size() - 1; i++){ 
      Student firstStudent = allStudents.get(i); 
      Student secondStudent = allStudents.get(i+1); 
      if(firstStudent.getFrequency() > secondStudent.getFrequency()){ 
       temp = firstStudent; 
       firstStudent = secondStudent; 
       secondStudent = temp; 
      } 
     } 

     //End of bubble sort algorithm. Data structure now sorted increasing 

     int firstRandom = (int) (Math.random() * (allStudents.size()/2) + 0.2 * allStudents.size()); //Likely to be bigger 
     int secondRandom = (int) (Math.random() * (allStudents.size()/2)); 
     int randomIndex = 0; //Used to represent a random index 

     if(firstRandom > secondRandom){ //More likely. Selects from first half of list 
      randomIndex = (int) (Math.random() * allStudents.size()/2); 
     } 

     else if(firstRandom < secondRandom){ //Possible, but less likely 
      randomIndex = (int) ((Math.random() * allStudents.size()/2) + allStudents.size()/2); 
     } 

     else{ //If the two random numbers are the same 
      randomIndex = (int) (Math.random() * allStudents.size()/2); 
     } 

     return allStudents.get(randomIndex); 

    } 
    public static void main(String[] args){ 
     new Main(); 
    } 


} 

回答

6

setVisible(true)必須是在叫你的主要方法的最後一個方法。否則,沒有什麼可以在JFrame的

+1

事情*可能*渲染,但不可靠。你的建議是正確的,1+。 –

+0

@HovercraftFullOfEels正確,但是當我遇到這樣的問題時,通常是因爲:( –

5

渲染爲什麼有時候我的應用程序來爲空白,有時 顯示組件。它似乎是零星的。

它不是零星的,當你錯過frame.pack()電話告訴框架收拾它的所有部件

執行下列操作和框架應顯示正常情況:

mainFrame.add(componentHolder); 
mainFrame.pack(); 
mainFrame.setVisible(true); 
+0

+1始終是'pack',然後是'setvisible'。 – davidXYZ