Java 更多paintComponent问题,JPanel组件上的背景仍在绘制
我已经问了本质上相同的问题,并且被告知我的背景画在我的JButtons上的原因是因为我从未调用super.paintComponent(g),但后来我学会了。除了现在,我已经设法打破了我的新代码,以及我的旧代码,工作了很短的一段时间。似乎只要我删除g.dispose(),我的代码就可以工作; 有人知道为什么钮扣的油漆不好吗 您声明: 似乎只要我删除g.dispose(),我的代码就可以工作 那就把它去掉Java 更多paintComponent问题,JPanel组件上的背景仍在绘制,java,swing,jpanel,jbutton,paintcomponent,Java,Swing,Jpanel,Jbutton,Paintcomponent,我已经问了本质上相同的问题,并且被告知我的背景画在我的JButtons上的原因是因为我从未调用super.paintComponent(g),但后来我学会了。除了现在,我已经设法打破了我的新代码,以及我的旧代码,工作了很短的一段时间。似乎只要我删除g.dispose(),我的代码就可以工作; 有人知道为什么钮扣的油漆不好吗 您声明: 似乎只要我删除g.dispose(),我的代码就可以工作 那就把它去掉 决不应决不处置JVM提供给您的图形对象。您应该做的第一件事是从paintComponent(
决不应决不处置JVM提供给您的图形对象。您应该做的第一件事是从
paintComponent(…)
方法中调用g.dispose()
。如果复制图形对象并使用该副本进行绘制,或者如果使用从BuffereImage获取的图形对象进行绘制,则选择“是”,处置它们以节省资源,但是,如果您处理了JVM的图形对象,那么当JVM试图继续使用该图形对象绘制其他组件时,可能会出现下游绘制问题。wow从不知道这一点。糟糕的在线教程让我觉得,为了节省资源而处理图形是一种惯例。谢谢解释了很多。@user3215804:我想你可能误解了教程告诉你的内容。我严重怀疑合法教程会建议您处理传递到paintComponent中的图形对象。我最近才开始理解Paint和paintComponent之间的区别,所以在此之前,人们似乎在教程中互换使用它们,这就是我感到困惑的原因。@user3215804:请显示指向的链接教程。我仍然认为您误读了它,因为paint(Graphics g)
的问题是一样的。我研究图形是很久以前的事了,所以我不太确定我看了哪些图形,对不起。
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();
}
}