我在一个JFrame里面加了多个JPanel,每个JPanel都用不同的类编写的。
我想在其中一个JPanel(这里叫做jPanel1)里面添加一个按钮监听来控制另外一个JPanel(比如jPanel2)里面文本框显示的内容。
请问是否能只在jPanel类里面做相应的操作就可以获取jPanel2,并对其进行修改。

解决方案 »

  1.   

    .getParent()方法
    我这里有一个例子
    JFRAME里面有一个JPanel里面又有一个JPanel,这个JPanel里面有一个关闭按钮。
    也是分开写的。
    ((JFrame)((JRootPane)((JLayeredPane)((JPanel)((JPanel)SearchAll.this.getParent()).getParent()).getParent()).getParent()).getParent()).dispose();
      

  2.   

    我还不是很明白
    用getParent()方法获取了父组件后如何再获取另外一个jPanel并对其进行操作?
      

  3.   

    CustomizePanel_1的代码
    import java.awt.BorderLayout;import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class CustomizePanel_1 extends JPanel {    private JButton btn = new JButton();    public CustomizePanel_1() {
            super();
            this.init();
        }    private void init() {
            this.setLayout(new BorderLayout());
            this.add(new JLabel("CustomizePanel_1"));
            this.add(this.btn, BorderLayout.SOUTH);
        }    public JButton getControlBtn() {
            return this.btn;
        }
    }CustomizePanel_2的代码
    import java.awt.BorderLayout;import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;public class CustomizePanel_2 extends JPanel {    private JTextField tf = new JTextField();    public CustomizePanel_2() {
            super();
            this.init();
        }    private void init() {
            this.setLayout(new BorderLayout());
            this.add(new JLabel("CustomizePanel_2"));
            this.add(this.tf, BorderLayout.SOUTH);
        }    public JTextField getTextField() {
            return this.tf;
        }
    }TestClass的代码
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;public class TestClass extends JFrame {    public TestClass() {
            super();
            this.init();
        }    private void init() {
            Container contentPane = this.getContentPane();
            contentPane.setLayout(new GridLayout(1, 2));
            CustomizePanel_1 customizePanel_1 = new CustomizePanel_1();
            contentPane.add(customizePanel_1);
            final CustomizePanel_2 customizePanel_2 = new CustomizePanel_2();
            contentPane.add(customizePanel_2);
            JButton btn = customizePanel_1.getControlBtn();
            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JTextField textField = customizePanel_2.getTextField();
                    String text = textField.getText();
                    // your logoc
                    if (text == null || "".equals(text)) {
                        textField.setText("change text");
                    }
                }
            });
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            TestClass tc = new TestClass();
            tc.setVisible(true);
            tc.pack();
            tc.setDefaultCloseOperation(EXIT_ON_CLOSE);
        }}
    实现方式不是很好,你看下是不是你想要得效果
      

  4.   

     JFrame jf=(JFrame)getRootPane().getParent();