2012-02-05 126 views
-3

我想要玩這個測驗,其中用戶有3個按鈕可供選擇。如果他們選擇了錯誤的答案,圖像炸彈就會出現,並立即自動改變爲下一個問題,如果他們選擇了核心問題,它也將改變爲下一個問題。但是,我的問題在於,我不知道我是否正確寫了我的if ... else語句。當我運行我的程序時,它不會更改爲下一個問題,並且沒有彈出圖像。我嘗試了很多方法來解決這個問題,但我仍然不能。我真的需要幫助。 這裏是我的代碼和if ... else語句是從線244至388Java Swing遊戲TT

public class GamesQuestionPanel extends JPanel{ 

private static final long serialVersionUID = 1L; 
private JLabel jLabelQuestion = null; 
private JLabel jLabelScore = null; 
private JLabel jLabelHeart1 = null; 
private JLabel jLabelHeart2 = null; 
private JLabel jLabelHeart3 = null; 
private JButton jButtonAnswer1; 
private JButton jButtonAnswer2; 
private JButton jButtonAnswer3; 
private JLabel jLabelDescription1; 
private JLabel jLabelDescription2; 
private JLabel jLabelDescription3; 
private JButton jButtonAdd = null; 
private JFrame myFrame = null; 
private JLabel jLabelTimer = null; 
private Timer t; 
private TimerModel tm; 
private int qn_num = 0; 
private JLabel jLabelScore1 = null; 
private JLabel jLabelQn; 
private JButton jButtonEdit = null; 
private static int answer1; 
private int answer2; 
private int answer3; 
private int count=0; 
int Score = 0; 

public void Randomise() { 
ArrayList<Integer> random = new ArrayList<Integer>(); 
try{ 
DBController db = new DBController(); 

//passing data source name 
db.setUp("CFDatabase"); 
String g_question =" "; 
String dbQuery = "SELECT * FROM GameQuestion WHERE g_question ='" + g_question + "'"; 

//for retrieve SQL use readRequest method 
ResultSet rs = db.readRequest(dbQuery); 

if (rs.next()){ 
int g_questionNo =rs.getInt("g_questionNo"); 
random.add(g_questionNo); 
System.out.println(random.get(g_questionNo)); 
} 

} 
catch (Exception e) 
{ 
e.printStackTrace(); 
} 
} 

/** 
* This is the default constructor 
*/ 
public GamesQuestionPanel() { 
super(); 
initialize(); 
} 

public GamesQuestionPanel(JFrame f) { 
this(); 
myFrame = f; 
} 

public GamesQuestionPanel(JFrame f, String s){ 
this(); 
myFrame = f; 
jLabelTimer.setText(s); 
} 

/** 
* This method initialises this 
* 
* @return void 
*/ 
private void initialize() { 
jLabelQn = new JLabel(); 
jLabelQn.setBounds(new Rectangle(13, 96, 55, 50)); 
jLabelQn.setFont(new Font("Dialog", Font.BOLD, 18)); 
jLabelQn.setHorizontalAlignment(SwingConstants.CENTER); 
jLabelQn.setHorizontalTextPosition(SwingConstants.CENTER); 
jLabelQn.setText(""); 

jLabelScore1 = new JLabel(); 
jLabelScore1.setText(""); 
jLabelScore1.setSize(new Dimension(83, 29)); 
jLabelScore1.setLocation(new Point(119, 438)); 

jLabelTimer = new JLabel(); 
jLabelTimer.setBounds(new Rectangle(17, 23, 199, 53)); 
jLabelTimer.setText("00:01:00"); 
jLabelTimer.setHorizontalTextPosition(SwingConstants.CENTER); 
jLabelTimer.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/timerbg.png"))); 
jLabelTimer.setFont(new Font("Dialog", Font.BOLD, 18)); 
jLabelTimer.setHorizontalAlignment(SwingConstants.CENTER); 
t = new Timer(1000, new java.awt.event.ActionListener(){ 
public void actionPerformed(java.awt.event.ActionEvent e) { 
tm.timeTick(); 
jLabelTimer.setText(tm.getTime()); 
if(tm.isTimeUp()){ 
t.stop(); 
JOptionPane.showMessageDialog(null, "HighScore: " + Score, "GameOver", 
JOptionPane.INFORMATION_MESSAGE); 
int finalScore = Score ; 
String display = Integer.toString(finalScore); 
jLabelScore.setText(display); 
tm.setTime("00:10:00"); 
JPanel panel = new ProgrammesEventsPanelEnq(myFrame); 
myFrame.getContentPane().removeAll(); 
myFrame.getContentPane().add(panel); 
myFrame.getContentPane().validate(); 
myFrame.getContentPane().repaint(); 
} 
} 
}); 
tm = new TimerModel(); 
tm.setTime("00:01:00"); 
t.start(); 

jLabelDescription3 = new JLabel(); 
jLabelDescription3.setFont(new Font("Dialog", Font.BOLD, 20)); 
jLabelDescription3.setLocation(new Point(483, 385)); 
jLabelDescription3.setSize(new Dimension(218, 33)); 
jLabelDescription3.setHorizontalAlignment(SwingConstants.CENTER); 
jLabelDescription3.setHorizontalTextPosition(SwingConstants.CENTER); 
jLabelDescription3.setForeground(Color.black); 

jLabelDescription2 = new JLabel(); 
jLabelDescription2.setFont(new Font("Dialog", Font.BOLD, 20)); 
jLabelDescription2.setLocation(new Point(244, 386)); 
jLabelDescription2.setSize(new Dimension(226, 33)); 
jLabelDescription2.setHorizontalAlignment(SwingConstants.CENTER); 
jLabelDescription2.setHorizontalTextPosition(SwingConstants.CENTER); 
jLabelDescription2.setForeground(Color.black); 

jLabelDescription1 = new JLabel(); 
jLabelDescription1.setFont(new Font("Dialog", Font.BOLD, 20)); 
jLabelDescription1.setBackground(new Color(238, 238, 238)); 
jLabelDescription1.setLocation(new Point(9, 386)); 
jLabelDescription1.setSize(new Dimension(225, 33)); 
jLabelDescription1.setHorizontalAlignment(SwingConstants.CENTER); 
jLabelDescription1.setHorizontalTextPosition(SwingConstants.CENTER); 
jLabelDescription1.setForeground(Color.black); 

jLabelHeart3 = new JLabel(); 
jLabelHeart3.setBounds(new Rectangle(611, 61, 45, 39)); 
jLabelHeart3.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/heartshape.png"))); 
jLabelHeart3.setText(""); 

jLabelHeart2 = new JLabel(); 
jLabelHeart2.setBounds(new Rectangle(546, 61, 43, 40)); 
jLabelHeart2.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/heartshape.png"))); 
jLabelHeart2.setText(""); 

jLabelHeart1 = new JLabel(); 
jLabelHeart1.setBounds(new Rectangle(482, 60, 44, 41)); 
jLabelHeart1.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/heartshape.png"))); 
jLabelHeart1.setText(""); 

jLabelScore = new JLabel(); 
jLabelScore.setBounds(new Rectangle(25, 438, 83, 29)); 
jLabelScore.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); 
jLabelScore.setForeground(Color.black); 
jLabelScore.setText(" Score:"); 

jLabelQuestion = new JLabel(); 
jLabelQuestion.setBounds(new Rectangle(71, 95, 618, 52)); 
jLabelQuestion.setFont(new Font("Comic Sans MS", Font.BOLD, 20)); 
jLabelQuestion.setForeground(Color.black); 

this.setSize(712, 500); 
this.setLayout(null); 
this.setBackground(Color.white); 
this.add(jLabelQuestion, null); 
this.add(jLabelScore, null); 
this.add(jLabelHeart1, null); 
this.add(jLabelHeart2, null); 
this.add(jLabelHeart3, null); 
this.add(getJButtonAnswer1(), null); 
this.add(getJButtonAnswer2(), null); 
this.add(getJButtonAnswer3(), null); 
this.add(jLabelDescription1, null); 
this.add(jLabelDescription2, null); 
this.add(jLabelDescription3, null); 
this.add(getJButtonAdd(), null); 
this.add(jLabelTimer, null); 
this.add(jLabelScore1, null); 
this.add(jLabelQn, null); 
this.add(getJButtonEdit(), null); 

qn_num = 1; 
GameQuestion g1 = new GameQuestion(qn_num); 
g1.retrieveGameQuestion(); 
jLabelQn.setText("Q1."); 
jLabelQuestion.setText(g1.getG_question()); 
jLabelDescription1.setText(g1.getG_imageDescription1()); 
jLabelDescription2.setText(g1.getG_imageDescription2()); 
jLabelDescription3.setText(g1.getG_imageDescription3()); 
jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); 
jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); 
jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); 

} 

/** 
* This method initialises jButtonAnswer1  
* 
* @return javax.swing.JButton 
*/ 
private JButton getJButtonAnswer1() { 
if (jButtonAnswer1 == null) { 
jButtonAnswer1 = new JButton(); 
jButtonAnswer1.setBackground(Color.white); 
jButtonAnswer1.setSize(new Dimension(228, 214)); 
jButtonAnswer1.setLocation(new Point(5, 164)); 
jButtonAnswer1.addActionListener(new java.awt.event.ActionListener() { 
public void actionPerformed(java.awt.event.ActionEvent e) { 
jButtonAnswer1.equals("answer1"); 
qn_num = qn_num + 1; 
GameQuestion g1 = new GameQuestion(qn_num); 
g1.retrieveGameQuestion(); 

if((jButtonAnswer1.isSelected()) && (g1.getG_correctAnswer() == answer1)) 
{ 
jLabelQn.setText("Q" + qn_num + "."); 
jLabelQuestion.setText(g1.getG_question()); 
jLabelDescription1.setText(g1.getG_imageDescription1()); 
jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); 
jLabelDescription2.setText(g1.getG_imageDescription2()); 
jLabelDescription3.setText(g1.getG_imageDescription3()); 
jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); 
jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); 
//jButtonAnswer1.removeAll(); 
} 
else if((jButtonAnswer1.isSelected()) && (g1.getG_correctAnswer() != answer1)) 
{ 
jButtonAnswer1.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/explode.png"))); 
int delay = 100; 
try 
{ 
Thread.sleep(delay); 
} 
catch (InterruptedException e1) 
{ 
e1.printStackTrace(); 
} 
jLabelQn.setText("Q" + qn_num + "."); 
jLabelQuestion.setText(g1.getG_question()); 
jLabelDescription1.setText(g1.getG_imageDescription1()); 
jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); 
jButtonAnswer1.removeAll(); 
jLabelDescription2.setText(g1.getG_imageDescription2()); 
jLabelDescription3.setText(g1.getG_imageDescription3()); 
jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); 
jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); 
} 
} 
}); 
} 
return jButtonAnswer1; 
} 

/** 
* This method initialises jButtonAnswer2  
* 
* @return javax.swing.JButton 
*/ 
private JButton getJButtonAnswer2() { 
if (jButtonAnswer2 == null) { 
jButtonAnswer2 = new JButton(); 
jButtonAnswer2.setBackground(Color.white); 
jButtonAnswer2.setSize(new Dimension(228, 214)); 
jButtonAnswer2.setLocation(new Point(240, 164)); 
jButtonAnswer2.addActionListener(new java.awt.event.ActionListener() { 
public void actionPerformed(java.awt.event.ActionEvent e) { 
jButtonAnswer2.equals("answer2"); 
qn_num = qn_num + 1; 
GameQuestion g1 = new GameQuestion(qn_num); 
g1.retrieveGameQuestion(); 
if((jButtonAnswer2.isSelected()) && (g1.getG_correctAnswer() == answer2)) 
{ 
jLabelQn.setText("Q" + qn_num + "."); 
jLabelQuestion.setText(g1.getG_question()); 
jLabelDescription1.setText(g1.getG_imageDescription1()); 
jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); 
jButtonAnswer1.removeAll(); 
jLabelDescription2.setText(g1.getG_imageDescription2()); 
jLabelDescription3.setText(g1.getG_imageDescription3()); 
jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); 
jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); 
} 
else if((jButtonAnswer2.isSelected()) && (g1.getG_correctAnswer() != answer2)) 
{ 
jButtonAnswer2.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/explode.png"))); 
int delay = 100; 
try 
{ 
Thread.sleep(delay); 
} 
catch (InterruptedException e1) 
{ 
e1.printStackTrace(); 
} 
jLabelQn.setText("Q" + qn_num + "."); 
jLabelQuestion.setText(g1.getG_question()); 
jLabelDescription1.setText(g1.getG_imageDescription1()); 
jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); 
jButtonAnswer1.removeAll(); 
jLabelDescription2.setText(g1.getG_imageDescription2()); 
jLabelDescription3.setText(g1.getG_imageDescription3()); 
jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); 
jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); 
} 
} 
}); 
} 
return jButtonAnswer2; 
} 
/** 
* This method initialises jButtonAnswer3  
* 
* @return javax.swing.JButton 
*/ 
private JButton getJButtonAnswer3() { 
if (jButtonAnswer3 == null) { 
jButtonAnswer3 = new JButton(); 
jButtonAnswer3.setBackground(Color.white); 
jButtonAnswer3.setSize(new Dimension(228, 214)); 
jButtonAnswer3.setLocation(new Point(474, 163)); 
jButtonAnswer3.addActionListener(new java.awt.event.ActionListener() { 
public void actionPerformed(java.awt.event.ActionEvent e) { 
jButtonAnswer3.equals("answer3"); 
qn_num = qn_num + 1; 
GameQuestion g1 = new GameQuestion(qn_num); 
g1.retrieveGameQuestion(); 
if((jButtonAnswer3.isSelected()) && (g1.getG_correctAnswer() == answer3)) 
{ 
jLabelQn.setText("Q" + qn_num + "."); 
jLabelQuestion.setText(g1.getG_question()); 
jLabelDescription1.setText(g1.getG_imageDescription1()); 
jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); 
jButtonAnswer1.removeAll(); 
jLabelDescription2.setText(g1.getG_imageDescription2()); 
jLabelDescription3.setText(g1.getG_imageDescription3()); 
jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); 
jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); 
} 
else if((jButtonAnswer3.isSelected()) && (g1.getG_correctAnswer() != answer3)) 
{ 
jButtonAnswer1.setIcon(new ImageIcon(getClass().getResource("/HealthOK/ui/images/explode.png"))); 
int delay = 100; 
try 
{ 
Thread.sleep(delay); 
} 
catch (InterruptedException e1) 
{ 
e1.printStackTrace(); 
} 
jLabelQn.setText("Q" + qn_num + "."); 
jLabelQuestion.setText(g1.getG_question()); 
jLabelDescription1.setText(g1.getG_imageDescription1()); 
jButtonAnswer1.setIcon(new ImageIcon(g1.getG_image1())); 
jButtonAnswer1.removeAll(); 
jLabelDescription2.setText(g1.getG_imageDescription2()); 
jLabelDescription3.setText(g1.getG_imageDescription3()); 
jButtonAnswer2.setIcon(new ImageIcon(g1.getG_image2())); 
jButtonAnswer3.setIcon(new ImageIcon(g1.getG_image3())); 
} 
} 
}); 
} 
return jButtonAnswer3; 
} 
/** 
* This method initializes jButtonEdit 
* 
* @return javax.swing.JButton 
*/ 
private JButton getJButtonAdd() { 
if (jButtonAdd == null) { 
jButtonAdd = new JButton(); 
jButtonAdd.setBounds(new Rectangle(591, 9, 90, 39)); 
jButtonAdd.setFont(new Font("Dialog", Font.BOLD, 16)); 
jButtonAdd.setText("Add"); 
jButtonAdd.addActionListener(new java.awt.event.ActionListener() { 
public void actionPerformed(java.awt.event.ActionEvent e) { 
t.stop(); 
JPanel panel = new GamesCreatePanel(myFrame); 
myFrame.getContentPane().removeAll(); 
myFrame.getContentPane().add(panel); 
myFrame.getContentPane().validate(); 
myFrame.getContentPane().repaint(); 

Object admin = null; 
if(getName() == admin) { 
jButtonAdd.setVisible(true); 
} 
else { 
jButtonAdd.setVisible(false); 
} 
} 
}); 
} 
return jButtonAdd; 
} 

/** 
* This method initializes jButtonEdit 
* 
* @return javax.swing.JButton 
*/ 
private JButton getJButtonEdit() { 
if (jButtonEdit == null) { 
jButtonEdit = new JButton(); 
jButtonEdit.setPreferredSize(new Dimension(65, 31)); 
jButtonEdit.setSize(new Dimension(90, 39)); 
jButtonEdit.setText("Edit"); 
jButtonEdit.setFont(new Font("Dialog", Font.BOLD, 16)); 
jButtonEdit.setLocation(new Point(492, 9)); 
jButtonEdit.addActionListener(new java.awt.event.ActionListener() { 
public void actionPerformed(java.awt.event.ActionEvent e) { 
t.stop(); 
String a = jLabelQuestion.getText(); 
String b = jButtonAnswer1.getText(); 
String c = jButtonAnswer2.getText(); 
String d = jButtonAnswer3.getText(); 
String f = jLabelDescription1.getText(); 
String g = jLabelDescription2.getText(); 
String h = jLabelDescription3.getText(); 
GameQuestion game = new GameQuestion(); 
game.setG_question(a); 
game.setG_image1(b); 
game.setG_image2(c); 
game.setG_image3(d); 
game.setG_imageDescription1(f); 
game.setG_imageDescription2(g); 
game.setG_imageDescription3(h); 
JPanel panel = new GamesEditPanel(myFrame, a, game); 
myFrame.getContentPane().removeAll(); 
myFrame.getContentPane().add(panel); 
myFrame.getContentPane().validate(); 
myFrame.getContentPane().repaint(); 
} 
}); 
} 
return jButtonEdit; 
} 

} 
+2

我不是向下選民,但有太多的代碼,翻閱。你需要更多地縮小問題的範圍。另外,如果這是作業,它應該被標記爲這樣。 – Bill 2012-02-05 05:47:06

+2

另請參閱:正確縮進代碼。這是不可讀的。並且不要在事件派發線程中睡覺。閱讀http://docs.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading and http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index html的 – 2012-02-05 08:04:08

回答

1

此代碼是太長的方式去通過它,但看了一下您的if報表顯示你正在檢查是否選擇了JButton。您可能需要的是將ActionListener添加到JButton,當用戶單擊JButton時將會調用這個ActionListener。在ActionListener中,您可以檢查是否給出了正確答案並繼續,或顯示「炸彈圖像」。

我建議你去看看Swing button tutorial,更具體的JButton part of the tutorial