gpt4 book ai didi

java - 如何使特定标签在 JList 中闪烁

转载 作者:行者123 更新时间:2023-11-29 05:59:43 25 4
gpt4 key购买 nike

我已经设法将面板用作 JList 的单元格呈现组件。面板中包含的闪烁标签本身就可以正常工作。但在我将 render 加入列表后,标签停止闪烁。

我通过以一定间隔刷新列表来解决闪烁问题,但是这次列表中的所有标签都开始闪烁(我只希望列表中的某些标签符合条件闪烁)。我已经钻研了几个小时,试图解决这个问题,但机会看起来很渺茫。

我的问题有两层:

  1. 为什么独立的眨眼标签证明面板需要JList 来刷新以查看眨眼?
  2. 为什么在渲染过程中没有挑出特定标签导致列表中的所有标签载歌载舞?

最佳答案

SSCCE对我有用:

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

@SuppressWarnings("serial")
public class BlinkingLabelInList extends JPanel {
public static final Color FLASH_COLOR = Color.red;
public static final int TIMER_DELAY = 500;
private String[] data = {"Mon", "Tues", "Wed", "Thurs", "Fri"};
private JList list = new JList(data);
public Color cellColor = null;


public BlinkingLabelInList() {
add(new JScrollPane(list));
list.setCellRenderer(new MyListCellRenderer());
new Timer(TIMER_DELAY, new TimerListener()).start();
}

private class TimerListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
cellColor = (cellColor == null) ? FLASH_COLOR : null;
list.repaint();
}
}

private class MyListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component cellRenderer = super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
if (isSelected || cellHasFocus) {
cellRenderer.setForeground(cellColor );
} else {
cellRenderer.setForeground(null);
}
return cellRenderer;
}

}

private static void createAndShowGui() {
BlinkingLabelInList mainPanel = new BlinkingLabelInList();

JFrame frame = new JFrame("BlinkingLabelInList");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - 如何使特定标签在 JList 中闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10669342/

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