怎样删除JComboBox组件中的一个条目?比如说之前有“苹果”这一项,删除以后下拉列表中就没了。
1个回答
展开全部
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* @author Hardneedl
*/
final class JComboBoxDemo extends JFrame {
public String getTitle() {return "JComboBoxDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
private JComboBox comboBox;
private Vector<Person> v= new Vector<Person>();
{
v.add(new Person("w","01"));
v.add(new Person("d","21"));
v.add(new Person("da","11"));
v.add(new Person("bbb","3"));
v.add(new Person("v","4"));
}
/*数据对象*/
private class Person{
private String name,id;
private Person(String name, String id) {
this.name = name;
this.id = id;
}
private String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
private String getId() {
return id;
}
private void setId(String id) {
this.id = id;
}
public String toString() {
return getName();
}
public boolean equals(Object obj) {
if(obj instanceof Person){
Person p=(Person)obj;
String id = p.getId();
return id!=null&&id.equals(getId());
}
return false;
}
}
JComboBoxDemo() throws HeadlessException {
init();
attachListeners();
doLay();
}
private void init(){
comboBox = new JComboBox(new DefaultComboBoxModel(v));
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
container.add(comboBox,BorderLayout.NORTH);
container.add(
new JButton(new AbstractAction("delete item 'bbb'"){
public void actionPerformed(ActionEvent e) {
v.remove(new Person("bbb","3"));
}
}),
BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String...args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new JComboBoxDemo();
}
}
);
}
}
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* @author Hardneedl
*/
final class JComboBoxDemo extends JFrame {
public String getTitle() {return "JComboBoxDemo";}
static private final Dimension size = new Dimension(600,400);
public Dimension getPreferredSize() {return size;}
public Dimension getMaximumSize() {return size;}
public Dimension getMinimumSize() {return size;}
public Dimension getSize(){return size;}
private JComboBox comboBox;
private Vector<Person> v= new Vector<Person>();
{
v.add(new Person("w","01"));
v.add(new Person("d","21"));
v.add(new Person("da","11"));
v.add(new Person("bbb","3"));
v.add(new Person("v","4"));
}
/*数据对象*/
private class Person{
private String name,id;
private Person(String name, String id) {
this.name = name;
this.id = id;
}
private String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
private String getId() {
return id;
}
private void setId(String id) {
this.id = id;
}
public String toString() {
return getName();
}
public boolean equals(Object obj) {
if(obj instanceof Person){
Person p=(Person)obj;
String id = p.getId();
return id!=null&&id.equals(getId());
}
return false;
}
}
JComboBoxDemo() throws HeadlessException {
init();
attachListeners();
doLay();
}
private void init(){
comboBox = new JComboBox(new DefaultComboBoxModel(v));
}
private void attachListeners(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void doLay(){
Container container = getContentPane();
container.add(comboBox,BorderLayout.NORTH);
container.add(
new JButton(new AbstractAction("delete item 'bbb'"){
public void actionPerformed(ActionEvent e) {
v.remove(new Person("bbb","3"));
}
}),
BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String...args) {
System.setProperty("swing.defaultlaf","com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.invokeLater(
new Runnable(){
public void run() {
new JComboBoxDemo();
}
}
);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询