如何更新Java swing背景图?

发布于 2025-01-08 06:19:04 字数 1307 浏览 0 评论 0原文

在此处输入图像描述

正如您所看到的这些图片,背景网格线和黑色矩形在菜单项关闭后不会更新。我怎样才能更新它?在 C# 中,有一个事件处理程序可以使其自动更新,但我是 Java swing GUI 应用程序的新手。

这是代码:

public void paint(Graphics g) {

  super.paintComponents(g); 

  MainDisplayForm mD = new MainDisplayForm();           


  Graphics2D g2 = (Graphics2D) g;  

  g2.setColor(Color.BLACK);
  int gridWidth = 1240;
  int gridHeight = 400;      
  g2.fillRect(20, 50, gridWidth, gridHeight);


  g2.setColor(Color.darkGray);

  paintGrid(g2,gridWidth, gridHeight);

  g2.setColor(Color.red);
  Line2D line = new Line2D.Float(20, 50, 250, 260);               
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  //g2.draw(line); //pending       

  g2.dispose();

}

private void paintGrid(Graphics g, int gridWidth, int gridHeight)
{

  for(int i=20; i<gridWidth+20; i=i+10)
  {      
      g.drawLine(i, 50, i, gridHeight+49);          
  }      

  for(int i=50; i<gridHeight+50; i=i+10)
  {      
      g.drawLine(20, i, 1259, i);          
  }      
}

//感谢您的评论!这是要添加的事件处理程序。 必须将其添加到网格上绘制的每个 memu 项目

private void jMenu2MenuDeselected(javax.swing.event.MenuEvent evt) {
    repaint();
}

enter image description here

enter image description here

As you see these picture, the background grid lines and black rectangle are not updated after the menu item is closed. How can I update it? In C# there is an event handler to make it updated automatically, but I am newbie to Java swing GUI application.

Here is the code:

public void paint(Graphics g) {

  super.paintComponents(g); 

  MainDisplayForm mD = new MainDisplayForm();           


  Graphics2D g2 = (Graphics2D) g;  

  g2.setColor(Color.BLACK);
  int gridWidth = 1240;
  int gridHeight = 400;      
  g2.fillRect(20, 50, gridWidth, gridHeight);


  g2.setColor(Color.darkGray);

  paintGrid(g2,gridWidth, gridHeight);

  g2.setColor(Color.red);
  Line2D line = new Line2D.Float(20, 50, 250, 260);               
  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  //g2.draw(line); //pending       

  g2.dispose();

}

private void paintGrid(Graphics g, int gridWidth, int gridHeight)
{

  for(int i=20; i<gridWidth+20; i=i+10)
  {      
      g.drawLine(i, 50, i, gridHeight+49);          
  }      

  for(int i=50; i<gridHeight+50; i=i+10)
  {      
      g.drawLine(20, i, 1259, i);          
  }      
}

//Thanks for comments!! Here is the event handler to add.
This must be added to every memu item drawn over the grid

private void jMenu2MenuDeselected(javax.swing.event.MenuEvent evt) {
    repaint();
}

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

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

发布评论

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

评论(3

就像说晚安 2025-01-15 06:19:04

不要重写paint()并调用super.paintComponents()。

自定义绘制是通过重写 JPanel(或 JComponent)的 PaintComponent() 方法来完成的,然后您将调用 super.paintComponent()。

如果您需要更多帮助,请发布您的 SSCCE 来演示问题。

Don't override paint() and invoke super.paintComponents().

Custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent) and then you would invoke super.paintComponent().

If you need more help then post your SSCCE demonstrating the problem.

吝吻 2025-01-15 06:19:04

使用repaint()刷新/更新组件。

何时使用repaint()

将某些组件添加到面板/框架或操作某些组件后,例如,当您更改 Swing 中组件的位置/属性时动画,调用 repaint() 它会为你完成这项工作。实际上,它触发了对调用组件的 update() 方法的调用。

Use repaint() for refreshing/updating the components.

When to use repaint()?

After you add some components to a panel/frame or manipulate some components, for example when you change the locations/properties of components in a Swing animation, call repaint() and it will do the job for you. Actually, it triggers a call for update() method of the calling component.

╰ゝ天使的微笑 2025-01-15 06:19:04

当菜单关闭时,调用重新打印到窗口。

不要调用paint(getGraphics())。相反,调用 repaint() 因为这会通知超级组件它也需要重绘。

When the menu is closed call reprint to the window.

Don't call paint(getGraphics()). Instead call repaint() because this informs the super component that it needs to redraw as well.

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