在 JPanel 上放置绘图

发布于 2024-08-07 07:04:34 字数 1434 浏览 2 评论 0原文

我的课程有 3 节课。 1)主,2)框架,3)绘图板。我的程序的逻辑是,每次用户单击新图案按钮时都会显示一个新的绘图(并且这工作正常)。

第一类 - 主方法

public class mainPage {
   public static void main(String[]args){
     JFrame appFrame = new Frame();
     appFrame.setVisible(true); 
     appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
   }
}

第二类 - 描述布局(我使用网格袋布局)

public class Frame extends JFrame implements ActionListener {
  public Frame (){
   GridBagLayout m = new GridBagLayout();
   Container c = (Container)getContentPane();
   c.setLayout (m);
   GridBagConstraints con;

   JButton bPattern = new JButton("New Pattern");
   ....
   bPattern.addActionListener(this);

   JPanel pDraw = new JPanel();        
   .....
   pDraw.add(new drawingBoard()); //drawing will be placed in this panel
 }

 public void actionPerformed(ActionEvent e) {
   repaint();        
 }

}

第三类 - 运行绘图函数,例如 PaintComponent () 等。

public class drawingBoard extends JPanel {
  public drawingBoard(){}
  public void paintComponent(Graphic g){}
  ....
  }

问题是,当我在控制台上查看时,似乎即使用户没有单击按钮,程序调用类“drawingBoard”并重新绘制。绘画组件属于第三类(drawingBoard)。虽然这似乎没有给我带来问题(例如,除非用户单击按钮,否则面板上不会显示任何图形),但我只是好奇这是如何发生的。是因为我在FRAME类()中写了这段代码。我编写此代码的目的是确保绘图应放置在这个特定面板中(我有 3 个面板),但除非单击按钮,否则不要调用第三类。

JPanel pDraw = new JPanel();        
pDraw.add(new drawingBoard()); //place drawing here

My program have 3 classes. 1) main, 2) frame, 3) drawingBoard. The logic of my program is that, a new drawing will be displayed every times user click on New pattern button (and this working fine).

1st class - main method

public class mainPage {
   public static void main(String[]args){
     JFrame appFrame = new Frame();
     appFrame.setVisible(true); 
     appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);*/
   }
}

2nd class - describe the layout (I use Grid Bag Layout)

public class Frame extends JFrame implements ActionListener {
  public Frame (){
   GridBagLayout m = new GridBagLayout();
   Container c = (Container)getContentPane();
   c.setLayout (m);
   GridBagConstraints con;

   JButton bPattern = new JButton("New Pattern");
   ....
   bPattern.addActionListener(this);

   JPanel pDraw = new JPanel();        
   .....
   pDraw.add(new drawingBoard()); //drawing will be placed in this panel
 }

 public void actionPerformed(ActionEvent e) {
   repaint();        
 }

}

3rd class - run drawing functions e.g. paintComponent (), etc.

public class drawingBoard extends JPanel {
  public drawingBoard(){}
  public void paintComponent(Graphic g){}
  ....
  }

The problem is that, when I look on the console, it seems that even though the user did not click on the button, the program call the class 'drawingBoard' and repaint. The paint component is in the 3rd class (drawingBoard). Although this seem not to give me a problem (e.g. no drawing displayed on the panel unless the user click the button), I am just curious how this happened. is that because I wrote this code at FRAME class (). My intention to write this code is to make sure the drawing should be place in this specific panel (I have 3 panels) but not to call the 3rd class unless the button has been clicked.

JPanel pDraw = new JPanel();        
pDraw.add(new drawingBoard()); //place drawing here

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

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

发布评论

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

评论(1

耳钉梦 2024-08-14 07:04:34

repaint 方法(随后是 paintComponent 方法) 不仅由 JFrame 也可以通过 Swing 本身,当需要重新绘制JPanel< 的内容/a>.

在 AWT 和 Swing 中绘画一文是一个很好的起点获取有关绘画如何进行的信息。

在本例中,repaint 方法由本文称为系统触发绘画的事件调用:

在系统触发的绘画中
操作时,系统请求
组件呈现其内容,
通常用于以下其中一项
原因:

  • 该组件首先在屏幕上可见。
  • 组件已调整大小。
  • 组件有损坏,需要修复。 (例如,
    以前掩盖的东西
    组件已移动,并且先前的
    组件的模糊部分有
    暴露)。

The repaint method (and subsequently, the paintComponent method) is not only called by the JFrame but also by Swing itself as well, when there needs to be a repaint of the contents of the JPanel.

The Painting in AWT and Swing article is a good place to start to get information on how painting works.

In this case, the repaint method is being called by events which the article calls System-triggered Painting:

In a system-triggered painting
operation, the system requests a
component to render its contents,
usually for one of the following
reasons:

  • The component is first made visible on the screen.
  • The component is resized.
  • The component has damage that needs to be repaired. (For example,
    something that previously obscured the
    component has moved, and a previously
    obscured portion of the component has
    become exposed).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文