有人可以帮我解决以下问题吗?我想实现一个动作侦听器,该动作侦听器将有关突出显示的Jlist对象的信息输出到已经存在的textarea中。

这是我设法制作的代码:

 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;

  public class ListModel extends JFrame {

   private JList list;
   private DefaultListModel model;
   private JTextField inputf;
   private JTextField inpute;
   private JTextArea text;
   private JPanel p1;
   private JPanel p2;

    public ListModel() {
      setLayout(new FlowLayout());
      model = new DefaultListModel();
      list = new JList(model);
      JButton addButton = new JButton( "Lägg till" );
      model.addElement("Kajsa Åslund");
      model.addElement("Erik Carlsson");
      model.addElement("John Åkesson");
      model.addElement("Per-arne Ingvarsson");
      model.addElement("Rebecka Asp");
      model.addElement("Linnéa Åslund");
      model.addElement("Åsa-Nisse Strong");
      model.addElement("Super Lasse");
      model.addElement("Alexander Ahl");
      model.addElement("Ann Ahl");
      model.addElement("Bo Sten");
     addButton.addActionListener(
     new ActionListener() {
        public void actionPerformed( ActionEvent event )
                     {
                     final String name=inputf.getText() + " " + inpute.getText();

                     model.addElement( name );
                 }
             }
             );
     JButton removeButton =
     new JButton( "Ta bort" );

     removeButton.addActionListener(
         new ActionListener() {

         public void actionPerformed( ActionEvent event )
             {
             setTitle("Borttagning");
             try
            {
            Thread.sleep(1000);
            }
            catch(InterruptedException e)
            {
            e.printStackTrace();
            }
             model.removeElement(list.getSelectedValue());
             setTitle("Personer");
         }
     }
     );

     text = new JTextArea(5, 20);

     inputf = new JTextField();
     inputf.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }

    });
     inpute = new JTextField();
     inpute.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }

    });
     list.setSelectionMode(
     ListSelectionModel.SINGLE_SELECTION );

     inpute.setBounds(5, 5, 100, 100);
     inpute.setPreferredSize(new Dimension(120,20));
     inputf.setBounds(10, 10, 150, 150);
     inputf.setPreferredSize(new Dimension(120,20));
     JScrollPane scroll = new JScrollPane(list);
     scroll.setPreferredSize(new Dimension(200,200));

     JPanel p2 = new JPanel();
     p2.add(text);
     p2.add(inputf);
     p2.add(inpute);
     p2.add(addButton);
     p2.add(removeButton);
     p2.setLayout(new BoxLayout(p2,BoxLayout.Y_AXIS));

     JPanel p1 = new JPanel();
     p1.add(scroll);


     Container container = getContentPane();
     container.add(p1);
     container.add(p2);
     container.setLayout(new FlowLayout());

     setDefaultCloseOperation( EXIT_ON_CLOSE );
     setSize( 500, 250 );
     setVisible( true );
 }

   public static void main( String args[] )
         {
         new ListModel();
     }
   }


提前致谢 :)

最佳答案

现在,只能从list读取的数据是名称和姓氏,可以使用ListSelectionListener来实现:

list.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        text.setText(list.getSelectedValue().toString());
    }
});

10-04 10:09