0

我有一个程序需要接受用户输入(来自输入框)并将其添加到JList. 但是,当我单击程序上的“添加”按钮时,会出现错误。

这是我希望能工作的代码

JButton addButton = new JButton( "<-Add" );
         addButton.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent event )
                         {
                         final String name=inputField.getText();
                         // prompt user for new philosopher's name

                         // add new philosopher to model
                         philosophers.addElement( name );
                     }
                 }
                 );

编辑:虽然我测试了这部分并且我相信它可以工作,但这是所有代码(除了我试图添加到文本框中的列表器)

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

    public class PhilosophersJList extends JFrame {

     private DefaultListModel philosophers;
     private JList list;
     private JTextField inputField;

     public PhilosophersJList()
         {
         super( "Favorite Philosophers" );

         // create a DefaultListModel to store philosophers
         philosophers = new DefaultListModel();
         philosophers.addElement( "Socrates" );
         philosophers.addElement( "Plato" );
         philosophers.addElement( "Aristotle" );
         philosophers.addElement( "St. Thomas Aquinas" );
         philosophers.addElement( "Soren Kierkegaard" );
         philosophers.addElement( "Immanuel Kant" );
         philosophers.addElement( "Friedrich Nietzsche" );
         philosophers.addElement( "Hannah Arendt" );

         // create a JList for philosophers DefaultListModel
         list = new JList( philosophers );
         JButton addButton = new JButton( "<-Add" );
         addButton.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent event )
                         {
                         final String name=inputField.getText();
                         // prompt user for new philosopher's name

                         // add new philosopher to model
                         philosophers.addElement( name );
                     }
                 }
                 );




         // create JButton for removing selected philosopher
         JButton removeButton =
         new JButton( "Rem->" );

         removeButton.addActionListener(
             new ActionListener() {

             public void actionPerformed( ActionEvent event )
                 {
                 // remove selected philosopher from model
                 setTitle("Now Removing Contact");
                 try 
                {
                Thread.sleep(1000); // do nothing for 1000 miliseconds (1 second)
                } 
                catch(InterruptedException e)
                {
                e.printStackTrace();
                }
                 philosophers.removeElement(list.getSelectedValue());
                 setTitle("My Contacts List");
             }
         }
         );
         JTextField inputField=new JTextField();
         inputField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }

        });

         // allow user to select only one philosopher at a time
         list.setSelectionMode(
         ListSelectionModel.SINGLE_SELECTION );
         //Create the text field


         // create JButton for adding philosophers


         // lay out GUI components
         JPanel inputPanel = new JPanel();
         inputPanel.add( addButton);
         inputPanel.add( removeButton);
         inputPanel.setLayout(new BoxLayout(inputPanel,BoxLayout.Y_AXIS));

         inputField.setLayout(new FlowLayout());
         inputField.setBounds(5, 5, 100, 100);
         inputField.setPreferredSize(new Dimension(120,20));
         JScrollPane scrollPane=new JScrollPane(list);
         scrollPane.setPreferredSize(new Dimension(200,200));

         Container container = getContentPane();
         add(scrollPane);
         container.add( inputPanel);
         add( inputField);
         container.setLayout(new FlowLayout());

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

     } // end PhilosophersJList constructor

     // execute application
       public static void main( String args[] )
             {
             new PhilosophersJList();
         }
    }
4

1 回答 1

6

您不初始化 inputField 字段。问题出在第 69 行,在这里您声明了一个名为 inputField的新局部变量,而不是分配该字段。您需要实际引用 inputField 字段。

所以而不是

JTextField inputField = new JTextField();

你应该只写

inputField = new JTextField();
于 2012-04-09T22:54:31.870 回答