2017-08-05 6 views
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(); 
    } 
} 
+1

(画像を変更し、 にこのケースを)JFramepack()あなたはUIの変更があったたびに呼び出す必要があり、 『それが私のために動作しませんでした』あなたは明確でした 『動作しませんでした』 ?あなたのコードが何をすることを期待しているのか、何を考えているのでしょうか、代わりに何が起こりますか? – Pshemo

+0

ボタンを押すと画像がスワップされませんでした。私が最初に(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})。 –

+2

[edit]オプションを使用すると、問題の説明とコードで質問を更新できます(コメントセクションでは判読できないため)。 – Pshemo

答えて

0
public class Main extends JFrame{ 

    private JButton changePic; 
    private JPanel panel; 

    private JLabel pic1; 
    private int x = 0; 

    public Main(){ 

     panel = new JPanel(); 
     add(panel); 

     changePic = new JButton("Change Button"); 
     panel.add(changePic); 


     pic1 = new JLabel(); 
     panel.add(pic1); 
     ImageIcon icon1 = new ImageIcon("pic1.gif"); 
     ImageIcon icon2 = new ImageIcon("pic2.gif"); 
     ImageIcon icon3 = new ImageIcon("pic3.gif"); 

     changePic.addActionListener(new ActionListener(){ 

      public void actionPerformed(ActionEvent e){ 
       if(e.getSource() == changePic){ 
        if(x == 0){ 
         pic1.setIcon(icon1); 
        }else if(x == 1){ 
         pic1.setIcon(icon2); 
        }else if(x == 2){ 
         pic1.setIcon(icon3); 
        } 
        Main.this.pack(); 
        x++; 
        if(x > 2) x = 0; 

       } 
      } 
     }); 
     getContentPane().setBackground(Color.white); 
     setSize(300, 300); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 

    } 

    public static void main(String[] args){ 
     Main fr = new Main(); 
    } 
} 
  • 複数のJLabelが必要でない場合は、代わりに3 ImageIconを使用してください。
  • はあなたが
関連する問題