Java Swing空白JFrame即将出现?
我是swing新手,想知道为什么有时我的应用程序显示为空白,有时它显示组件。这似乎是零星的。没有抛出异常或类似的情况。它只是经常出现在一个空白的JFrame中。有时,当我关闭应用程序并再次运行它时,它会正确地显示组件,但主要显示为空白。我是不是在代码中做错了什么?如果有必要的话,我正在使用EclipseIDE。代码如下:Java Swing空白JFrame即将出现?,java,eclipse,swing,jframe,Java,Eclipse,Swing,Jframe,我是swing新手,想知道为什么有时我的应用程序显示为空白,有时它显示组件。这似乎是零星的。没有抛出异常或类似的情况。它只是经常出现在一个空白的JFrame中。有时,当我关闭应用程序并再次运行它时,它会正确地显示组件,但主要显示为空白。我是不是在代码中做错了什么?如果有必要的话,我正在使用EclipseIDE。代码如下: import java.applet.Applet; import java.awt.BorderLayout; import java.awt.FlowLayout; imp
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();
}
}
import java.applet.applet;
导入java.awt.BorderLayout;
导入java.awt.FlowLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.ArrayList;
导入java.util.Scanner;
导入javax.swing.*;
公共类Main扩展JFrame实现ActionListener{
私有静态最终长serialVersionUID=1L;
JRadioButton随机单选按钮;
JRadioButton uniqueRadioButton;
JRadioButton participationRadioButton;
ArrayList所有学生;
JFrame主机;
公用干管(){
allStudents=newarraylist();
processAllStudents();
大型机=新的JFrame();
mainFrame.setVisible(true);
大型机。设置大小(250400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel componentHolder=新的JPanel();
setLayout(新的GridLayout(5,1));
JLabel titleText=新的JLabel(“随机化发生器”);
组件持有人。添加(标题文本);
JButton picker=新JButton(“选择学生”);
JFileChooser filePick=新的JFileChooser();
filePick.addActionListener(这个);
ButtonGroup allRadioButtons=新建ButtonGroup();
randomRadioButton=新的JRadioButton(“完全随机”);
uniqueRadioButton=新的JRadioButton(“唯一”);
participationRadioButton=新的JRadioButton(“完全参与”);
所有单选按钮。添加(随机单选按钮);
所有单选按钮。添加(单选按钮);
所有单选按钮。添加(participationRadioButton);
组件支架。添加(随机单选按钮);
组件保持器。添加(uniqueRadioButton);
组件持有人。添加(participationRadioButton);
picker.addActionListener(这个);
组件支架。添加(选择器);
componentHolder.add(filePick);
大型机。添加(组件持有人);
}
已执行的公共无效操作(操作事件e){
如果(例如getActionCommand().equals(“选择学生”)){
if(randomRadioButton.isSelected()){
学生成绩=getStudentRandom();
result.increment();
String resultString=新字符串(result.getName()+”,“+result.getFrequency());
System.out.println(resultString);
JLabel resultLabel=新的JLabel(resultString);
showMessageDialog(大型机,结果标签);
}
else if(uniqueRadioButton.isSelected()){
Student firstStudent=getStudentRandom();
Student secondStudent=getStudentRandom();
学生成绩;
if(firstStudent.getName().equals(secondStudent.getName())){
结果=第二名学生;
}
否则{
结果=第一名学生;
}
result.increment();
String resultString=新字符串(result.getName()+”,“+result.getFrequency());
System.out.println(resultString);
JLabel resultLabel=新的JLabel(resultString);
showMessageDialog(大型机,结果标签);
}
else if(participationRadioButton.isSelected()){
学生成绩=selectStudentParticipant();
result.increment();
showMessageDialog(大型机,result.getName()+“,“+result.getFrequency());
}
}else System.out.println(“错误”);
}
公众假期所有学生(){
文件f=新文件(“Test.txt”);
扫描仪扫描文件=空;
试一试{
扫描文件=新扫描仪(f);
}catch(filenotfounde异常){
System.out.println(“未找到文件”);
}
while(scanFile.hasNext()){
字符串名称=scanFile.next();
int frequency=scanFile.nextInt();
学生s=新学生(姓名、频率);
所有学生。添加(s);
}
}
公立学生getStudentRandom(){
int结果=(int)(Math.random()*allStudents.size());
返回所有学生。获取(结果);
}
公共学生选择学生参与者(){
Student temp=null;//开始冒泡排序算法
对于(int i=0;isecondStudent.getFrequency()){
temp=第一名学生;
第一名学生=第二名学生;
第二名学生=临时工;
}
}
//气泡排序算法结束。数据结构现在排序增加
int firstRandom=(int)(Math.random()*(allStudents.size()/2)+0.2*allStudents.size());//可能更大
int secondRandom=(int)(Math.random()*(allStudents.size()/2));
int randomIndex=0;//用于表示随机索引
如果(firstRandom>secondRandom){//更有可能。从列表的前半部分选择
randomIndex=(int)(Math.random()*allStudents.size()/2);
}
else如果(firstRandommainFrame.add(componentHolder);
mainFrame.pack();
mainFrame.setVisible(true);