从 JList 中删除项目
Removing items from a JList
这是一个代码,我想更改 JList
项目,但是当我单击打开按钮并且 JList.removeAll()
运行时,我的 JList
没有删除项目...
有什么问题?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JListTest {
public static void main(String[] args) {
String[] j = {"item1","item2","item3"};
final JList list = new JList(j);
JButton open = new JButton("open");
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
list.removeAll();
}
});
JFrame frame = new JFrame();
frame.setSize(400, 400);
Container con = frame.getContentPane();
con.setLayout(new BorderLayout());
con.add(open,BorderLayout.LINE_START);
con.add(list,BorderLayout.CENTER);
con.add(new JScrollPane(list));
frame.setVisible(true);
}
}
不信请测试
你的做法不对。对于你的构造函数new JList(j)
,只有一个"read- only model"。
http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html
It's easy to display an array or Vector of objects, using the JList constructor that automatically builds a read-only ListModel instance for you:
您应该使用真实模型,例如:
public class JListTest {
public static void main(String[] args) {
DefaultListModel<String> model = new DefaultListModel<>();
model.addElement("item1");
model.addElement("item2");
model.addElement("item3");
final JList<String> list = new JList<String>(model);
JButton open = new JButton("open");
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
model.removeAllElements();
}
});
JFrame frame = new JFrame();
frame.setSize(400, 400);
Container con = frame.getContentPane();
con.setLayout(new BorderLayout());
con.add(open, BorderLayout.LINE_START);
con.add(list, BorderLayout.CENTER);
con.add(new JScrollPane(list));
frame.setVisible(true);
}
}
这是一个代码,我想更改 JList
项目,但是当我单击打开按钮并且 JList.removeAll()
运行时,我的 JList
没有删除项目...
有什么问题?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JListTest {
public static void main(String[] args) {
String[] j = {"item1","item2","item3"};
final JList list = new JList(j);
JButton open = new JButton("open");
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
list.removeAll();
}
});
JFrame frame = new JFrame();
frame.setSize(400, 400);
Container con = frame.getContentPane();
con.setLayout(new BorderLayout());
con.add(open,BorderLayout.LINE_START);
con.add(list,BorderLayout.CENTER);
con.add(new JScrollPane(list));
frame.setVisible(true);
}
}
不信请测试
你的做法不对。对于你的构造函数new JList(j)
,只有一个"read- only model"。
http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html
It's easy to display an array or Vector of objects, using the JList constructor that automatically builds a read-only ListModel instance for you:
您应该使用真实模型,例如:
public class JListTest {
public static void main(String[] args) {
DefaultListModel<String> model = new DefaultListModel<>();
model.addElement("item1");
model.addElement("item2");
model.addElement("item3");
final JList<String> list = new JList<String>(model);
JButton open = new JButton("open");
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
DefaultListModel<String> model = (DefaultListModel<String>) list.getModel();
model.removeAllElements();
}
});
JFrame frame = new JFrame();
frame.setSize(400, 400);
Container con = frame.getContentPane();
con.setLayout(new BorderLayout());
con.add(open, BorderLayout.LINE_START);
con.add(list, BorderLayout.CENTER);
con.add(new JScrollPane(list));
frame.setVisible(true);
}
}