我需要通过覆盖JPanel的paintComponent()方法在JPanel上绘制图形。在JPanel上绘图并将JPanel添加到JFrame中
当我通过JFrame拖放一个JPanel来设计使用NetBeans的GUI时,它通过创建一个私有变量JPanel对象来生成代码。在这种情况下,我怎么能覆盖它的方法来绘制它...
否则,如果我通过扩展JPanel编写类的代码并重写绘制它的方法,我必须创建一个新的JFrame和添加JPanel到它..
JFrame fr = new JFrame(); fr.add(pane); //窗格是扩展JPanel的类的对象,其中我绘制了 fr.setVisible(true);
在这种情况下,它的工作原理..
但是,如果我得到它通过NetBeans的扩展JFrame中自动创建类的参考和使用,使用引用的add方法添加的JPanel了它doesn”将不起作用......
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 ...
原谅我,如果这个问题是愚蠢的..即时通讯初学者..
在此先感谢...