代码和要求如下,大大们帮帮忙!不明白要求的PM我。package gui;import java.awt.BorderLayout;
import java.awt.FlowLayout;import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;public class OuterPane extends JPanel {
/**
 * 添加监听,使得InnerPane的任意元素(list,checkBox,comboBox)改变时,OuterPane可以收到该事件。
 * 注意:不要直接使用innerPane.list.add....之类的。我只需要innerPane里有选项改变就通知outerPane,该怎么办?
 */
private InnerPane innerPane;
private JTextField textField;
    public OuterPane() {
     this.setLayout(new BorderLayout());
     JLabel label = new JLabel("这个label在上边");
     this.add(label, BorderLayout.NORTH);
     textField = new JTextField(20);
     this.add(textField, BorderLayout.CENTER);
     innerPane = new InnerPane();   
     this.add(innerPane, BorderLayout.SOUTH);   
    }
    
private static class InnerPane extends JPanel {
private JList list;
private JCheckBox checkBox;
private JComboBox comboBox; public InnerPane() {
this.setLayout(new FlowLayout());
String[] str = { "aaa", "bbb", "ccc" };
list = new JList(str);
this.add(new JScrollPane(list));
checkBox = new JCheckBox("单选", true);
this.add(checkBox);
comboBox = new JComboBox(str);
this.add(comboBox);
}
}

public static void main(String[] args)  {
JFrame frame = new JFrame();
JPanel pane = (JPanel)frame.getContentPane();
pane.add(new OuterPane());
frame.pack();
frame.setVisible(true);
}
}

解决方案 »

  1.   

    关注一下了,毕业以后就一直没做过GUI,忘记差不多了。添加一个listener行吗
      

  2.   

    不明白楼主的意思,或者说为啥要让OuterPane监听到。需要实现啥功能要用OuterPane来监听
      

  3.   

    在InnerPane中定义一个public方法封装:
    list.add...
    checkBox.add...
    comboBox.add...
    ......如何?
      

  4.   

    要outerPane监听到的原因就是想让outerPane里的textField能及时的对innerPane里的改变作出显示上的改变,比如checkBox被点了一下或者list被选中了一个,textField就显示为1,再点一下就显示为0....我的实际用途是在outerPane中即时预览innerPane中的改变,比如说innerPane中有个字体选择面板,那么改变了字体大小,预览的地方就立即改变。
      

  5.   

    在InnerPane中持有OuterPane的引用不是可以实现吗?!将OuterPane作为构造函数传递进来,在事件监听中通过OuterPane的引用来调用OuterPane中的方法
      

  6.   

    就简单给你写个例子,其他也是加监听,需要怎么处理,就在OuterPane中添加相应的方法,便于调用public class OuterPane extends JPanel {
        private InnerPane innerPane;
        private JTextField textField;
        public OuterPane() {
            this.setLayout(new BorderLayout());
            JLabel label = new JLabel("这个label在上边");
            this.add(label, BorderLayout.NORTH);
            textField = new JTextField(20);
            this.add(textField, BorderLayout.CENTER);
            innerPane = new InnerPane(this);       
            this.add(innerPane, BorderLayout.SOUTH);       
        }    public void setMyText(String str){
            textField.setText(str);
        }

        
        private static class InnerPane extends JPanel implements ActionListener{
            private JList list;
            private JCheckBox checkBox;
            private JComboBox comboBox;        private OuterPane op;        public InnerPane(OuterPane op) {
                this.op = op;
                this.setLayout(new FlowLayout());
                String[] str = { "aaa", "bbb", "ccc" };
                list = new JList(str);
                this.add(new JScrollPane(list));
                checkBox = new JCheckBox("单选", true);
                checkBox.addActionListener(this);
                this.add(checkBox);
                comboBox = new JComboBox(str);
                this.add(comboBox);
            }        public void actionPerformed(ActionEvent e){
                if(e.getSource == checkBox){
                    String str = checkBox.getSelected() ? "1" : "0";
                    op.setMyText(str);

                }
            }
        }
        
        public static void main(String[] args)  {
            JFrame frame = new JFrame();
            JPanel pane = (JPanel)frame.getContentPane();
            pane.add(new OuterPane());
            frame.pack();
            frame.setVisible(true);
        }
    }