gpt4 book ai didi

java - 多个ListCell渲染器

转载 作者:行者123 更新时间:2023-12-02 04:14:24 25 4
gpt4 key购买 nike

是否可以在一个类中实现多个ListCellRenderer?

实际上,我的应用程序中有多个 JList,并且每个 JList 都需要不同的 ListCellRenderer。

我可以使用不同的类名来实现 ListCellRenderer 的 Component 方法吗?

例如:如果我有一个名为“MultiColumnCellRenderer”的类,其中包含 Component 方法的某些实现,而另一个类名为“MultiColumnCellRenderer2”,其中包含 Component 方法的某些其他实现?

public class MultiColumnCellRenderer extends JPanel implements
ListCellRenderer {

public MultiColumnCellRenderer() {
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {

// Some implementation of Component Method

super.setEnabled(list.isEnabled());
super.setFont(list.getFont());

return this;
}
}


public class MultiColumnCellRenderer2 extends JPanel implements
ListCellRenderer {

public MultiColumnCellRenderer2() {
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {

// Some implementation of Component Method

super.setEnabled(list.isEnabled());
super.setFont(list.getFont());

return this;
}
}`

如果我做类似的事情:

list1.setCellRenderer(new MultiColumnCellRenderer());
list2.setCellRenderer(new MultiColumnCellRenderer2());

它不起作用......

我正在寻找 list1 和 list2 的不同渲染。

我怎样才能实现这个目标

最佳答案

Is it possible to have multiple ListCellRenderer's implementation in a single class?

如果“拥有”的意思是“使用”,而“类”的意思是“GUI”,那么是的。

这是一个例子:

MultiColumnCellRendererTest

来源

import java.awt.*;
import javax.swing.*;

class MultiColumnCellRendererTest {

public static void main(String[] args) {
final String[] fruits = {
"Apple",
"Pear",
"Banana",
"Grapefruit"
};

SwingUtilities.invokeLater(new Runnable() {
public void run() {
JList fruitList1 = new JList(fruits);
fruitList1.setCellRenderer(new MultiColumnCellRenderer());

JList fruitList2 = new JList(fruits);
fruitList2.setCellRenderer(new MultiColumnCellRenderer2());

JPanel gui = new JPanel(new GridLayout(1,0,2,2));
gui.add(fruitList1);
gui.add(fruitList2);

JOptionPane.showMessageDialog(null, gui);
}
});
}
}

class MultiColumnCellRenderer extends JPanel implements
ListCellRenderer {

JLabel label;

public MultiColumnCellRenderer() {
setBackground(Color.RED);
label = new JLabel();
add(label);
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
label.setText(value.toString());

super.setEnabled(list.isEnabled());
super.setFont(list.getFont());

return this;
}
}


class MultiColumnCellRenderer2 extends JPanel implements
ListCellRenderer {

JLabel label;

public MultiColumnCellRenderer2() {
setBackground(Color.GREEN);
label = new JLabel();
add(label);
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
label.setText(value.toString());

super.setEnabled(list.isEnabled());
super.setFont(list.getFont());

return this;
}
}

关于java - 多个ListCell渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com