哪位高手帮我解决个问题:
   一个主窗体(frm_main) 中先用一个分隔面板(JSplitPane)将其分为 上,下 两部分,上部分往里丢了面板(JPanela,面板中数个按钮) ,下部分再是用分隔面板将其分为 左,右 两部分,左边部分往里丢了个面板(JPanelb 布局管理器没有改变),右部分同样也往里丢了个面板(JPanelc, 布局管理器没有改变) ...
    现在我要通过点击 JPanela 中的按钮,在JPanelb中显示我想要显示的面板(该面板为一个独立的面板类 extends Panel ),然后点击JPanelb中新显示的面板中的按钮 再在JPanelc 中显示我想要显示的面板(同样,该面板也为一独立面板类,extends Panel)
      问题是,前一部分是实现了(JPanelb中显示了新的面板),但用同样的方法来设置JPanelc中的面板却不能实现,
原因我知道,因为JPanelb面板中新显示出来的面板为一个独立的面板类,在它其中的按钮事件中,它不能对当前窗体的JPanelc进行操作.
    请问有什么办法可以解决??

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【xiaok_312】截止到2008-06-25 15:37:51的历史汇总数据(不包括此帖):
    发帖数:7                  发帖分:120                
    结贴数:7                  结贴分:120                
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

    你把代码粘上来看看吧.而且你说的原因是错的,JPanelb面板中新显示出来的面板为一个独立的面板类,在它其中的按钮事件中只要能获得JPanelc对象,一样可以实现你的操作
      

  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 JSplitPane splitDown = new JSplitPane();    private JPanel panelUp = new JPanel();    private JPanel panelDownLeft = new JPanel();    private JPanel panelDownRight = new JPanel();    private JButton btn = new JButton();    private static int count = 0;    private JButton downLeftBtn;    private BtnActionListener l = new BtnActionListener();    public ChangePanel() {
            this.panelDownLeft.setLayout(new BorderLayout());
            this.btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel();
                    panel.setBorder(new TitledBorder("JPanel" + count));
                    ChangePanel cp = ChangePanel.this;
                    cp.downLeftBtn = new JButton("JButton" + count);
                    cp.downLeftBtn.addActionListener(cp.l);
                    cp.panelDownLeft.removeAll();
                    panel.add(cp.downLeftBtn);
                    cp.panelDownLeft.add(panel, BorderLayout.CENTER);
                    cp.panelDownLeft.validate();
                    count++;
                }
            });
            this.splitDown.setTopComponent(this.panelDownLeft);
            this.panelDownRight.setLayout(new BorderLayout());
            this.splitDown.setBottomComponent(this.panelDownRight);
            this.splitDown.setDividerLocation(120);        this.panelUp.add(this.btn);
            this.split.setOrientation(JSplitPane.VERTICAL_SPLIT);
            this.split.setTopComponent(this.panelUp);
            this.split.setBottomComponent(this.splitDown);
            this.getContentPane().add(this.split);
        }    private class BtnActionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                JPanel panel = new JPanel();
                panel.setBorder(new TitledBorder("JPanel" + count));
                count++;
                panelDownRight.removeAll();
                panelDownRight.add(panel, BorderLayout.CENTER);
                panelDownRight.validate();
            }
        }    /**
         * @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.   

             楼上的大哥,你说的没错,上次我问过类似的问题,你也回复过我(你记性还真好哈)    可这次情况不同了,看了你上面的代码,你写的还是在同一个窗体下的对象(象panelUp,panelDownLeft 里的按钮,都是在本窗体(ChangePanel)中new 出来的,属于同一对象,当然可以对该对象(ChangePanel)中的其它组件进行操作,     但我现在的问题是"panelDownLeft"单独的面板对象,如:          public class Panelx extends JPanel{
                  JButton btn1= new JButton();
                    ....
               }    然后在主窗体中 panelUp 面板中的按钮单击事件代码如下:       Panelx pa1 = new Panelx();
           panelUp.removeall();
           panelUp.add(pa1);
           panelUp.repaint();
           panelUp.setVisible(true);
         //这样,单击panelUp面板中的按钮时,就将pa1添加到了panelDownLeft中,并正常显示了   问题是,我现在想实现单击 pa1 面板中的按钮 btn1 时,主窗体中的 "panelDownRight"面板又要显示另一个面板,很显然如果要实现这种效果,肯定是要在 Panelx 面板中的 btn1 按钮的单击事件中写代码      问题就是到这里我不知道该怎么写了.
      

  5.   

    那就让左下的panel持有右下panel的对象就行了吧,这种设计本身关联性就很强
    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 JSplitPane splitDown = new JSplitPane();    private JPanel panelUp = new JPanel();    private JPanel panelDownLeft = new JPanel();    private JPanel panelDownRight = new JPanel();    private JButton btn = new JButton();    private static int count = 0;    public ChangePanel() {
            this.panelDownLeft.setLayout(new BorderLayout());
            this.btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ChangePanel cp = ChangePanel.this;
                    X_Panel panel = new X_Panel(cp.panelDownRight);
                    panel.setBorder(new TitledBorder("JPanel" + count));
                    cp.panelDownLeft.removeAll();
                    cp.panelDownLeft.add(panel, BorderLayout.CENTER);
                    cp.panelDownLeft.validate();
                    count++;
                }
            });
            this.splitDown.setTopComponent(this.panelDownLeft);
            this.panelDownRight.setLayout(new BorderLayout());
            this.splitDown.setBottomComponent(this.panelDownRight);
            this.splitDown.setDividerLocation(120);        this.panelUp.add(this.btn);
            this.split.setOrientation(JSplitPane.VERTICAL_SPLIT);
            this.split.setTopComponent(this.panelUp);
            this.split.setBottomComponent(this.splitDown);
            this.getContentPane().add(this.split);
        }    class X_Panel extends JPanel {        private BtnActionListener l = new BtnActionListener();        private JPanel downRight;        private JButton downLeftBtn;        public X_Panel(JPanel downRight) {
                this.downRight = downRight;
                downLeftBtn = new JButton("JButton");
                downLeftBtn.addActionListener(l);
                this.add(downLeftBtn);
            }        private class BtnActionListener implements ActionListener {
                public void actionPerformed(ActionEvent e) {
                    JPanel panel = new JPanel();
                    panel.setBorder(new TitledBorder("JPanel" + count));
                    count++;
                    downRight.removeAll();
                    downRight.add(panel, BorderLayout.CENTER);
                    downRight.validate();
                }
            }
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            ChangePanel cp = new ChangePanel();
            cp.setVisible(true);
            cp.setSize(300, 200);
            cp.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }}
      

  6.   

    如果这样不行,你可以让左下角的panel持有最大的panel对象,也就是rootPane,从rootPane中得到你想要得右下角的panel,这样可能会好一些