這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();
}
});
}
}
想你一定知道該渲染器充當橡皮圖章顯示一個JLabel的圖像,但真的不是*的*的JLabel,而通常這是問題的根源。這就是說,很難說沒有代碼的情況下你做錯了什麼。考慮創建一個小型可編譯的可運行程序,我們可以運行,測試和修改,並顯示您的問題,[sscce](http://sscce.org)。 –
上述標籤是該渲染面板單元格中的子組件。因此,如果個別案件工作(即標籤將閃爍,如果滿足一定條件),爲什麼它會需要列表刷新才能看到效果,因爲所有的這些都應該渲染(getListCellRendererComponent)期間進行大修。代碼片段的這一部分看起來像這樣: – Saint
請查看我的代碼發佈作爲答案,以查看示例SSCCE,以及在JList中閃爍標籤的方式。 –