2016-11-30 47 views
-1

我正在Java中進行測驗。現在我正在努力做出結果的最終屏幕。我想在第一列有一張表格,第二列是正確答案,最後一列是給定的答案。我無法添加圖片和給定的答案。下面有代碼的一部分,我讓表:在新的JLabel中添加圖像和JLabel

 ImageIcon image1 = new ImageIcon("images/Zwitserland.png"); 

     FinalTekst = new JLabel ("<html>" 
            + "<p style='text-align:center;'>Je bent klaar met de quiz. Je hebt in totaal <strong>"+CorrAntw+"</strong> van de 10 vragen goed.</p>" 
            + "<table border='0'>" 
             + "<tr><th> </th><th>Correct antwoord</th><th>Gegeven antwoord</th></tr>" 
             + "<tr><td>"+image1+"</td><td>Zwitserland</td><td>"+Antw1+"</td></tr>" 
            + "</table>" 
           + "</html>"); 
     FinalTekst.setFont(new Font("Arial", Font.PLAIN, 15)); 
     FinalTekst.setHorizontalAlignment(JLabel.CENTER); 
     add(FinalTekst, BorderLayout.CENTER); 

此搜索顯示爲文本「圖像/ Zwitserland.png」。以下是Antw1的代碼,顯示爲非常長的文本。

 if (event.getSource() == bv1a) { 
      Antw1 = new JLabel ("Zwitserland"); 
     } 
     else if (event.getSource() == bv1b) { 
      Antw1 = new JLabel ("Bahrein"); 
     } 
     else if (event.getSource() == bv1c) { 
      Antw1 = new JLabel ("Oostenrijk"); 
     } 
     else if (event.getSource() == bv1d) { 
      Antw1 = new JLabel ("Turkije"); 
     } 
     else { 
      Antw1 = new JLabel ("Geen antwoord"); 
     } 

有人可以幫我添加給定的答案和圖像嗎?我真的很感激。

回答

0

而不是使用HTML來設置您的標籤的圖像,你可以只使用

myLabel.setImageIcon(new ImageIcon("path_to_your_icon"));

在這種情況下,你可以使用兩個標籤以上一個問題,第二個爲回答。

此外,您還需要更新您的用戶界面,以便將其與標籤中添加的html相匹配