0
ボタンをクリックすると画像をどのように変更できますか? -文ならば、私は違う作ることができるので、私はx
変数を作ったが、それは私が何か間違ったことをしたせいか、私のために動作しませんでした...ここに私のコードは、これまでです:JButtonで画像を変更する方法
public class Main extends JFrame{
private JButton changePic;
private JPanel panel;
private JLabel pic1;
private JLabel pic2;
private JLabel pic3;
private JLabel picture;
private int x = 0;
public Main(){
panel = new JPanel();
add(panel);
changePic = new JButton("Change Button");
panel.add(changePic);
pic1 = new JLabel(new ImageIcon("pic1.png"));
pic2 = new JLabel(new ImageIcon("pic.png"));
pic3 = new JLabel(new ImageIcon("pic3.png"));
panel.add(pic1);
panel.add(pic2);
panel.add(pic3);
changePic.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource() == changePic){
}
}
});
getContentPane().setBackground(Color.white);
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){
Main fr = new Main();
}
}
(画像を変更し、 にこのケースを)
JFrame
のpack()
あなたはUIの変更があったたびに呼び出す必要があり、 『それが私のために動作しませんでした』あなたは明確でした 『動作しませんでした』 ?あなたのコードが何をすることを期待しているのか、何を考えているのでしょうか、代わりに何が起こりますか? – Pshemoボタンを押すと画像がスワップされませんでした。私が最初に(PIC1)で参照画像のみ\t changePic.addActionListener(新しいのActionListener(){ \t \t \tます。public void actionPerformedの(のActionEvent e)の{ \t \t \t \t場合(e.getSource()== changePic){ \t \t \t \t 0; \t \t \t \t \t(x == 0){ \t \t \t \t \t \t panel.add(PIC1)場合} else if(x == 1){ \t \t \t \t \t \t panel.add(pic2);他 \t \t \t \t \t} IF(X == 2){ \t \t \t \t \t \t panel.add(PIC3)。 \t \t \t \t \t} \t \t \t \t \t X ++。 \t \t \t \t \t場合(X> 2) \t \t \t \t \t \t X = 0。 \t \t \t \t} \t \t \t} \t \t})。 –
[edit]オプションを使用すると、問題の説明とコードで質問を更新できます(コメントセクションでは判読できないため)。 – Pshemo