有没有办法在 JList 中使用 Enum?

我知道JList有带有Stringand的构造函数DefaultListModel。我想找到一种将列表与enum类型一起使用的方法,例如JList<SomeEnum> jlist。


有办法实现这一点吗?


这是一个小的运行示例:


import javax.swing.*;

import java.awt.*;


public class Example implements Runnable {

    private JPanel jPanel;

    private JFrame jFrame;

    private JList<Algorithm> diagnosisList;

    public static void main(String[] args) {

        EventQueue.invokeLater(new Example());

    }

    @Override

    public void run() {

        jFrame = new JFrame();

        jPanel = new JPanel();

        jPanel.setLayout(new FlowLayout());

        jFrame.getContentPane().add(jPanel);

        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jPanel.setPreferredSize(new Dimension(500, 300));

        DefaultListModel defaultListModel = new DefaultListModel();

        defaultListModel.add(0, "Decision Tree");

        defaultListModel.add(1, "Random Forest");

        diagnosisList = getJList(defaultListModel);


        jPanel.add(diagnosisList);


        jFrame.pack();

        jFrame.setVisible(true);

    }

    // I want to use Algorithm enum for parameter instaed of DefaultListModel

    private JList<Algorithm> getJList(DefaultListModel algorithms) {

        JList<Algorithm> jlist = new JList<Algorithm>(algorithms);

        return jlist;

    }



    enum Algorithm {

        DECISION_TREE_CLASSIFIER_DIAGNOSTIC("Decision Tree Classifier (D)", "Diagnostic"),

        RANDOM_FOREST_DIAGNOSTIC("Random Forest (D)", "Diagnostic"),

        LOGISTIC_REGRESSION_DIAGNOSTIC("Logistic Regression (D)", "Diagnostic"),

        K_MEANS_DIAGNOSTIC("KMeans (D)", "Diagnostic");


        private final String name;

        private final String type;


        Algorithm(final String name, final String type) {

            this.name = name;

            this.type = type;

        }


        @Override public String toString() {

            return name;

        }

    }

}


吃鸡游戏
浏览 63回答 1
1回答

阿波罗的战车

您可以像任何其他数组一样将 an 的值添加enum到 a 中。JList例如:public class Example implements Runnable {&nbsp; &nbsp; private JPanel jPanel;&nbsp; &nbsp; private JFrame jFrame;&nbsp; &nbsp; private JList<Algorithm> diagnosisList;&nbsp; &nbsp; private DefaultListModel<Algorithm> model;&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; EventQueue.invokeLater(new Example());&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; jFrame = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp; jPanel = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; jPanel.setLayout(new FlowLayout());&nbsp; &nbsp; &nbsp; &nbsp; jFrame.getContentPane().add(jPanel);&nbsp; &nbsp; &nbsp; &nbsp; jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; jPanel.setPreferredSize(new Dimension(500, 300));&nbsp; &nbsp; &nbsp; &nbsp; model = new DefaultListModel<>();&nbsp; &nbsp; &nbsp; &nbsp; diagnosisList = new JList<Algorithm>(model);&nbsp; &nbsp; &nbsp; &nbsp; for (Algorithm a : Algorithm.values())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; model.addElement(a);&nbsp; &nbsp; &nbsp; &nbsp; jPanel.add(diagnosisList);&nbsp; &nbsp; &nbsp; &nbsp; jFrame.pack();&nbsp; &nbsp; &nbsp; &nbsp; jFrame.setVisible(true);&nbsp; &nbsp; }&nbsp; &nbsp; public static enum Algorithm {&nbsp; &nbsp; &nbsp; &nbsp; BFS, DFS, A_STAR&nbsp; &nbsp; }}如果您不喜欢它的渲染方式,您可以随时添加自己的ListCellRenderer:diagnosisList.setCellRenderer(new DefaultListCellRenderer() {&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; @Override&nbsp; &nbsp; public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean cellHasFocus) {&nbsp; &nbsp; &nbsp; &nbsp; JLabel renderer = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);&nbsp; &nbsp; &nbsp; &nbsp; renderer.setText(renderer.getText().toLowerCase()); //something&nbsp; &nbsp; &nbsp; &nbsp; return renderer;&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java