在 JPanel 上放置绘图
我的课程有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
repaint
方法(随后是paintComponent
方法) 不仅由JFrame
也可以通过 Swing 本身,当需要重新绘制JPanel
< 的内容/a>.在 AWT 和 Swing 中绘画一文是一个很好的起点获取有关绘画如何进行的信息。
在本例中,
repaint
方法由本文称为系统触发绘画的事件调用:The
repaint
method (and subsequently, thepaintComponent
method) is not only called by theJFrame
but also by Swing itself as well, when there needs to be a repaint of the contents of theJPanel
.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: