代码和要求如下,大大们帮帮忙!不明白要求的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);
}
}
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);
}
}
list.add...
checkBox.add...
comboBox.add...
......如何?
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);
}
}