这是以前做的一个小东西,里面的 MainFace.java 大概跟您说地差不多
你可以参考一下思路何实现方法。 (献丑了)
===============================================================
/**
* [MainFace.java] 主体面板布局类
*
* 创建日期:(2003-8-2)
* @author:ONE_Fox
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class MainFace extends JFrame {
//操作面板---------------------//
private JPanel searchFace = null;
private JPanel addFace = null;
private JPanel countFace = null;
//界面组件---------------------//
private JRadioButton search = new JRadioButton("查询", true);
private JRadioButton add = new JRadioButton("添加");
private JRadioButton count = new JRadioButton("统计");
private ButtonGroup BTGroup = new ButtonGroup(); private Container contentPane = this.getContentPane();
/**
* 构造方法:
* @param searchFace JPanel 搜索面板
* @param addFace JPanel 添加面板
* @param countFace JPanel 统计面板
*/
public MainFace(JPanel searchFace, JPanel addFace, JPanel countFace) {
this.searchFace = searchFace;
this.addFace = addFace;
this.countFace = countFace;
//界面制作------------------------//
makeFace();
addListener();
}
/**
* 方法:面板构建
*/
private void makeFace() {
BTGroup.add(search);
BTGroup.add(add);
BTGroup.add(count); JPanel upPanel = new JPanel();
upPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 3));
upPanel.add(search);
upPanel.add(add);
upPanel.add(count); //总体布局----------------------//
contentPane.add(upPanel, BorderLayout.NORTH);
contentPane.add(searchFace, BorderLayout.CENTER);
}
/**
* 方法:面板显示
*/
public void showFace() {
this.setTitle("房改房资料管理系统");
setSize(550,350);
Dimension dime = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(dime.width/2 - 275, dime.height/2 - 175);
setResizable(false);
show();
}
/**
* 方法:事件监听
*/
private void addListener() {
//添加窗口关闭事件------------------//
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if(dbload.Loader.getLoader().disconnect()) {
dispose();
System.exit(0);
}
else {
dispose();
System.exit(1);
}
}
});
//"查询" 按钮----------------------//
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
contentPane.remove(addFace);
contentPane.remove(countFace);
contentPane.add(searchFace, BorderLayout.CENTER);
contentPane.repaint();
show();
}
});
//"添加" 按钮---------------------//
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
contentPane.remove(searchFace);
contentPane.remove(countFace);
contentPane.add(addFace, BorderLayout.CENTER);
contentPane.repaint();
show();
}
});
//"统计" 按钮---------------------//
count.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
contentPane.remove(searchFace);
contentPane.remove(addFace);
contentPane.add(countFace, BorderLayout.CENTER);
contentPane.repaint();
show();
}
});
}
}
你可以参考一下思路何实现方法。 (献丑了)
===============================================================
/**
* [MainFace.java] 主体面板布局类
*
* 创建日期:(2003-8-2)
* @author:ONE_Fox
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;public class MainFace extends JFrame {
//操作面板---------------------//
private JPanel searchFace = null;
private JPanel addFace = null;
private JPanel countFace = null;
//界面组件---------------------//
private JRadioButton search = new JRadioButton("查询", true);
private JRadioButton add = new JRadioButton("添加");
private JRadioButton count = new JRadioButton("统计");
private ButtonGroup BTGroup = new ButtonGroup(); private Container contentPane = this.getContentPane();
/**
* 构造方法:
* @param searchFace JPanel 搜索面板
* @param addFace JPanel 添加面板
* @param countFace JPanel 统计面板
*/
public MainFace(JPanel searchFace, JPanel addFace, JPanel countFace) {
this.searchFace = searchFace;
this.addFace = addFace;
this.countFace = countFace;
//界面制作------------------------//
makeFace();
addListener();
}
/**
* 方法:面板构建
*/
private void makeFace() {
BTGroup.add(search);
BTGroup.add(add);
BTGroup.add(count); JPanel upPanel = new JPanel();
upPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 3));
upPanel.add(search);
upPanel.add(add);
upPanel.add(count); //总体布局----------------------//
contentPane.add(upPanel, BorderLayout.NORTH);
contentPane.add(searchFace, BorderLayout.CENTER);
}
/**
* 方法:面板显示
*/
public void showFace() {
this.setTitle("房改房资料管理系统");
setSize(550,350);
Dimension dime = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(dime.width/2 - 275, dime.height/2 - 175);
setResizable(false);
show();
}
/**
* 方法:事件监听
*/
private void addListener() {
//添加窗口关闭事件------------------//
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
if(dbload.Loader.getLoader().disconnect()) {
dispose();
System.exit(0);
}
else {
dispose();
System.exit(1);
}
}
});
//"查询" 按钮----------------------//
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
contentPane.remove(addFace);
contentPane.remove(countFace);
contentPane.add(searchFace, BorderLayout.CENTER);
contentPane.repaint();
show();
}
});
//"添加" 按钮---------------------//
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
contentPane.remove(searchFace);
contentPane.remove(countFace);
contentPane.add(addFace, BorderLayout.CENTER);
contentPane.repaint();
show();
}
});
//"统计" 按钮---------------------//
count.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
contentPane.remove(searchFace);
contentPane.remove(addFace);
contentPane.add(countFace, BorderLayout.CENTER);
contentPane.repaint();
show();
}
});
}
}
但是如果我需要切换的面板很多,是不是也要用remove来切换?
=============================
我不保证上面的做法是最好的,
但是我试过这样的流程再替换显示界面时不会有闪烁