JPanel 不显示绘图

发布于 2024-08-07 09:21:50 字数 1291 浏览 7 评论 0原文

我在 JPanel 上显示绘图时遇到问题。我创建了三个相互链接的类,如下所示。我想知道为什么这段代码不显示我的绘图。

c.add(pDraw);
pDraw.add(draw);

1) 主

public class mainPage {
    public static void main(String[]args){
      JFrame appFrame = new Frame();
      appFrame.setVisible(true); 
      appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

2) JFrame

 public class Frame extends JFrame implements ActionListener{

    private drawingBoard draw;  

    public Frame (){
         draw = new drawingBoard(); //generate pattern
         GridBagLayout m = new GridBagLayout();
         Container c = (Container)getContentPane();
         c.setLayout (m);
         GridBagConstraints con;
         .......

         JPanel pDraw = new JPanel();       
         pDraw.setPreferredSize(new Dimension(500,500));
             .....  
         c.add(pDraw);
         pDraw.add(draw); // Call other class for drawing

         .....
         setResizable(false); 
         pack();
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
         setVisible(true);
    }
}

3) JPANEL

public class drawingBoard extends JPanel {
      .....
      public void paint(Graphics g) {
      ......
      }
   }

I have problem displaying drawing on JPanel. I created three class which linked to each other as the following. I was wondering why this code, doesn't display my drawing.

c.add(pDraw);
pDraw.add(draw);

1) MAIN

public class mainPage {
    public static void main(String[]args){
      JFrame appFrame = new Frame();
      appFrame.setVisible(true); 
      appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

2) JFRAME

 public class Frame extends JFrame implements ActionListener{

    private drawingBoard draw;  

    public Frame (){
         draw = new drawingBoard(); //generate pattern
         GridBagLayout m = new GridBagLayout();
         Container c = (Container)getContentPane();
         c.setLayout (m);
         GridBagConstraints con;
         .......

         JPanel pDraw = new JPanel();       
         pDraw.setPreferredSize(new Dimension(500,500));
             .....  
         c.add(pDraw);
         pDraw.add(draw); // Call other class for drawing

         .....
         setResizable(false); 
         pack();
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
         setVisible(true);
    }
}

3) JPANEL

public class drawingBoard extends JPanel {
      .....
      public void paint(Graphics g) {
      ......
      }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

怼怹恏 2024-08-14 09:21:50

这就是答案,我忘记设置绘图板 JPanel 的大小:-)

 public class drawingBoard extends JPanel {
        public drawingBoard(){
        setPreferredSize(new Dimension (500,500));

        }
    }

Here is the answer, I forgot to set the size of the drawingBoard JPanel :-)

 public class drawingBoard extends JPanel {
        public drawingBoard(){
        setPreferredSize(new Dimension (500,500));

        }
    }
老街孤人 2024-08-14 09:21:50

它对您的代码进行了一行更改。

您所要做的就是阅读有关“自定义绘画”的 Swing 教程找出你做错了什么。

这是连续的第三个问题,如果您愿意阅读本教程,则可以在几分钟内解决。

而且您还没有学会如何发布 SSCCE,所以我不会即将用勺子把答案给你。

Its a one line change to your code.

All you have to do is read the Swing tutorial on "Custom Painting" to figure out what you are doing wrong.

This is the third question in a row that could have been solved in minutes if you bothered to read the tutorial.

And you still haven't learned how to post a SSCCE, so I'm not about to spoon feed the answer to you.

孤君无依 2024-08-14 09:21:50

我同意 Ben Torell 在他的帖子中提出的所有观点 - 以及这里的一些额外的故障排除建议。

试试这个 -

public class DrawingBoardTest extends JFrame {
    public DrawingBoardTest() {
        getContentPane().add(new drawingBoard(), BorderLayout.CENTER);
    }
    public static void main(String[] args ) {
        JFrame f = new DrawingBoardTest();
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

如果 DrawingPanel 显示出来,那么这是您的 GridBagLayout 的问题,或者在 DrawingPanel 上设置的首选尺寸不够大,无法显示绘图,或者将 DrawingBoard 添加到 pDraw (据我所知,从代码来看,并不是真的需要......)。

JFrame 内容窗格的默认布局是 BorderLayout,它将为中心组件提供所有空间,这就是我在发布的代码中放置绘图板的位置。

JPanel 的默认布局是 FlowLayout,它只会为组件提供首选大小。我看到您在原始代码中设置了 pDraw 上的首选尺寸,但没有在绘图板上设置 - 绘图板可能设置的首选尺寸太小而无法显示绘图。

如果 DrawingPanel 没有显示 - 那么这是您的 DrawingPanel Paint() 方法中的问题。

I agree with all the points Ben Torell made in his post - plus some extra troubleshooting advice here.

Try this -

public class DrawingBoardTest extends JFrame {
    public DrawingBoardTest() {
        getContentPane().add(new drawingBoard(), BorderLayout.CENTER);
    }
    public static void main(String[] args ) {
        JFrame f = new DrawingBoardTest();
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

If the drawingPanel shows up, then it's a problem with your GridBagLayout, or having a preferred size set on drawingPanel that isn't large enough to display the drawing, or adding drawingBoard to pDraw (which, as far as I can see from the code, isn't really needed...).

The default layout for a JFrame's content pane is BorderLayout, which will give all space to the center component, which is where I'm placing drawingBoard in the code I posted.

The default layout of a JPanel is FlowLayout, which will only give the component it's preferred size. I see you set the preferred size on pDraw, but not on drawingBoard, in the original code - drawingBoard may have a preferred size set that is too small to display the drawing.

If the drawingPanel doesn't show up - then it's a problem in your drawingPanel paint() method.

最美的太阳 2024-08-14 09:21:50

虽然在没有看到 GridBagConstraints 或 Paint() 方法的情况下调试代码有点困难,但我会向您提供,通常认为重写 PaintComponent() 方法而不是 Paint() 方法更好。在drawBoard中,尝试这个而不是重写paint():

public void paintComponent(Graphics g) {
    super.paintComponent(g); //optional
    ...
}

这可能会有所不同。有关详细信息,请查看这篇 Java 文章

另外,作为免费赠品,您可能不需要在 main() 方法和 Frame 的构造函数中执行 setVisible() 和 setDefaultCloseOperation() 。

While it's a little tough to debug the code without seeing the GridBagConstraints or the paint() method, I will offer to you that it's generally considered better to override the paintComponent() method rather than the paint() method. In drawingBoard, try this instead of overriding paint():

public void paintComponent(Graphics g) {
    super.paintComponent(g); //optional
    ...
}

That may make a difference. For more information, check out this article from Java.

Also, as a freebie, you probably don't need to perform setVisible() and setDefaultCloseOperation() in both the main() method and Frame's constructor.

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