简体   繁体   English

更多paintComponent问题,JPanel组件仍在绘画背景

[英]More paintComponent issues, background still painting over JPanel components

public class GamePanel extends JPanel {
    private int windowHeight = Toolkit.getDefaultToolkit().getScreenSize().height-37;
    private int windowWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    private int height = windowHeight / 6;
    private int width = windowWidth;
    private BufferedImage bg;
    public GamePanel() {
    setBounds(0, windowHeight / 5 * 4, windowWidth, windowHeight);
    try {bg = ImageIO.read(new File("GUI Images/wood background.png"));}
    catch (Exception e) {Utilities.showErrorMessage(this, e);}
    setVisible(true);


    //add buttons
    JButton InventButton = new JButton("Inventory");
    InventButton.setOpaque(false);
    InventButton.setBorderPainted(false);
    InventButton.setContentAreaFilled(false);
    InventButton.setVerticalTextPosition(SwingConstants.CENTER);
    InventButton.setHorizontalTextPosition(SwingConstants.CENTER);
    InventButton.setFont(new Font("TimesRoman", Font.PLAIN, 20));
    InventButton.setBounds(width/6*5,0,width/6,height/3);
    //get button texture
    Image i1 = new ImageIcon("GUI Images/Button.png").getImage().getScaledInstance
        (InventButton.getWidth(),InventButton.getHeight(),java.awt.Image.SCALE_SMOOTH);
    InventButton.setIcon(new ImageIcon(i1));
    InventButton.setForeground(Color.white);
    InventButton.addActionListener(e -> {


        });


    JButton PartyButton = new JButton("Party");
    PartyButton.setOpaque(false);
    PartyButton.setBorderPainted(false);
    PartyButton.setContentAreaFilled(false);
    PartyButton.setVerticalTextPosition(SwingConstants.CENTER);
    PartyButton.setHorizontalTextPosition(SwingConstants.CENTER);
    PartyButton.setFont(new Font("TimesRoman", Font.PLAIN, 20));
    PartyButton.setBounds(width/6*5,height/3,width/6,height/3);
    PartyButton.setIcon(new ImageIcon(i1));
    PartyButton.setForeground(Color.white);
    PartyButton.addActionListener(e -> {

        });
    JButton MenuButton = new JButton("Menu");
    MenuButton.setOpaque(false);
    MenuButton.setBorderPainted(false);
    MenuButton.setContentAreaFilled(false);
    MenuButton.setVerticalTextPosition(SwingConstants.CENTER);
    MenuButton.setHorizontalTextPosition(SwingConstants.CENTER);
    MenuButton.setFont(new Font("TimesRoman", Font.PLAIN, 20));
    MenuButton.setBounds(width/6*5,height/3*2,width/6,height/3);
    MenuButton.setIcon(new ImageIcon(i1));
    MenuButton.setForeground(Color.white);
    MenuButton.addActionListener(e -> {

        });
    add(MenuButton);
    add(InventButton);
    add(PartyButton);
    revalidate();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bg,0,0,width,height, null);
        g.dispose();
    }
}

I've asked essentially the same question question already and was told that the reason that my background was painting over my JButtons is because I never called super.paintComponent(g), but I've since learned. 我已经问过基本上相同的问题,并被告知我的背景在我的JButton上绘画的原因是因为我从未调用过super.paintComponent(g),但是我从中学到了。 Except now I've managed to break my new code as well as my old code which worked for a brief period of time. 除了现在,我设法破坏了我的新代码以及旧代码,这些代码在很短的时间内都可以正常工作。 It seems that my code works as soon as I remove g.dispose(); 看来,我删除g.dispose()之后,我的代码就会正常工作。 Does anybody know why the buttons don't seem to paint properly? 有人知道为什么按钮似乎无法正确绘制吗?

You state: 您声明:

It seems that my code works as soon as I remove g.dispose(); 看来,我删除g.dispose()之后,我的代码就会正常工作。

So then remove it. 因此,将其删除。

You should never never dispose of a Graphics object given to you by the JVM. 永远不要丢弃JVM给您的Graphics对象。 The first thing you should do is get the g.dispose() call out of your paintComponent(...) method. 您应该做的第一件事是从paintComponent(...)方法中获取g.dispose()调用。 If you copy the Graphics object and paint with the copy, or if you paint with a Graphics object obtained from a BufferedImage, then yes, dispose of them to save on resources, but if you dispose of the JVM's Graphics object you risk painting problems down stream when the JVM tries to continue using that Graphics object to paint other components. 如果复制Graphics对象并使用该副本进行绘制,或者使用从BufferedImage获得的Graphics对象进行绘制,则可以,将它们处置以节省资源,但是如果处置JVM的Graphics对象,则可能会出现绘制问题当JVM尝试继续使用该Graphics对象绘制其他组件时,将返回流。

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

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