2012-03-01 22 views
0

OK、ここに必要なのはここです。 trueを返し、文字列にテキストを追加し、それ以外の場合は同じ文字列に別のテキストを追加するif文があります。私のコードHERESに:if文がtrueを返す場合には文字列にテキストを書き込み、そうでない場合は別のテキストを返します。

import java.awt.*; 
import java.util.*; 
import javax.swing.*; 
import java.awt.event.*; 

     public class theclock extends JFrame { 
     theclock() { 
     final JLabel timeField = new JLabel(); 
     timeField.setFont(new Font("sansserif", Font.PLAIN, 20)); 

     Container content = this.getContentPane(); 
     content.setLayout(new FlowLayout()); 
     content.add(timeField); 

     setTitle("Norway"); 
     setSize(150,70); 

     javax.swing.Timer t = new javax.swing.Timer(1000,new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      Calendar cal = new GregorianCalendar(); 
      String a = ""; //empty string that I want to add text to 
      String h = String.valueOf(cal.get(Calendar.HOUR)+8); //I know its not the easiest way to add 8 hours, but im experimenting. 
       int i = Integer.parseInt(h); 
        if (i>12) 
        { 
          i=i-12; 
          a = "A.M"; //what to add to the string if it is true 
        } 
        else 
        { 
         i=i; //haven't applied myself to this part yet, so i know its probably wrong, but its just a place holder 
         a = "P.M"; //for when it is else, i should say pm. 
        } 
      String m = String.valueOf(cal.get(Calendar.MINUTE)); 
      String s = String.valueOf(cal.get(Calendar.SECOND)); 
      timeField.setText("" + i + ":" + m + ":" + s + " " + a); //where the string is shown. 
      } 
     }); 
     t.start(); 
    } 
    public static void main(String[] args) { 
     JFrame clock = new theclock(); 
     clock.setVisible(true); 
    } 
} 
+0

それは働いていませんか?それは私には妥当と思われる。 – corsiKa

+0

あなたの質問は何ですか? – dasblinkenlight

+0

また、SimpleDateFormatの使用を検討しましたか?これは、使用するのがかなり簡単な、日付フォーマッタです。 – corsiKa

答えて

0

あなたが使用できる文字列に追加する必要がある場合:

if (i>12) 
{ 
    i=i-12; 
    a = a + "A.M"; //appends 'A.M' to the string if the string already has a value 
} 
else 
{ 
    i=i; 
    a = a + "P.M"; //appends 'P.M' to the string if the string already has a value 
} 
+0

うん、あまりにも、ありがとう! –

関連する問題