简体   繁体   English

Java-覆盖JFrame的绘画时,JPanel将不会显示在JFrame上

[英]Java - JPanel won't show up on JFrame when overriding JFrame's paint

I have a JFrame on which I am using overriding the paint method to display some graphics. 我有一个JFrame,正在使用它覆盖paint方法来显示一些图形。 I also want to add a JPanel to it that will show up on top of the graphics. 我还想为其添加一个JPanel,它将显示在图形的顶部。 Right now, all I see is the graphics created from JFrame paint method. 现在,我所看到的只是从JFrame绘制方法创建的图形。

Here's my JPanel: 这是我的JPanel:

public class NoteDraw extends JPanel {

    public NoteDraw() {
        setSize(200, 100);
        setBackground(Color.BLACK);
    }

    @Override public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.red);
        g.fillRect(0, 0, 100, 100);
    }
}

Here's my JFrame: 这是我的JFrame:

public class ui extends JFrame {
    public void paint(Graphics g) {
        //do some drawing here...
    }
}

Here's my main: 这是我的主要内容:

public class Main {
    static ui main_gui = new ui();

    public static void main(String[] args) {
        NoteDraw note = new NoteDraw();
        main_gui.getContentPane().add(note);
        main_gui.setVisible(true);
    }
}

You should NEVER override the paint() method of a JFrame (unless you really know what you are doing). 永远不要重写JFrame的paint()方法(除非您真的知道自己在做什么)。 This method is responsible for all the optimized painting behaviour of Swing. 此方法负责Swing的所有优化绘画行为。

Custom painting should always be done by overriding the paintComponent() method of a Swing component like you have done on your NoteDraw panel. 自定义绘画应始终通过重写Swing组件的paintComponent()方法来完成,就像在NoteDraw面板上所做的那样。

The reason the code in the paint method doesn't show is because the NoteDraw panel is opaque and therefore the panel paints over top of the Graphics code in your paint method. 绘制方法中的代码未显示的原因是因为NoteDraw面板是不透明的,因此该面板在绘制方法中的图形代码上方进行了绘制。

So the solution is to move the Graphics painting code to the NoteDraw panel. 因此,解决方案是将Graphics绘画代​​码移动到NoteDraw面板。

Or if you are trying to create some kind of background image for your frame then you can try using the Background Panel . 或者,如果您尝试为框架创建某种背景图像,则可以尝试使用“ 背景面板”

Or if you truly do need custom painting then you create a background panel and override the paintComponent() method. 或者,如果您确实确实需要自定义绘画,则可以创建背景面板并覆盖paintComponent()方法。 Then you set the layout to a BorderLayout and add this panel to the frame. 然后,将布局设置为BorderLayout并将此面板添加到框架。 Then you make your NoteDraw panel non-opaque and add it to the custom background panel. 然后,使NoteDraw面板不透明,并将其添加到自定义背景面板。 Now the background will show through on the NoteDraw panel. 现在,背景将在NoteDraw面板上显示出来。

Remember to call super.paint() when you override your paint() method. 当您重写paint()方法时,请记住要调用super.paint()

This way, you still use the behavior defined in the parent class, and you can add your modifications safely. 这样,您仍然可以使用父类中定义的行为,并且可以安全地添加修改。


Resources : 资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM