我有一个程序需要接受用户输入(来自输入框)并将其添加到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();
}
}