0

我是 Java 编程的新手,我一直在尝试解决这个读取用户输入的简单程序,然后 ADD 按钮将简单地在JList. 删除按钮将简单地删除JList.

我很困惑如何将动作监听器放在代码中,或者获取文本。如果您能帮我解决这个简单的 GUI,我将不胜感激。

它读取(字符串)中的用户输入,JText当我单击添加按钮(可能执行的操作?)时,字符串将基本上填充为JList. 并且“删除”按钮将简单地删除StringJList.

4

1 回答 1

2

第1步

使用方法将代码绑定到特定事件addActionListener

button.addActionListener(new ActionListener({
       public void actionPerformed(ActionEvent e)
       {
            // Bind the method to the button.
       }
});

第2步

使用相关代码填充该方法。

 String value = jTextBox.getText();
 // Grab the String value.
 jListModel.addElement(value);
 // And add it to the list model that informs the JList.

button.addActionListener(new ActionListener({
       public void actionPerformed(ActionEvent e)
       {
              String value = jTextBox.getText();
              // Grab the String value.
              jListModel.addElement(value);
              // And add it to the list.
       }
});

有用的链接

是 Oracle 提供的一个非常好的教程,详细介绍了如何以不同的方式操作列表。

于 2013-07-29T10:28:53.390 回答