我有兩個按鈕,每個按鈕上都有一個圖像,我希望通過點擊另一個(第三個)按鈕來交換前兩個按鈕的圖像。我已經嘗試過,但我收到了一些錯誤。 Plz的幫助。我的代碼是:單擊另一個按鈕在2個按鈕上交換圖像
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SwapImage implements ActionListener
{
JButton b1;
JButton b2;
JButton b3;
ImageIcon bgi1;
ImageIcon bgi2;
ImageIcon bgi3=new ImageIcon();
SwapImage()
{
JFrame f=new JFrame("Swap Image");
bgi1=new ImageIcon(getClass().getResource("a.png"));
bgi2=new ImageIcon(getClass().getResource("b.png"));
b1=new JButton(bgi1);
b1.setBounds(80,80,100,100);
//b1.addActionListener(this);
b2=new JButton(bgi2);
b2.setBounds(200,80,100,100);
//b2.addActionListener(this);
b3=new JButton("SWAP");
b3.setBounds(170,230,100,100);
b3.addActionListener(this);
f.add(b1);
f.add(b2);
f.add(b3);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
bgi3=bgi1;
bgi1=bgi2;
bgi2=bgi3;
b1=new JButton(bgi1);
b2=new JButton(bgi2);
}
public static void main(String s[])
{
new SwapImage();
}
}
什麼是某種錯誤的? – Compass 2014-09-18 19:36:58