在Java 8的Swing中是否有列表GUI组件?

huangapple go评论61阅读模式
英文:

Is there a list gui component in Java 8 Swing?

问题

我希望列表中的每个元素都有多个组件。也许有一个标题,标题下面是一些文本,甚至可能有一个按钮?这是否可能?我希望能找到类似于SwiftUI的列表视图。

类似于这样的效果:链接(由于声望不够,无法添加图片)

英文:

I would like each element of the list to have multiple components. Maybe a title, some text below it or even a button? Is that possible? I'm hoping to find something similar to SwiftUI's list view.

Something like this: https://www.appcoda.com/wp-content/uploads/2019/06/image-8-1024x607.png (not enough reputation to add images)

答案1

得分: 2

@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;
        }
        
    }
}

结果:

在Java 8的Swing中是否有列表GUI组件?

英文:

@ewramner's answer is correct. A custom ListCellRenderer will get it done. Here is an example using a JPanel as the renderering component which includes a nested JPanel (named rightPanel).

(Some comments inside code)

public class MultiComponentListCellRenderer {
public static void main(String[] args) {
Person mike = new Person(&quot;Mike&quot;, 25);
Person alice = new Person(&quot;Alice&quot;, 30);
DefaultListModel&lt;Person&gt; model = new DefaultListModel&lt;&gt;();
model.addElement(mike);
model.addElement(alice);
JList&lt;Person&gt; personJList = new JList&lt;&gt;(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&lt;Person&gt; {
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(&quot;https://cdn2.iconfinder.com/data/icons/people-80/96/Picture1-512.png&quot;));
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&lt;? extends Person&gt; 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;
}
}
}

The result:

在Java 8的Swing中是否有列表GUI组件?

答案2

得分: 1

这并不是非常详细,但是Java教程提到你可以使用自定义的ListCellRenderer。那应该可以完成任务!

英文:

It is not very detailed, but the Java tutorial mentions that you can use a custom ListCellRenderer. That should do the job!

答案3

得分: 0

也许你想尝试一下 NetBeans 以及它的图形界面构建工具?

在右侧面板上,你有一个 Palette,它为你展示了在 Swing 中可用的组件,提供了很好的概述。

至于列表(List)本身,你可能正在看类似这样的内容:列表

英文:

Maybe you want to try NetBeans and it's GUI builder?

在Java 8的Swing中是否有列表GUI组件?

On the right panel you have a Palette that gives you pretty good overview of components available in Swing.

As for the List itself, you are probably looking at something like this: List

huangapple
  • 本文由 发表于 2020年8月26日 20:24:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63597631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定