Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java列表集列表项的背景_Java_List_Applet_Awt - Fatal编程技术网

Java列表集列表项的背景

Java列表集列表项的背景,java,list,applet,awt,Java,List,Applet,Awt,如何更改JavaAWT列表项的背景色?我的意思是AWT列表中的一项,而不是全部。因为Java AWT列表继承自组件,所以使用组件的setBackground(颜色c)方法 列表现在是绿色的。因为Java AWT列表继承自组件,所以使用组件的setBackground(c色)方法 列表现在是绿色的。我已经有一段时间没有使用AWT了,但您不能只使用setBackground(颜色)吗?List是java.awt.Component的一个子类。我已经有一段时间没有使用awt了,但是您不能只使用setB

如何更改JavaAWT列表项的背景色?我的意思是AWT列表中的一项,而不是全部。

因为Java AWT列表继承自组件,所以使用组件的setBackground(颜色c)方法


列表现在是绿色的。

因为Java AWT列表继承自组件,所以使用组件的setBackground(c色)方法


列表现在是绿色的。

我已经有一段时间没有使用AWT了,但您不能只使用setBackground(颜色)吗?List是java.awt.Component的一个子类。

我已经有一段时间没有使用awt了,但是您不能只使用setBackground(颜色)吗?List是java.awt.Component的一个子类。

您需要一个自定义渲染器。也就是说,如果您使用Swing。最好坚持使用Swing组件,而不是awt gui组件

JList
...
setCellRenderer(new MyCellRenderer());
...
class MyCellRenderer extends DefaultListCellRenderer
{
  public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
  {
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    Color bg = <calculated color based on value>;
    setBackground(bg);
    setOpaque(true); // otherwise, it's transparent
    return this;  // DefaultListCellRenderer derived from JLabel, DefaultListCellRenderer.getListCellRendererComponent returns this as well.
  }
}
JList
...
setCellRenderer(新的MyCellRenderer());
...
类MyCellRenderer扩展了DefaultListCellRenderer
{
公共组件getListCellRenderComponent(JList列表、对象值、int索引、布尔isSelected、布尔cellHasFocus)
{
getListCellRenderComponent(列表、值、索引、isSelected、cellHasFocus);
彩色bg=;
退避地(bg);
set不透明(true);//否则,它是透明的
返回此值;//从JLabel派生的DefaultListCellRenderer,DefaultListCellRenderer.GetListCellRenderComponent也返回此值。
}
}

您需要一个自定义渲染器。也就是说,如果您使用Swing。最好坚持使用Swing组件,而不是awt gui组件

JList
...
setCellRenderer(new MyCellRenderer());
...
class MyCellRenderer extends DefaultListCellRenderer
{
  public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
  {
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    Color bg = <calculated color based on value>;
    setBackground(bg);
    setOpaque(true); // otherwise, it's transparent
    return this;  // DefaultListCellRenderer derived from JLabel, DefaultListCellRenderer.getListCellRendererComponent returns this as well.
  }
}
JList
...
setCellRenderer(新的MyCellRenderer());
...
类MyCellRenderer扩展了DefaultListCellRenderer
{
公共组件getListCellRenderComponent(JList列表、对象值、int索引、布尔isSelected、布尔cellHasFocus)
{
getListCellRenderComponent(列表、值、索引、isSelected、cellHasFocus);
彩色bg=;
退避地(bg);
set不透明(true);//否则,它是透明的
返回此值;//从JLabel派生的DefaultListCellRenderer,DefaultListCellRenderer.GetListCellRenderComponent也返回此值。
}
}

这将完成整个列表。我希望双击列表项的背景不会改变整个列表。然后将其添加到您的问题中:)是的,现在我正试图找出如何设置列表的行,因为新列表(4,false);没用了:P哈哈呜!那可能是因为我把名单放在了东边界的布局中,所以不管怎么说,它都会散乱。。。啊!我不知道。这个问题一开始并不准确,但现在是。要么调整你的答案,要么删除它。这样就可以完成整个列表。我希望双击列表项的背景不会改变整个列表。然后将其添加到您的问题中:)是的,现在我正试图找出如何设置列表的行,因为新列表(4,false);没用了:P哈哈呜!那可能是因为我把名单放在了东边界的布局中,所以不管怎么说,它都会散乱。。。啊!我不知道。这个问题一开始并不准确,但现在是。要么调整你的答案,要么删除它。触摸,但我已经看了,我是一个无助的矮个子男人,试图跳过一栋楼:(你需要学会不要这么“无助”。否则你需要远离软件开发。好吧,我退出。我现在要洗碗了。触摸,但我已经看过了,我是一个无助的矮人,试图跳过一座大楼:(你需要学会不要这么“无助”。否则你就需要远离软件开发。好吧,我退出了。我现在要洗碗了,我的余生都要洗碗了。)