[英]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();
}
}
我已經問過基本上相同的問題,並被告知我的背景在我的JButton上繪畫的原因是因為我從未調用過super.paintComponent(g),但是我從中學到了。 除了現在,我設法破壞了我的新代碼以及舊代碼,這些代碼在很短的時間內都可以正常工作。 看來,我刪除g.dispose()之后,我的代碼就會正常工作。 有人知道為什么按鈕似乎無法正確繪制嗎?
您聲明:
看來,我刪除g.dispose()之后,我的代碼就會正常工作。
因此,將其刪除。
永遠不要丟棄JVM給您的Graphics對象。 您應該做的第一件事是從paintComponent(...)
方法中獲取g.dispose()
調用。 如果復制Graphics對象並使用該副本進行繪制,或者使用從BufferedImage獲得的Graphics對象進行繪制,則可以,將它們處置以節省資源,但是如果處置JVM的Graphics對象,則可能會出現繪制問題當JVM嘗試繼續使用該Graphics對象繪制其他組件時,將返回流。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.