在 Swing 中绘画

发布于 2024-12-28 19:27:45 字数 2074 浏览 2 评论 0原文

我有一个扩展 JPanel 的类(Timeline)。时间轴面板包含许多手动定位的 JLabel(绿色和橙色元素)(“空布局”)。时间轴顶部有一些用于在月份之间切换的按钮。有时,当我在几个月之间切换时,swing 不会绘制 JLabels,但总是绘制网格背景。

我已经尝试过许多“神奇”方法(重绘、重新验证、无效、验证、updateUI)。

成功绘制时间轴:

Timeline successesful

绘制失败:

时间线失败

一个简短的示例:

public interface IDateSelectorRegistrar {

  void addListener(DateSelectorListener listener);

  void removeListener(DateSelectorListener listener);
}

public interface DateSelectorListener {
  void dateChanged(Timestamp from, Timestamp to);
}

public interface ITimelineModel {
  Timespan[] getTimespans(Timestamp from, Timestamp to);
}

public class Timespan {
  private String title;
  private Timestamp to;
  private Timestamp from;

  public Timespan(String title, Timestamp from, Timestamp to) {
    this.title = title;
    this.from = from; 
    this.to = to;
  }

  // setters and getters
}

public class TimelineLabel extends JLabel {
  public TimelineLabel(Timespan timespan) {
    super(timespan.getTitle());
  }

  @Override
  protected void paintComponent(Graphics g) {
    // paint colored background
    super.paintComponent(g);
  }
}

public class Timeline extends JPanel {

   public Timeline(final ITimelineModel model, IDateSelectorRegistrar registrar) {
     registrar.addListener(new DateSelectorListener() {
       public void dateChanged(Timestamp from, Timestamp to) {
         Timeline.this.removeAll();
         Timespan[] timespans = model.getTimespans(from, to);
         for(Timespan timespan : timespans) {
           TimelineLabel label = new TimelineLabel(timespan);
           Timeline.this.add(label);
           // label positioning because of Timestamp object data
         }
         // repaint of timeline
         Timeline.this.invalidate();
         Timeline.this.repaint();
       }
     });
   }

   @Override
   protected void paintComponent(Graphics g) {
      // paint background grid
      super.paintComponent(g);
   }
}

I have a class (Timeline) which extends JPanel. The Timeline Panel contains many JLabels (green and orange elements) which are positioned manually ("null-Layout"). On top of the Timeline are some buttons for switching between months. Sometimes when i switch between months swing won't paint the JLabels but always paints the grid background.

I've already tried many of the "magic" methods(repaint, revalidate, invalidate, validate, updateUI).

Successfully painted Timeline:

Timeline succesful

Failed painting:

Timeline failed

A short example:

public interface IDateSelectorRegistrar {

  void addListener(DateSelectorListener listener);

  void removeListener(DateSelectorListener listener);
}

public interface DateSelectorListener {
  void dateChanged(Timestamp from, Timestamp to);
}

public interface ITimelineModel {
  Timespan[] getTimespans(Timestamp from, Timestamp to);
}

public class Timespan {
  private String title;
  private Timestamp to;
  private Timestamp from;

  public Timespan(String title, Timestamp from, Timestamp to) {
    this.title = title;
    this.from = from; 
    this.to = to;
  }

  // setters and getters
}

public class TimelineLabel extends JLabel {
  public TimelineLabel(Timespan timespan) {
    super(timespan.getTitle());
  }

  @Override
  protected void paintComponent(Graphics g) {
    // paint colored background
    super.paintComponent(g);
  }
}

public class Timeline extends JPanel {

   public Timeline(final ITimelineModel model, IDateSelectorRegistrar registrar) {
     registrar.addListener(new DateSelectorListener() {
       public void dateChanged(Timestamp from, Timestamp to) {
         Timeline.this.removeAll();
         Timespan[] timespans = model.getTimespans(from, to);
         for(Timespan timespan : timespans) {
           TimelineLabel label = new TimelineLabel(timespan);
           Timeline.this.add(label);
           // label positioning because of Timestamp object data
         }
         // repaint of timeline
         Timeline.this.invalidate();
         Timeline.this.repaint();
       }
     });
   }

   @Override
   protected void paintComponent(Graphics g) {
      // paint background grid
      super.paintComponent(g);
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的心境我的脸 2025-01-04 19:27:45

作为替代方案,请考虑 org.jfree.chart.renderer.category.GanttRenderer,它非常适合时域图表并允许各种自定义。可以在此处找到如下所示的示例。

甘特图子任务

As an alternative, consider org.jfree.chart.renderer.category.GanttRenderer, which is ideal for time domain charts and admits a variety of customization. An example, illustrated below, may be found here.

Gantt Subtasks

一桥轻雨一伞开 2025-01-04 19:27:45

按以下顺序调用以下方法解决了该问题:

invalidate();
repaint();
validate();

Calling following methods in the following order fixed the problem:

invalidate();
repaint();
validate();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文