编辑:这是@HovercraftFullOfEels 回答后更新的代码:
注意:我没有使用 HashMap,因为我想做几个动作,比如改变颜色和使字体变为粗体或斜体,而答案中提供的 HashMap 仅用于将特定颜色设置为特定枚举类型。
所以,这里是代码:
package src;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import src.MainWindow.LogType;
public class MainWindow {
public enum LogType {
ERROR("Error"), COMMENT("Comment"), READ_INFO("Read_Info"), ACTION(
"Action"), PARAM_ERROR("Param_Error");
String text;
private LogType(String s) {
this.text = s;
}
public void setText(String s) {
this.text = s;
}
public String getText() {
return text;
}
};
private JFrame frmNavdng;
private static DefaultListModel<LogType> logListModel = new DefaultListModel<>();
private static JList<LogType> log_List = new JList<>(logListModel);
public MainWindow() {
initialize();
}
public static void add_Log_Line(LogType type, String s) {
// add action to the log list
type.setText(s);
logListModel.addElement(type);
log_List.ensureIndexIsVisible(log_List.getLastVisibleIndex());
}
private void initialize() {
/** Frame Initialisation */
frmNavdng = new JFrame();
frmNavdng.setFont(new Font("Tahoma", Font.PLAIN, 12));
frmNavdng.setTitle("list_window");
frmNavdng.setResizable(false);
frmNavdng.setBounds(100, 100, 640, 400);
frmNavdng.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmNavdng.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 0, frmNavdng.getWidth(), frmNavdng.getHeight());
frmNavdng.getContentPane().add(scrollPane);
scrollPane.setViewportView(log_List);
log_List.setToolTipText("history of the last actions");
log_List.setVisibleRowCount(10);
log_List.setValueIsAdjusting(true);
log_List.setFont(new Font("Tahoma", Font.PLAIN, 10));
log_List.setCellRenderer(new MyCellRenderer());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frmNavdng.setVisible(true);
for (int i = 0; i <= 5; i++) {
add_Log_Line(LogType.ACTION, ("loop " + i));
}
} catch (Exception e) {
e.toString();
}
}
});
}
}
class MyCellRenderer extends DefaultListCellRenderer {
private static final long serialVersionUID = 1L;
public MyCellRenderer() {
setOpaque(true);
}
@Override
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
LogType lt = (LogType) value;
value = lt.getText();
Color background = Color.white;
Color foreground = Color.black;
Font font = list.getFont();
if (lt == LogType.ACTION) {
foreground = Color.blue;
}
if (lt == LogType.COMMENT) {
foreground = Color.darkGray;
}
if (lt == LogType.ERROR) {
foreground = Color.red;
font = new Font(list.getFont().getFontName(), Font.BOLD, list
.getFont().getSize());
}
if (lt == LogType.READ_INFO) {
foreground = Color.magenta;
font = new Font(list.getFont().getFontName(), Font.ITALIC, list.getFont().getSize());
}
if (lt == LogType.PARAM_ERROR) {
foreground = Color.orange;
font = new Font(list.getFont().getFontName(), Font.BOLD, list
.getFont().getSize());
}
if (isSelected)
background = Color.lightGray;
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
c.setForeground(foreground);
c.setBackground(background);
c.setFont(font);
return c;
}
}
编辑2:这是我得到的和我想要得到的说明:
如果我运行@HovercraftFullofEels 给出的代码,稍作修改以更改文本:
for(int i = 0; i<= 10; i++)
{
LogType logType = LogType.values()[i];
//just added this line
logType.setText("Type : " + logType.toString() + " - Loop : " + i);
logListModel.addElement(logType);
}
我得到了这个结果: 每种类型的日志都会得到每个日志的最后一个文本......如果他们能得到我一开始给他们的文本,那就太好了,这样我就可以显示像“打开失败”这样的文本文件”带有ERROR类型和样式,“这个文件中给定的参数错误,将被忽略”带有READ_INFO样式和类型,“结果写入文件result.txt”作为ACTION,例如.
使用给出的代码,我不知道如何以及在何处设置此文本并将其设置为“静态”(这意味着它不会在此处更改)。
- - - - - - - - - 老问题 - - - - - - - - -
我使用JList作为日志查看器,我想添加一些不同颜色的行。例如,我有一个COMMENT、ACTION和ERRORenum
类型。COMMENT为深灰色,ACTION为蓝色,ERROR为红色。
我试图进行自定义ListCellRendered
,但我无法弄清楚如何根据enum
.
这是我的代码:
public class MainWindow {
/* all the other declarations... */
private static JList<String> log_List;
/* configuration of the list somewhere in the code */
log_List = new JList<String>(new DefaultListModel<String>());
scrollPane.setViewportView(log_List);
log_List.setToolTipText("history of the last actions");
log_List.setVisibleRowCount(10);
log_List.setValueIsAdjusting(true);
log_List.setCellRenderer(new MyCellRenderer());
public enum LOG_TYPE {
ERROR, COMMENT, READ_INFO, ACTION
};
// add action to the log list
public static void add_Log_Line(LOG_TYPE type, String s) {
//what I did before, but not working...
/* if (type == LOG_TYPE.ERROR) log_List.setForeground(Color.red);
if (type == LOG_TYPE.ACTION) log_List.setForeground(Color.blue);
if (type == LOG_TYPE.COMMENT) log_List.setForeground(Color.darkGray);
if (type == LOG_TYPE.READ_INFO)log_List.setForeground(Color.magenta); */
((DefaultListModel<String>) log_List.getModel()).addElement(s);
//makes the last raw visible after adding a line
log_List.ensureIndexIsVisible(log_List.getLastVisibleIndex());
}
/* somewhere else in the code */
//add the path of a file chosen in a JFileChooser (fc)
add_Log_Line(LOG_TYPE.ACTION, "OPEN - " + fc.getSelectedFile().getAbsolutePath());
/* My custom cell renderer */
class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList<?> list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
Color background = Color.white;
Color foreground = Color.black;
if (value == LOG_TYPE.ACTION)
foreground = Color.blue;
if (value == LOG_TYPE.COMMENT)
foreground = Color.darkGray;
if (value == LOG_TYPE.ERROR)
foreground = Color.red;
if (value == LOG_TYPE.READ_INFO)
foreground = Color.magenta;
setForeground(foreground);
setBackground(background);
return this;
}
}
我得到的只是白底黑字...