2012-03-02 73 views
1

我怎麼可以將更多容器添加到我的Jframe?我的繼承人的代碼行,我想作一個時鐘在包含在同一JFrame的側面以外的時鐘中的一個窗口,我的繼承人代碼:將更多容器添加到jframe

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.Calendar; 
public class CopyOftheclock { 
public static void main(String[] args) { 
    JFrame clock = new TextClockWindow(); 
    clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    clock.setVisible(true); 
    } 
} 


@SuppressWarnings("serial") 
class TextClockWindow extends JFrame { 
private JTextField timeField; 
    public TextClockWindow() { 
    timeField = new JTextField(7); 
    timeField.setFont(new Font("sansserif", Font.PLAIN, 48)); 

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

    this.setTitle("Norway"); 
    this.pack(); 
    javax.swing.Timer t = new javax.swing.Timer(1000, 
      new ActionListener() { 
       public void actionPerformed(ActionEvent e) { 
        String a = ""; 
        Calendar now = Calendar.getInstance(); 
        int h = now.get(Calendar.HOUR_OF_DAY); 
        if (h==24) 
        { 
         h=8; 
         a = "A.M"; 
        } 
        if (h==1) 
        { 
         h=9; 
         a = "A.M"; 
        } 
        if (h==2) 
        { 
         h=10; 
         a = "A.M"; 
        } 
        if (h==3) 
        { 
         h=11; 
         a = "A.M"; 
        } 
        if (h==4) 
        { 
         h=12; 
         a = "P.M"; 
        } 
        if (h==5) 
        { 
         h=1; 
         a = "P.M"; 
        } 
        if (h==6) 
        { 
         h=2; 
         a = "P.M"; 
        } 
        if (h==7) 
        { 
         h=3; 
         a = "P.M"; 
        } 
        if (h==8) 
        { 
         h=4; 
         a = "P.M"; 
        } 
        if (h==9) 
        { 
         h=5; 
         a = "P.M"; 
        } 
        if (h==10) 
        { 
         h=6; 
         a = "P.M"; 
        } 
        if (h==11) 
        { 
         h=7; 
         a = "P.M"; 
        } 
        if (h==12) 
        { 
         h=8; 
         a = "P.M"; 
        } 
        if (h==13) 
        { 
         h=9; 
         a = "P.M"; 
        } 
        if (h==14) 
        { 
         h=10; 
         a = "P.M"; 
        } 
        if (h==15) 
        { 
         h=11; 
         a = "P.M"; 
        } 
        if (h==16) 
        { 
         h=12; 
         a = "P.M"; 
        } 
        if (h==17) 
        { 
         h=1; 
         a = "A.M"; 
        } 
        if (h==18) 
        { 
         h=2; 
         a = "A.M"; 
        } 
        if (h==19) 
        { 
         h=3; 
         a = "A.M"; 
        } 
        if (h==20) 
        { 
         h=4; 
         a = "A.M"; 
        } 
        if (h==21) 
        { 
         h=5; 
         a = "A.M"; 
        } 
        if (h==22) 
        { 
         h=6; 
         a = "A.M"; 
        } 
        if (h==23) 
        { 
         h=7; 
         a = "A.M"; 
        } 
        int m = now.get(Calendar.MINUTE); 
        int s = now.get(Calendar.SECOND); 
        timeField.setText("" + h + ":" + m + ":" + s + " " + a); 
       } 

      }); 
    t.start(); 
    } 
} 

如果你們能幫助我得到這個工作,我會真的很感激它!

+0

繼承人一張圖片,即時通訊設法試圖做什麼http://s1173.photobucket.com/albums/r597/jackiechanwins/?action=view¤t=clock.jpg – 2012-03-02 17:44:22

+0

通過使用'add()'方法? – 2012-03-02 17:45:52

+0

我在哪裏可以把它放在代碼中?每個容器將會有不同的數字 – 2012-03-02 17:46:35

回答

5

1)對於顯示不可編輯的文本,使用JLabel而不是JTextField

2)對於較少的代碼,請使用SimpleDateFormat的方法。

3)使用合適的LayoutManager;在你的情況(也許)GridLayout會讓所有JComponent在屏幕上具有相同的Dimension

4)main public static void main(String[] args) {中所有與GUI相關的代碼都應該包含在invokeLater();更多信息請登錄Initial Threads

5)也許對於其餘的this thread could be useful