denk
denk
JCHJava Community | Help. Code. Learn.
Created by denk on 10/9/2024 in #java-help
ButtenImage not working well
why when i use this code, my whole ui is covered with the button? What is weird, is that the image has the size 100 x 100 but the button just covers the whole ui
try {

BufferedImage img = ImageIO.read(new File("src/goldbarren.png"));

Image scaledImg = img.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(scaledImg);


JButton klickButton = new JButton(icon);
klickButton.setBounds(100, 100, 100, 100);


klickButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
punkte += punkteProKlick;
punkteLabel.setText("Punkte: " + punkte);
}
});
try {

BufferedImage img = ImageIO.read(new File("src/goldbarren.png"));

Image scaledImg = img.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(scaledImg);


JButton klickButton = new JButton(icon);
klickButton.setBounds(100, 100, 100, 100);


klickButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
punkte += punkteProKlick;
punkteLabel.setText("Punkte: " + punkte);
}
});
4 replies