在jframe中为jpanel调用paintcomponent [英] call paintcomponent for a jpanel in jframe
本文介绍了在jframe中为jpanel调用paintcomponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有JPanel
的JFrame
(JPanel
在JFrame
中是私有的).现在,我想使用paintComponent
方法覆盖JPanel
.
I have a JFrame
with a JPanel
on it (JPanel
is private in JFrame
). Now I want to override JPanel
by using paintComponent
method.
我该怎么做?
推荐答案
创建JPanel
的实例时,(假设您这样做),请执行以下操作:
When you create your instance of JPanel
, (assuming you're doing it this way), do this:
JPanel panel = new JPanel(){
@Override
public void paintComponent(Graphics g){
// paint code
}
};
另一种替代方法是创建扩展 JPanel
的private class
.
例如:
The other alternative is to create a private class
which extends JPanel
.
For example:
public class OuterClass{
// fields, constructors, methods etc..
private class MyPanel extends JPanel{
// fields, constructors, methods etc..
@Override
public void paintComponent(Graphics g){
// paint code
}
}
}
这篇关于在jframe中为jpanel调用paintcomponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文