绘制 JPanel 并将 JPanel 添加到 JFrame

发布于 2024-08-02 04:10:53 字数 1382 浏览 3 评论 0原文

我需要通过重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑凤梨 2024-08-09 04:10:53

您的代码的行为是不可预测的,因为您违反了 Swing 开发的主要规则:所有 UI 工作都应在事件调度线程 (EDT) 上完成。您的代码应类似于:

public static void main(String args[]) { 
    SwingUtilities.invokeLater( new Runnable() {
         void run() 
         {
             JFrame z = new JFrame();
             z.add(new X()); // works only in java 6
            //z.getContentPane().add(new X()); // works in any version of java
             z.pack(); // assuming your pane has preferred size 
             z.setVisible(true); 

         }
    }); 
}

有关该主题的更多信息如下:
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:

public static void main(String args[]) { 
    SwingUtilities.invokeLater( new Runnable() {
         void run() 
         {
             JFrame z = new JFrame();
             z.add(new X()); // works only in java 6
            //z.getContentPane().add(new X()); // works in any version of java
             z.pack(); // assuming your pane has preferred size 
             z.setVisible(true); 

         }
    }); 
}

More about the subject is here:
http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

蘑菇王子 2024-08-09 04:10:53

你的代码非常混乱。 无论如何,而不是

obj.add(pane); 

你需要

obj.getContentPane().add(pane); 

Your code is pretty much obfuscated. Anyway, instead of

obj.add(pane); 

you need

obj.getContentPane().add(pane); 
江湖正好 2024-08-09 04:10:53

听起来您是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文