Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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 JList如何重新喷漆?_Java_Swing_Jlist - Fatal编程技术网

Java JList如何重新喷漆?

Java JList如何重新喷漆?,java,swing,jlist,Java,Swing,Jlist,请尝试运行下面的示例,并向我解释为什么重新绘制有时会失败,特别是当您单击某一行时。看起来JList忘记了行的首选大小。 我的意思是,当以固定间隔运行此代码时,JList会在某个随机间隔内变为空白 import javax.swing.JList; import javax.swing.ListCellRenderer; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JFrame; im

请尝试运行下面的示例,并向我解释为什么重新绘制有时会失败,特别是当您单击某一行时。看起来JList忘记了行的首选大小。 我的意思是,当以固定间隔运行此代码时,JList会在某个随机间隔内变为空白

 import javax.swing.JList;
 import javax.swing.ListCellRenderer;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JFrame;
 import javax.swing.BorderFactory;
 import javax.swing.DefaultListModel;
 import java.awt.Color;
 import java.awt.Component;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.util.Timer;
 import java.util.TimerTask;
class ListItem
{
 private Color color;
 private String value;
 public ListItem(Color c, String s)
 {
  color = c;
  value = s;
 }
 public Color getColor()
 {
  return color;
 }
 public String getValue()
 {
  return value;
 }
}

class MyCellRenderer extends 
JLabel implements ListCellRenderer
{
 public MyCellRenderer ()
 {
  setOpaque(true);
 }
 public Component getListCellRendererComponent(JList list,
   Object value, // value to display
         int index,    // cell index
         boolean iss,  // is selected
         boolean chf)  // cell has focus?
 {
  System.out.println(" i cell-rendering "+CustomList.tid + " " 
    + CustomList.model.getSize() + " " + index+" "+CustomList.statusList.getValueIsAdjusting() + " ");
  setText(((ListItem)value).getValue());
  setBackground(((ListItem)value).getColor());
  if (iss)
  {
   setBorder(BorderFactory.createLineBorder(Color.blue, 1));
  }
  else
  {
   setBorder(BorderFactory.createLineBorder(list.getBackground(), 1));
  }
  return this;
 }
}

public class CustomList
{
 public static DefaultListModel model;
 public static JList statusList;
 public static int tid = 0;

 public static void tidUr()
 {
  int delay = 1000;
  int period = 1000; 
  Timer timer = new Timer(); 
  timer.scheduleAtFixedRate(new TimerTask()
  { 
   public void run()
   { 
    // model = new DefaultListModel();
    // statusList.setModel(model);
    // statusList.setCellRenderer(new MyCellRenderer());
    model.clear();
    tid++;
    ListItem li = new ListItem(Color.cyan, "test line one "+tid);
    model.addElement(li);
    li = new ListItem(Color.yellow, "foo foo foo foo foo");
    model.addElement(li);
    li = new ListItem(Color.green, "quick brown fox");
    model.addElement(li);
   }
  }, delay, period); 
 }

 public static void main(String args[])
 {
  JFrame frame = new JFrame("Custom List Demo");
  frame.addWindowListener( new WindowAdapter()
  {
   public void windowClosing( WindowEvent e)
   {
    System.exit(0);
   }
  }
   );

  model = new DefaultListModel();
  statusList = new JList(model);
  statusList.setCellRenderer(new MyCellRenderer());

  ListItem li = new ListItem(Color.cyan, "test line one");
  model.addElement(li);
  li = new ListItem(Color.yellow, "foo foo foo foo foo");
  model.addElement(li);
  li = new ListItem(Color.green, "quick brown fox");
  model.addElement(li);

  JPanel panel = new JPanel();
  panel.add(statusList);
  frame.getContentPane().add("Center", panel);
  frame.setLocation(300, 400);
  frame.setSize(200, 200);
  //frame.pack();
  frame.setVisible(true);

  // tidUr();
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
          System.out.println("Hello World Timer");
            model.clear();
            tid++;
            ListItem li = new ListItem(Color.cyan, "test line one "+tid);
            model.addElement(li);
            li = new ListItem(Color.yellow, "foo foo foo foo foo");
            model.addElement(li);
            li = new ListItem(Color.green, "quick brown fox");
            model.addElement(li);
        }
      };

    javax.swing.Timer t2 = new javax.swing.Timer(1000, actionListener);
    t2.start();

 }
}

不确定您试图描述的重绘问题到底是什么

但是,快速查看一下代码,明显的问题是您应该使用Swing计时器来进行更新。对Swing组件的所有更改都应该在事件分派线程上完成。由于对模型的更改将通知Swing组件进行自我更新,因此应在EDT上进行模型更新


有关更多信息,请阅读Swing教程中的部分。

不确定您试图描述的重新绘制问题到底是什么

但是,快速查看一下代码,明显的问题是您应该使用Swing计时器来进行更新。对Swing组件的所有更改都应该在事件分派线程上完成。由于对模型的更改将通知Swing组件进行自我更新,因此应在EDT上进行模型更新


有关更多信息,请阅读Swing教程中的部分。

不要使用
java.util.Timer
您应该使用
javax.Swing.Timer
,它设计用于处理Swing组件,这很可能是您的问题所在


Swing组件只能在事件调度线程上修改,并且您描述的症状(“不时”)是典型的,当EDT以外的另一个线程更新GUI时,它似乎工作正常,但突然出现了一些“奇怪”的情况发生。

不要使用
java.util.Timer
您应该使用
javax.swing.Timer
,它设计用于处理swing组件,这很可能是您的问题


Swing组件只能在事件调度线程上修改,并且您描述的症状(“不时”)是典型的,当EDT以外的另一个线程更新GUI时,它似乎工作正常,但突然发生了一些“奇怪”的事情。

谢谢大家!!!这解决了问题,我了解到java.util.Timer不适合与SwingActionListener actionListener=new actionListener(){public void actionPerformed(ActionEvent ActionEvent){System.out.println(“Hello World Timer”);model.clear();tid++;ListItem li=new ListItem组合使用(Color.cyan,“testline one”+tid);model.addElement(li);li=new-ListItem(Color.yellow,“foo-foo-foo-foo”);model.addElement(li);li=new-ListItem(Color.green,“quick-brown-fox”);model.addElement(li);}};javax.swing.Timer t2=new-javax.swing.Timer(1000,actionListener);t2.start();谢谢大家!!!这解决了问题,我了解到java.util.Timer不适合与SwingActionListener actionListener=new actionListener()结合使用{public void actionPerformed(ActionEvent ActionEvent){System.out.println(“Hello World Timer”);model.clear();tid++;ListItem li=new ListItem(Color.cyan,“测试行一”+tid);model.addElement(li);li=new ListItem(Color.yellow,“foo foo foo foo foo”);model.addElement(li);li=new ListItem(Color.green,“quick brown fox”);model.addElement(li);};javax.swing.Timer t2=new javax.swing.Timer(1000,actionListener);t2.start();JTable同时适用于java.util.Timer和javax.swing.Timer。JList仅适用于javax。swing:TimerIt可能看起来适用于java.util.Timer,但您违反了swing单线程策略,因此您永远无法确定。JTable同时适用于java.util.Timer和javax.swing.Timer。JList仅适用于javax。swing:TimerIt它似乎可以与java.util.Timer配合使用,但您违反了Swing单线程策略,因此永远无法确定。