绘制 JPanel 并将 JPanel 添加到 JFrame
我需要通过重写 JPanel 的 PaintComponent() 方法在 JPanel 上绘制图形。
在使用 netbeans 设计 gui 时,当我将 JPanel 拖放到 JFrame 上时,它会通过创建私有变量 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 未显示...
如果问题很愚蠢,请原谅我.. 我是初学者..
提前致谢...
I need to draw a graph over a JPanel by overriding the JPanel's paintComponent() method.
While designing gui using netbeans when i drag/drop a JPanel over JFrame it generates code by creating a private variable, JPanel object. In such a case how can i override its method to draw over it...
or else if i write code for a class by extending the JPanel and override the method to paint it, I have to create a new JFrame and add the JPanel to it..
JFrame fr=new JFrame();
fr.add(pane); //pane is the object of class that extends JPanel where i draw
fr.setVisible(true);
In this case it works..
But if i get a reference of the auto-created class which extends JFrame by netbeans and use that to add the JPanel using the add method of the reference got it doesn't work...
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();
}
}
It shows no error but when i run the program only the Jframe is visible.. JPanel is not shown...
Pardon me if the question is silly.. im a beginner..
Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码的行为是不可预测的,因为您违反了 Swing 开发的主要规则:所有 UI 工作都应在事件调度线程 (EDT) 上完成。您的代码应类似于:
有关该主题的更多信息如下:
http://java.sun.com/products/jfc/ tsc/articles/threads/threads1.html
Behavior of your code is unpredictable because you are violating main rule of Swing development: all UI work should be done on Event Dispatch Thread (EDT). Your code should look something like:
More about the subject is here:
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
你的代码非常混乱。 无论如何,而不是
你需要
Your code is pretty much obfuscated. Anyway, instead of
you need
听起来您是 Swing 的初学者。 然而,使用库 JXLayer 使得在组件上绘制变得非常简单和直观
查看他们的演示和示例代码。
除此之外,优秀的 JFreeChart 是一个很棒的免费 Java 图形(和可视化)库
It sounds like you are a beginner using Swing. However, using the library JXLayer makes painting over components extremely easy and intuitive
Check out their demos and sample code.
Otherwise, the excellent JFreeChart is a great, free Java graphing (and visualization) library