一个窗口(jFrame) 用JSplitPanel将其分隔成上,下两部分 上面部分往里放了个面板(JPanela),下面部分也往里放了个面板(JPanelb) 
    现在在 JPanela 中有几个按钮,想通过点击各个按钮实现各个面板之间的切换显示(这里的面板是独自的面板类) 并在JPanelb中显示出来. 
  
    原来我的方法是(在按钮的单击事件中写代码): 
            JPanelc pa = new JPanelc();  //JPanelc 为自己写的面板类名 
              jPanelb.add(pa);       我这方法是行不通,不知道哪里有问题或是少了些什么,忘哪位知道的说下,先谢谢...      这个窗体是只有两个面板,但我的意思是想通过点击JPanela中的按钮,在JPanelb中显示另一个面板(这个面板是我新建的一个面板类,比如: 
              public class Panelc extendx JPanel{ 
                    ...面板的内容... 
              } 
我的意思就是通过点击JPanela中的按钮,在JPanelb中显示Panelc,就这样. 

解决方案 »

  1.   

    这样吗?import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.border.TitledBorder;public class ChangePanel extends JFrame {    private JSplitPane split = new JSplitPane();    private JPanel panelUp = new JPanel();    private JPanel panelDown = new JPanel();    private JButton btn = new JButton();    private static int count = 0;    public ChangePanel() {
            this.btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel();
                    panel.setBorder(new TitledBorder("JPanel" + count));
                    ChangePanel.this.split.setBottomComponent(panel);
                    count++;
                }
            });
            this.panelUp.add(this.btn);
            this.split.setOrientation(JSplitPane.VERTICAL_SPLIT);
            this.split.setTopComponent(this.panelUp);
            this.split.setBottomComponent(this.panelDown);
            this.getContentPane().add(this.split);
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            ChangePanel cp = new ChangePanel();
            cp.setVisible(true);
            cp.setSize(300, 200);
            cp.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }}
      

  2.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【xiaok_312】截止到2008-06-24 09:25:08的历史汇总数据(不包括此帖):
    发帖数:6                  发帖分:100                
    结贴数:5                  结贴分:80                 
    未结数:1                  未结分:20                 
    结贴率:83.33 %            结分率:80.00 %            
    楼主加油
      

  3.   

    还是这样?import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.border.TitledBorder;public class ChangePanel extends JFrame {    private JSplitPane split = new JSplitPane();    private JPanel panelUp = new JPanel();    private JPanel panelDown = new JPanel();    private JButton btn = new JButton();    private static int count = 0;    public ChangePanel() {
            this.panelDown.setLayout(new BorderLayout());
            this.btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel();
                    panel.setBorder(new TitledBorder("JPanel" + count));
                    ChangePanel.this.panelDown.removeAll();
                    ChangePanel.this.panelDown.add(panel, BorderLayout.CENTER);
                    ChangePanel.this.panelDown.validate();
                    count++;
                }
            });
            this.panelUp.add(this.btn);
            this.split.setOrientation(JSplitPane.VERTICAL_SPLIT);
            this.split.setTopComponent(this.panelUp);
            this.split.setBottomComponent(this.panelDown);
            this.getContentPane().add(this.split);
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            ChangePanel cp = new ChangePanel();
            cp.setVisible(true);
            cp.setSize(300, 200);
            cp.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }}
      

  4.   

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.border.TitledBorder;public class ChangePanel extends JFrame {    private JSplitPane split = new JSplitPane();    private JPanel panelUp = new JPanel();    private JPanel panelDown = new JPanel();    private JButton btn = new JButton();    private static int count = 0;    public ChangePanel() {
            this.panelDown.setLayout(new BorderLayout());
            this.btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel();
                    panel.setBorder(new TitledBorder("JPanel" + count));
                    ChangePanel.this.panelDown.removeAll();
                    ChangePanel.this.panelDown.add(panel, BorderLayout.CENTER);
                    ChangePanel.this.panelDown.validate();
                    count++;
                }
            });
            this.panelUp.add(this.btn);
            this.split.setOrientation(JSplitPane.VERTICAL_SPLIT);
            this.split.setTopComponent(this.panelUp);
            this.split.setBottomComponent(this.panelDown);
            this.getContentPane().add(this.split);
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            ChangePanel cp = new ChangePanel();
            cp.setVisible(true);
            cp.setSize(300, 200);
            cp.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }}
      

  5.   


      不好意思,昨天晚上实在扛不住~睡着了
        今天早上起来自己看API把问题解决了,现在才发现原来那么简单!
      唉,怪自己平时看的少~~~`
       
         解决方法是:(在按钮单击事件中写代码)        ZongheGN xxxg = new ZongheGN();  //创建面板对象,(单击按钮后,下面要显示的面板)
            jPanel2.removeAll();             //移除原先面板中的所有组件
            jPanel2.add(xxxg);               //将要显示的面板添加到面板
            jPanel2.repaint();               //刷新面板    ----之前就是少了这句,不更新显示不了.
            jPanel2.setVisible(true);
    ---------------------------------------------------------------------------------------    不过还是谢谢二楼你的回答,我运行了一下你的代码,正好可以实现我想要的结果,呵..         到此,结了!