JPanel定时切换

package com.test;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import com.flow.ChartTest;
/**
 * @author 徐彬
 * @ 界面JPanel定时切换
 */
public class TestToJPanel extends JFrame{
	/**
	 * 总JPanel
	 * */
	private JPanel conpanel=new JPanel(new GridLayout(1, 1));
	/**
	 * tmp_JPanel
	 * */
	private JPanel[] panel1=new JPanel[20];
	/**
	 * JPanel定时标记
	 */
	private int t=0;
	/**
	 * 构造函数
	 */
	public TestToJPanel(){			  
		//设置页面的基本信息
		this.setSize(Toolkit.getDefaultToolkit().getScreenSize());//全屏显示Toolkit.getDefaultToolkit().getScreenSize()
		this.setTitle("图");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//拦截关闭窗口事件
	}		
	
	public void init(){

		String shu[]={"第1个组件","第2个组件","第3个组件","第4个组件","第5个组件"};		
		panel1[0]=new JPanel(new GridLayout(1, 1));	
		panel1[0].add("添加进第一个所要建立的组件-shu[0]");
		conpanel.add(panel1[0]);
		this.getContentPane().add(conpanel);
		queryPanle();//进入定时切换JPanel方法	
	}
	
	/**
	 * 定时切换JPanel
	 */
	public void queryPanle(){	
	 Timer swt   =   new Timer(   
				 60000,     			      
		new   ActionListener(){   			    	  
	        public   void   actionPerformed(ActionEvent   ae){ 
        	  String shu[]={"第1个组件","第2个组件","第3个组件","第4个组件","第5个组件"};	
        		  if(shu.length>=2){		        			  
        				//System.out.println("移除:"+t);
        				conpanel.remove(panel1[t]);	
        				if(t==0){
        				t++;
        				//System.out.println("增加:"+t);			        				
						if(panel1[t]==null){
    						panel1[t]=new JPanel(new GridLayout(1, 1));	
    						panel1[t].add("添加进所要建立的组件-shu[t]");
    					}		        						        								
						conpanel.add(panel1[t]);		        									        				
        				}else{
        					if(shu.length>2){
        						t++;
        						if(t<=shu.length){
        						//System.out.println("增加:"+t);
        						if(panel1[t]==null){
	        						panel1[t]=new JPanel(new GridLayout(1, 1));	
	        						panel1[t].add("添加进所要建立的组件-shu[t]");
        						}
        						conpanel.add(panel1[t]);	
        						}else{
        							t=0;
        							//System.out.println("增加:"+t);
	        						if(panel1[t]==null){
		        						panel1[t]=new JPanel(new GridLayout(1, 1));	
		        						panel1[t].add("添加进所要建立的组件-shu[t]");
	        						}
        							conpanel.add(panel1[t]);
        						}
        					}else{
        						t--;
        						//System.out.println("增加:"+t);			        						
        						if(panel1[t]==null){
        							panel1[t]=new JPanel(new GridLayout(1, 1));
        							panel1[t].add("添加进所要建立的组件-shu[t]");
        						}
        						conpanel.add(panel1[t]);	
        					}
        				}			        				 
			        	  conpanel.revalidate();
			        	  conpanel.repaint();						        
	        	  }			        		  			        	  			    	  
			     }}   
			 );
		swt.start();
	}	
	
	public static void main(String[] args){
		TestToJPanel ttjp=new TestToJPanel();	
		ttjp.init();		
		ttjp.setVisible(true);
	}
}

 

你可能感兴趣的:(swing)