我制作了一个更简单的程序版本,但仍然有一个问题,我相信 ActionPerformed 发送了数据,但 JList 无法识别它,或者基本上不希望收到它。所以这就是我到目前为止所做的。所以这是我的更多研究和尝试,也许它提供了有关问题的更多细节。
主窗口:
public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new ClassA();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
A类:
public class ClassA extends JFrame {
DefaultListModel<String> myList;
JList list;
public ClassA() {
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
myList = new DefaultListModel<String>();
list = new JList(myList);
//ClassB sendsText = new ClassB(myList, list);
JButton find = new JButton("Find");
find.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
new ClassB().setVisible(true);
}
});
add(panel);
panel.add(find);
panel.add(list);
}
}
B类:
public class ClassB extends JFrame {
DefaultListModel<String> myList;
JList list;
public ClassB(DefaultListModel<String> myList, JList list){
this.myList = myList;
this.list = list;
}
public ClassB() {
setSize(300,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,1));
JButton addMe = new JButton("Add Me");
addMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
myList.addElement("Danial");
}
});
add(panel);
panel.add(addMe);
}
}