Java Swing空白JFrame来了? [英] Java Swing Blank JFrame coming up?

查看:159
本文介绍了Java Swing空白JFrame来了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚摇摆,想知道为什么有时我的应用程序出现空白,有时它会显示组件。这似乎是零星的。没有抛出任何异常或类似的东西。它只是经常出现作为一个空白的JFrame。当我关闭应用程序并再次运行它时,它会正确显示组件,但它主要出现为空白。我在代码中做错了吗?我使用Eclipse IDE,如果重要。这里是代码:

  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参与RadioButton;
ArrayList< Student>全体学生;
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()){
学生结果= 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()){
学生firstStudent = getStudentRandom();
Student secondStudent = getStudentRandom();
学生成绩;
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()){
学生结果= selectStudentParticipant();
result.increment();
JOptionPane.showMessageDialog(mainFrame,result.getName()+,+ result.getFrequency());
}

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


}
public void processAllStudents(){
File f = new File(Test.txt);
扫描仪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();
学生s =新学生(姓名,频率);
allStudents.add(s);
}
}
public学生getStudentRandom(){
int result =(int)(Math.random()* allStudents.size());
return allStudents.get(result);
}
public Student selectStudentParticipant(){
Student temp = null; //开始泡泡排序算法

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

//气泡排序算法结束。数据结构现在排序增加

int firstRandom =(int)(Math.random()*(allStudents.size()/ 2)+ 0.2 * allStudents.size()); //可能要更大
int secondRandom =(int)(Math.random()*(allStudents.size()/ 2));
int randomIndex = 0; //用于表示随机索引

if(firstRandom> secondRandom){//更有可能。从列表的上半部分选择
randomIndex =(int)(Math.random()* allStudents.size()/ 2);
}

else if(firstRandom< secondRandom){//可能但不太可能
randomIndex =(int)((Math.random()* allStudents.size )/ 2)+ allStudents.size()/ 2);
}

else {//如果两个随机数是相同的
randomIndex =(int)(Math.random()* allStudents.size()/ 2);
}

return allStudents.get(randomIndex);

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


}


解决方案

setVisible(true)必须是Main方法中调用的最后一个方法。否则,JFrame


可能无法呈现

I'm new to swing, and was wondering why sometimes my application comes up as blank, and sometimes it displays the components. It seems to be sporadic. There are no exceptions thrown or anything like that. It just frequently comes up as a blank JFrame. At times when I close the application and run it again, it shows the components correctly, but it mainly comes up as blank. Am I doing something wrong in the code? I'm using the Eclipse IDE, if it matters. Here's the code:

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


}

解决方案

setVisible(true) has to be the last method called in your Main method. Otherwise nothing may render on the JFrame

这篇关于Java Swing空白JFrame来了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆