Witam,
mam problem z ustawienie zmiany obrazu dla przycisku, po kliknięciu innego przycisku. Jeśli klikam przycisk, obrazek się zmienia na obu przyciskach, z tą wada że na przycisku który chce zmienić, zmienia się dopiero po najechaniu na niego myszą.
package Game_1;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import DisplayWindow.MyFrame;
@SuppressWarnings("serial")
public class bottonPanel extends JPanel {
public static final int HEIGHT = LeftPanel.HEIGHT / 4 - 10;
public static final int WIDTH = LeftPanel.WIDTH - 10;
public static boolean sound;
public static String closeImageIcon = "res/game1/CloseButton.png";
public static ImageIcon closeIcon;
private JButton Close;
private JButton Sound;
public bottonPanel() {
Close = new closeButton();
Sound = new soundButton();
setLayout(new GridLayout(1, 2));
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setVisible(true);
add(Close);
add(Sound);
}
static class closeButton extends JButton implements ActionListener{
static Image image;
static ImageObserver imageObserver;
closeButton() {
super();
closeIcon = new ImageIcon(closeImageIcon);
image = closeIcon.getImage();
imageObserver = closeIcon.getImageObserver();
addActionListener(this);
}
public void paint( Graphics g ) {
super.paint( g );
g.drawImage(image, 0 , 0 , MyFrame.WIDTH / 4, MyFrame.HEIGHT / 4, imageObserver);
}
@Override
public void actionPerformed(ActionEvent e) {
MyFrame.cardLayout.show(MyFrame.cardPanel, "0");
}
}
public static class soundButton extends JButton implements ActionListener{
public boolean sound = true;
Image image;
ImageObserver imageObserver;
public soundButton() {
super();
ImageIcon icon = new ImageIcon("res/game1/SoundOn.png");
image = icon.getImage();
imageObserver = icon.getImageObserver();
addActionListener(this);
}
public void paint( Graphics g ) {
super.paint( g );
g.drawImage(closeButton.image, 0 , 0 , MyFrame.WIDTH / 4, MyFrame.HEIGHT / 4, imageObserver);
}
@Override
public void actionPerformed(ActionEvent e) {
if (sound == true)
{
sound = false;
//ImageIcon icon = new ImageIcon("res/game1/SoundOff.png");
closeImageIcon = "res/game1/SoundOff.png";
closeButton.image = closeIcon.getImage();
closeButton.imageObserver = closeIcon.getImageObserver();
new closeButton();
} else {
sound = true;
//ImageIcon icon = new ImageIcon("res/game1/SoundOn.png");
closeImageIcon = "res/game1/SoundOn.png";
closeButton.image = closeIcon.getImage();
closeButton.imageObserver = closeIcon.getImageObserver();
new closeButton();
}
}
}
}