import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Proyecto_Frame extends JFrame {
private ImageIcon Aston;
private ImageIcon Ferrari;
private ImageIcon Lambo;
private ImageIcon McLaren;
/**
*
*/
private static final long serialVersionUID = 1L;
public Proyecto_Frame() {
super ("Carrera");
this.setLayout(null);
Aston = new ImageIcon (getClass().getResource("Aston1.png"));
Ferrari = new ImageIcon (getClass().getResource("Ferrari1.png"));
Lambo = new ImageIcon (getClass().getResource("Lambo1.png"));
McLaren = new ImageIcon (getClass().getResource("McLaren1.png"));
final JLabel Aston1 = new JLabel(Aston);
final JLabel Ferrari1 = new JLabel(Ferrari);
final JLabel Lambo1 = new JLabel(Lambo);
final JLabel McLaren1 = new JLabel(McLaren);
final JLabel Aston2 = new JLabel(Aston);
final JLabel Ferrari2 = new JLabel(Ferrari);
final JLabel Lambo2 = new JLabel(Lambo);
final JLabel McLaren2 = new JLabel(McLaren);
JButton B_Aston = new JButton (Aston);
JButton B_Ferrari = new JButton (Ferrari);
JButton B_Lambo = new JButton (Lambo);
JButton B_McLaren = new JButton (McLaren);
B_Aston.setBounds(new java.awt.Rectangle(30,30,230,75));
this.getContentPane().add(B_Aston);
B_Ferrari.setBounds(new java.awt.Rectangle(270,30,230,75));
this.getContentPane().add(B_Ferrari);
B_Lambo.setBounds(new java.awt.Rectangle(30,110,230,75));
this.getContentPane().add(B_Lambo);
B_McLaren.setBounds(new java.awt.Rectangle(270,110,230,75));
this.getContentPane().add(B_McLaren);
add(B_Aston);
add(B_Ferrari);
add(B_Lambo);
add(B_McLaren);
Aston1.setBounds(30, 250, 230, 75);
Aston1.setVisible(false);
Ferrari1.setBounds(30, 250, 230, 75);
Ferrari1.setVisible(false);
Lambo1.setBounds(30, 250, 230, 75);
Lambo1.setVisible(false);
McLaren1.setBounds(30, 250, 230, 75);
McLaren1.setVisible(false);
Aston2.setBounds(30, 350, 230, 75);
Aston2.setVisible(false);
Ferrari2.setBounds(30, 350, 230, 75);
Ferrari2.setVisible(false);
Lambo2.setBounds(30, 350, 230, 75);
Lambo2.setVisible(false);
McLaren2.setBounds(30, 350, 230, 75);
McLaren2.setVisible(false);
add(Aston1);
add(Ferrari1);
add(Lambo1);
add(McLaren1);
add(Aston2);
add(Ferrari2);
add(Lambo2);
add(McLaren2);
B_Aston.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Aston1.setVisible(true);
Ferrari1.setVisible(false);
Lambo1.setVisible(false);
McLaren1.setVisible(false);
}
});
B_Ferrari.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Aston1.setVisible(false);
Ferrari1.setVisible(true);
Lambo1.setVisible(false);
McLaren1.setVisible(false);
}
});
B_Lambo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Aston1.setVisible(false);
Ferrari1.setVisible(false);
Lambo1.setVisible(true);
McLaren1.setVisible(false);
}
});
B_McLaren.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
Aston1.setVisible(false);
Ferrari1.setVisible(false);
Lambo1.setVisible(false);
McLaren1.setVisible(true);
}
});
}
}
嘿,我是新來的Java和得到此代碼。所以,這裏的問題是,我想對這些圖像中的兩個進行選擇,並在按鈕下方繪製它們以模擬比賽。當我點擊任何Jbutton時,該圖像是可見的,其他3個不是,但我無法弄清楚如何離開這個選擇並用這些相同的JButton做出下一個選擇。誰能幫我?我讀過許多主題和許多其他頁面,但似乎沒有找到答案。如何爲任何JButton添加兩個不同的事件
謝謝,抱歉的格式,不適用於論壇格式。
問題是,我剛剛在Java上工作了大多數1周,並且不瞭解Java語言上的很多事情,所以我試圖讓它儘可能簡單,儘管它意味着要有一個非常你可能已經看到我已經;儘管我會進一步考慮實施這種方法,謝謝! – VaguitoV 2012-04-22 16:29:05