0

我有 2 个窗口。一个有一个空的 JList,另一个有一个按钮。所以我想在按下按钮时将值添加到列表中。这是我的代码,但未完成:

窗口 1

final DefaultListModel<String> favouriteNames = new DefaultListModel<String>();
JList namesList = new JList(favouriteNames);

窗口 2

public class button extends JFrame {

private JList namesList;
private DefaultListModel<String> favouriteNames;

this.namesList = namesList;

 JButton addThis = new JButton("Add");
 addThis.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
          favouriteNames.addElement("Jack");
   }
 });
}
}
4

2 回答 2

3

将 DefaultListModel 的实例传递给构造函数中的 Window 2。

编辑添加:这是在构造函数中传递实例的方式。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class ButtonFrame implements Runnable {

    private JFrame              frame;

    private DefaultListModel    favouriteNames;

    public ButtonFrame(final DefaultListModel favouriteNames) {
        this.favouriteNames = favouriteNames;
    }

    @Override
    public void run() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton addThis = new JButton("Add");
        addThis.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                favouriteNames.addElement("Jack");
            }
        });

        frame.add(addThis);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ButtonFrame(new DefaultListModel()));
    }

}
于 2013-03-28T16:39:40.940 回答
0

我制作了一个更简单的程序版本,但仍然有一个问题,我相信 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);
    }

}
于 2013-03-29T18:30:41.327 回答