Java中swing编程中界面组件加载不出的问题

前段时间在用Java进行swing编程中出现的一个小问题,今天突然想起来,记录下来

练习时编写了一个简单的随机数生成器,代码如下:

 

 

package NumberGenerator;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class SetInterface extends JFrame implements ActionListener{
	// 声明组件
	JLabel[] num = new JLabel[5];
	JLabel prompt;
	JButton start, stop;
	JPanel jp_label, jp_button, jp_num;

	// 声明线程
	SetThread[] num_thread = new SetThread[5];
	
	//按钮次数
	int count = 0;

	public SetInterface(){

		//设置窗口属性
		this.setBounds(400, 200, 280, 200);
		this.setTitle("随机数生成器");
		this.setResizable(false);
		
		this.setVisible(true);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		
		//实例化组件
		for(int i = 0; i <= 4; i++){
			//初始化标签上的数字
			num[i] = new JLabel("00");
			//设置字体
			num[i].setFont(new Font("Dialog", 1, 30));
			//设置边框
			num[i].setBorder(BorderFactory.createEtchedBorder());
		}
		
		prompt = new JLabel("请点击start开始!!!");
		prompt.setHorizontalAlignment(SwingConstants.CENTER);//居中
		prompt.setFont(new Font("Dialog", 1, 20));//字体
		prompt.setForeground(Color.RED);//颜色
		
		start = new JButton("Start");
		stop = new JButton("Stop");
		
		jp_label = new JPanel();
		jp_button = new JPanel();
		jp_num = new JPanel();
		
		
		//实例化线程
		for(int i = 0; i <= 4; i++){
			num_thread[i] = new SetThread(num[i]);
		}
		
		//添加组件
		for(int i = 0; i <= 4; i++){
			jp_num.add(num[i]);
		}
		jp_num.setBorder(BorderFactory.createEtchedBorder());
		
		jp_label.add(jp_num);
		jp_label.add(prompt);
		
		jp_button.add(start);
		jp_button.add(stop);
		
		//设置面板边框
		jp_label.setBorder(BorderFactory.createEtchedBorder());
		jp_button.setBorder(BorderFactory.createEtchedBorder());
		
		//添加事件
		start.addActionListener(this);
		stop.addActionListener(this);
		
		//添加面板
		this.add(jp_label, BorderLayout.CENTER);
		this.add(jp_button, BorderLayout.SOUTH);
		
		
	}
	
	//重写actionPerformed方法
	public void actionPerformed(ActionEvent e){
		//start事件
		if(e.getSource() == start){
			//判断线程是否全部停止(最后个线程是否在活动)
			if(!num_thread[4].isAlive()){
				//重新实例化线程
				for(int i = 0; i <= 4; i++){
					num_thread[i] = new SetThread(num[i]);
				}
				//将count计数归0
				count = 0;
			}
			//启动线程,启动数字生成器
			for(int i= 0; i <= 4; i++){
				num_thread[i].start();
			}
			//改变提示信息
			prompt.setText("请按stop生成随机数!");
		}//end事件
		else{
			if(count <= 5){
				//停止线程
				num_thread[count].SetFlag(false);
				count++;
				if(count == 5){
					prompt.setText("请点击start重新开始!!!");					
				}
			}
		}
	}
}


程序没问题,但是运行时,界面的组件一直加载不出来,要先最小化,然后才能正常显示:

 

 

经过百度,查询资料,发现原因是在swing编程中JFrame的初始化在添加和初始化各个组件时要放到最后,修改后代码如下:

 

package NumberGenerator;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class SetInterface extends JFrame implements ActionListener{
	// 声明组件
	JLabel[] num = new JLabel[5];
	JLabel prompt;
	JButton start, stop;
	JPanel jp_label, jp_button, jp_num;

	// 声明线程
	SetThread[] num_thread = new SetThread[5];
	
	//按钮次数
	int count = 0;

	public SetInterface(){
		//实例化组件
		for(int i = 0; i <= 4; i++){
			//初始化标签上的数字
			num[i] = new JLabel("00");
			//设置字体
			num[i].setFont(new Font("Dialog", 1, 30));
			//设置边框
			num[i].setBorder(BorderFactory.createEtchedBorder());
		}
		
		prompt = new JLabel("请点击start开始!!!");
		prompt.setHorizontalAlignment(SwingConstants.CENTER);//居中
		prompt.setFont(new Font("Dialog", 1, 20));//字体
		prompt.setForeground(Color.RED);//颜色
		
		start = new JButton("Start");
		stop = new JButton("Stop");
		
		jp_label = new JPanel();
		jp_button = new JPanel();
		jp_num = new JPanel();
		
		
		//实例化线程
		for(int i = 0; i <= 4; i++){
			num_thread[i] = new SetThread(num[i]);
		}
		
		//添加组件
		for(int i = 0; i <= 4; i++){
			jp_num.add(num[i]);
		}
		jp_num.setBorder(BorderFactory.createEtchedBorder());
		
		jp_label.add(jp_num);
		jp_label.add(prompt);
		
		jp_button.add(start);
		jp_button.add(stop);
		
		//设置面板边框
		jp_label.setBorder(BorderFactory.createEtchedBorder());
		jp_button.setBorder(BorderFactory.createEtchedBorder());
		
		//添加事件
		start.addActionListener(this);
		stop.addActionListener(this);
		
		//添加面板
		this.add(jp_label, BorderLayout.CENTER);
		this.add(jp_button, BorderLayout.SOUTH);
		
		//设置窗口属性
		this.setBounds(400, 200, 280, 200);
		this.setTitle("随机数生成器");
		this.setResizable(false);
		
		this.setVisible(true);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
	}
	
	//重写actionPerformed方法
	public void actionPerformed(ActionEvent e){
		//start事件
		if(e.getSource() == start){
			//判断线程是否全部停止(最后个线程是否在活动)
			if(!num_thread[4].isAlive()){
				//重新实例化线程
				for(int i = 0; i <= 4; i++){
					num_thread[i] = new SetThread(num[i]);
				}
				//将count计数归0
				count = 0;
			}
			//启动线程,启动数字生成器
			for(int i= 0; i <= 4; i++){
				num_thread[i].start();
			}
			//改变提示信息
			prompt.setText("请按stop生成随机数!");
		}//end事件
		else{
			if(count <= 5){
				//停止线程
				num_thread[count].SetFlag(false);
				count++;
				if(count == 5){
					prompt.setText("请点击start重新开始!!!");					
				}
			}
		}
	}
}

界面显示正常:

 

 

具体原因暂时不清楚,总之先记录下来吧。

你可能感兴趣的:(java)