我需要通过覆盖 JPanel 的 paintComponent() 方法在 JPanel 上绘制图形。
当我在 JFrame 上拖放 JPanel 时使用 netbeans 设计 gui 时,它通过创建私有变量 JPanel 对象来生成代码。在这种情况下,我如何覆盖它的方法来绘制它......
或者,如果我通过扩展 JPanel 为类编写代码并覆盖绘制它的方法,我必须创建一个新的 JFrame 并将 JPanel 添加到它。
JFrame fr=new JFrame(); fr.add(窗格); //pane 是扩展 JPanel 的类的对象,我在其中绘制 fr.setVisible(true);
在这种情况下它有效..
但是,如果我得到自动创建的类的引用,该类通过 netbeans 扩展 JFrame 并使用它使用引用的 add 方法添加 JPanel,那么它就不起作用了......
class x extends JPanel
{
paintComponent(Graphics g){ //overridden method
//my code for drawing say lines goes here..
}
}
class y extends Thread
{
z obj;
y(z obj){
this.obj=obj;
}
public void run(){
x pane=new x();
pane.setVisible(true);
obj.add(pane);
obj.setVisible(true); //im not getting the pane visible here.. if i created a new JFrame class here as i said earlier and added the pane to it i can see it..
}
}
class z extends JFrame
{
z(){//code generated by netbeans}
public static void main(String args[])
{
new y(new z()).start();
}
}
它没有显示错误,但是当我运行程序时,只有 Jframe 可见.. JPanel 没有显示...
如果问题很愚蠢,请原谅我..我是初学者..
提前致谢...