国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

App下載
首頁javajcomboboxJava Swing - 如何將自定義Java對(duì)象添加到JComboBox

Java Swing - 如何將自定義Java對(duì)象添加到JComboBox

我們想知道如何將自定義Java對(duì)象添加到JComboBox。
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class Main {

  public static void main(String argv[]) throws Exception {
    JComboBox<Item> comboBox = new JComboBox<Item>(new Item[] {
        new Item("Major", "red"), new Item("Critical", "dark"),
        new Item("Minor", "green") });
    comboBox.addActionListener(e -> {
        JComboBox<Item> combo = (JComboBox<Item>) e.getSource();
        Item item = (Item) combo.getSelectedItem();
        System.out.println(item.getColor());
    });
    JFrame frame = new JFrame();
    frame.add(comboBox);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

class Item {

  private String name;
  private String color;

  public Item(String name, String color) {
    this.name = name;
    this.color = color;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getColor() {
    return color;
  }

  public void setColor(String color) {
    this.color = color;
  }

  @Override
  public String toString() {
    return name;
  }
}