逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MultiPanel extends JPanel {
    
private static final long serialVersionUID = 1L;
    
private JButton           changeButton1;
    
private JButton           changeButton2;
    
private JButton           changeButton3;
    
private JPanel            panel1;
    
private JPanel            panel2;
    
private JPanel            panel3;
    
private JPanel            currentPanel;

    
public MultiPanel() {
        
this.setLayout(new BorderLayout());

        changeButton1 
= new JButton(
                
"<html><font color=red>Red Panel</font></html>");
        changeButton2 
= new JButton(
                
"<html><font color=green>Green Panel</font></html>");
        changeButton3 
= new JButton(
                
"<html><font color=blue>Blue Panel</font></html>");
        panel1 
= new JPanel();
        panel2 
= new JPanel();
        panel3 
= new JPanel();

        panel1.setBackground(Color.RED);
        panel2.setBackground(Color.GREEN);
        panel3.setBackground(Color.BLUE);
        currentPanel 
= panel1;

        Box box 
= Box.createHorizontalBox();
        box.add(changeButton1);
        box.add(changeButton2);
        box.add(changeButton3);
        
this.add(box, BorderLayout.SOUTH);

        
this.add(currentPanel, BorderLayout.CENTER);

        addButtonActionListener();
    }


    
private void addButtonActionListener() {
        changeButton1.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                currentPanel.setVisible(
false);
                currentPanel 
= panel1;
                currentPanel.setVisible(
true);
                add(currentPanel, BorderLayout.CENTER);
                validate();
            }

        }
);

        changeButton2.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                currentPanel.setVisible(
false);
                currentPanel 
= panel2;
                currentPanel.setVisible(
true);
                add(currentPanel, BorderLayout.CENTER);
                validate();
            }

        }
);

        changeButton3.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                currentPanel.setVisible(
false);
                currentPanel 
= panel3;
                currentPanel.setVisible(
true);
                add(currentPanel, BorderLayout.CENTER);
                validate();
            }

        }
);
    }


    
private static void createAndShowGUI() {
        JFrame frame 
= new JFrame("Multi Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(
500600);

        MultiPanel panel 
= new MultiPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);

        frame.setVisible(
true);
    }


    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        SwingUtilities.invokeLater(
new Runnable() {
            
public void run() {
                MultiPanel.createAndShowGUI();
            }

        }
);
    }


}

posted on 2008-03-22 18:35 逛奔的蜗牛 阅读(855) 评论(0)  编辑 收藏 引用 所属分类: Java

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理