在swing编程中我们经常需要对某个容器中的某一位置的JPanel进行动态的切换,下面代码给出了一个动态切换的例子:(ChangePanelExample.java,用VisualEditor编写,关键代码用汉语注释了)
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
public class ChangePanelExample extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jPanel = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JPanel jPanel1 = null;
private JLabel jLabel = null;
private JPanel jPanel2 = null;
private JLabel jLabel1 = null;
private JPanel jPanel3 = null;
private JLabel jLabel2 = null;
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jPanel = new JPanel();
jPanel.setLayout(null);
jPanel.setPreferredSize(new Dimension(0, 60));
jPanel.setBorder(BorderFactory.createLineBorder(Color.black, 1));
jPanel.add(getJButton(), null);
jPanel.add(getJButton1(), null);
jPanel.add(getJButton2(), null);
}
return jPanel;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(54, 11, 83, 27));
jButton.setForeground(Color.red);
jButton.setText("Panel一");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jContentPane.remove(((BorderLayout) jContentPane
.getLayout())
.getLayoutComponent(BorderLayout.CENTER));//删除内容面板中间原来的组建
jContentPane.add(getJPanel1(), BorderLayout.CENTER);//添加要切换的面板
jContentPane.validate();//重构内容面板
jContentPane.repaint();//重绘内容面板
//上面两句缺一不可,
//没有validate()删除和添加没有效果
//没有repaint()界面绘出现混乱
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(191, 11, 83, 27));
jButton1.setForeground(Color.blue);
jButton1.setText("Panel二");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jContentPane.remove(((BorderLayout) jContentPane
.getLayout())
.getLayoutComponent(BorderLayout.CENTER));
jContentPane.add(getJPanel2(), BorderLayout.CENTER);
jContentPane.validate();
jContentPane.repaint();
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(328, 11, 83, 27));
jButton2.setForeground(Color.green);
jButton2.setText("Panel三");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jContentPane.remove(((BorderLayout) jContentPane
.getLayout())
.getLayoutComponent(BorderLayout.CENTER));
jContentPane.add(getJPanel3(), BorderLayout.CENTER);
jContentPane.validate();
jContentPane.repaint();
}
});
}
return jButton2;
}
/**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(151, 99, 163, 113));
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
jLabel.setForeground(new Color(255, 51, 51));
jLabel.setText("Panel一");
jPanel1 = new JPanel();
jPanel1.setLayout(null);
jPanel1.add(jLabel, null);
}
return jPanel1;
}
/**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(162, 100, 138, 96));
jLabel1.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setForeground(Color.blue);
jLabel1.setText("Panel二");
jPanel2 = new JPanel();
jPanel2.setLayout(null);
jPanel2.add(jLabel1, null);
}
return jPanel2;
}
/**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(149, 87, 172, 134));
jLabel2.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setForeground(Color.green);
jLabel2.setText("Panel三");
jPanel3 = new JPanel();
jPanel3.setLayout(null);
jPanel3.add(jLabel2, null);
}
return jPanel3;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ChangePanelExample thisClass = new ChangePanelExample();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public ChangePanelExample() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(472, 410);
this.setResizable(false);
this.setContentPane(getJContentPane());
this.setTitle("JPanel切换示例");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJPanel(), BorderLayout.NORTH);
jContentPane.add(getJPanel1(), BorderLayout.CENTER);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="92,55"