簡體   English   中英

Java 8 Swing 中是否有列表 gui 組件?

[英]Is there a list gui component in Java 8 Swing?

我希望列表的每個元素都有多個組件。 也許是一個標題,它下面的一些文字,甚至是一個按鈕? 那可能嗎? 我希望找到類似於 SwiftUI 的列表視圖的東西。

像這樣: https : //www.appcoda.com/wp-content/uploads/2019/06/image-8-1024x607.png (沒有足夠的聲譽來添加圖像)

它不是很詳細,但Java 教程提到您可以使用自定義ListCellRenderer 那應該做的工作!

也許您想嘗試使用NetBeans和它的 GUI 構建器?

在此處輸入圖片說明

在右側面板上,您有一個Palette ,它可以讓您很好地概覽Swing可用的組件。

至於List本身,您可能正在查看如下內容: List

@ewramner 的回答是正確的。 自定義ListCellRenderer將完成它。 這是一個使用JPanel作為渲染組件的示例,其中包括一個嵌套的JPanel (名為rightPanel )。

(代碼中的一些注釋)

public class MultiComponentListCellRenderer {

    public static void main(String[] args) {
        Person mike = new Person("Mike", 25);
        Person alice = new Person("Alice", 30);
        DefaultListModel<Person> model = new DefaultListModel<>();
        model.addElement(mike);
        model.addElement(alice);
        JList<Person> personJList = new JList<>(model);
        personJList.setCellRenderer(new PersonListCellRenderer());
        JOptionPane.showMessageDialog(null, personJList);
    }

    public static class Person {
        private String name;
        private int age;

        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
    }

    public static class PersonListCellRenderer extends JPanel implements ListCellRenderer<Person> {
        private JLabel nameLabel;
        private JLabel ageLabel;
        private JLabel iconLabel;
        private JPanel rightPanel;

        public PersonListCellRenderer() {
            super(new BorderLayout());
            nameLabel = new JLabel();
            ageLabel = new JLabel();
            iconLabel = new JLabel();
            try {
                Image personImg = ImageIO.read(new URL("https://cdn2.iconfinder.com/data/icons/people-80/96/Picture1-512.png"));
                ImageIcon personIcon = new ImageIcon(personImg.getScaledInstance(20, 20, Image.SCALE_SMOOTH));
                iconLabel.setIcon(personIcon);
            } catch (IOException e) {
                e.printStackTrace();
            }

            add(iconLabel, BorderLayout.LINE_START);

            //Right panel will contain name and age, top and bottom respectively 
            rightPanel = new JPanel(new BorderLayout());
            rightPanel.add(nameLabel, BorderLayout.PAGE_START);
            rightPanel.add(ageLabel, BorderLayout.PAGE_END);

            add(rightPanel, BorderLayout.CENTER);
            setOpaque(true); //for visible backgrounds
            rightPanel.setOpaque(true);//for visible backgrounds
        }

        @Override
        public Component getListCellRendererComponent(JList<? extends Person> list, Person value, int index, boolean isSelected,
                boolean cellHasFocus) {
            nameLabel.setText(value.name);
            ageLabel.setText(String.valueOf(value.age));
            if (isSelected) {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
                rightPanel.setBackground(list.getSelectionBackground());
                rightPanel.setForeground(list.getSelectionForeground());
            } else {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
                rightPanel.setBackground(list.getBackground());
                rightPanel.setForeground(list.getForeground());
            }
            return this;
        }

    }
}

結果:

預覽

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM