2012-07-08 50 views
0

我在合併的時間和鍵有問題...的JPanel時鐘和按鈕合併

我不能在JPanel按鈕show。

這是我的代碼:

import java.awt.Color; 
import java.awt.Font; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public final class Date_Time extends JFrame{ 
    private static final long serialVersionUID = 1L; 
    private JPanel show_date_time = new JPanel(); 
    private JLabel time = new JLabel("Time:"); 
    private JLabel show_time = new JLabel("Show Time"); 

    DateFormat dateFormat2 = new SimpleDateFormat("h:mm:ss a"); 
    java.util.Date date2; 
    private JLabel label; 
    private JPanel panel; 

    public Date_Time(){ 
     this.setSize(300, 300); 
     this.setResizable(false); 
     getContentPane().add(Show_Time_date()); 
    } 
    private JButton button1 = new JButton(); 
    private JFrame frame1 = new JFrame(); 
    public JPanel Show_Time_date(){ 
     frame1.add(show_date_time); 
     show_date_time.setBackground(Color.ORANGE); 
     frame1.add(button1); 
     getShow_date_time().setLayout(null); 
     Font f; 
     f = new Font("SansSerif", Font.PLAIN, 15); 
     getTime().setBounds(0,250,400,30); 
     getTime().setFont(f); 
     getShow_date_time().add(getTime()); 
     setShow_time(new JLabel("")); 
     updateDateTime(); 
     getShow_time().setBounds(37,250,400,30); 
     getShow_time().setFont(f); 
     getShow_date_time().add(getShow_time()); 
     return getShow_date_time(); 

    } 

    public static void main(String[] args) { 
     Date_Time Main_win=new Date_Time(); 
     Main_win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Main_win.setVisible(true); 
    } 

    public void updateDateTime() 
    { 
    Thread th = new Thread(new Runnable() 
     { 
      @Override 
     public void run() 
      { 
      while(true) 
       { 
       date2 = new java.util.Date(); 
       String dateTime = dateFormat2.format(date2); 
        getShow_time().setText(dateTime); 
        getShow_time().updateUI(); 

       } 
      } 
     }); 
    th.start(); 
    } 

    /** 
    * @return the show_time 
    */ 
    public JLabel getShow_time() { 

     return show_time; 
    } 

    /** 
    * @param show_time the show_time to set 
    */ 
    public void setShow_time(JLabel show_time) { 
     this.show_time = show_time; 
    } 

    /** 
    * @return the time 
    */ 
    public JLabel getTime() { 
     return time; 
    } 

    /** 
    * @param time the time to set 
    */ 
    public void setTime(JLabel time) { 
     this.time = time; 
    } 

    /** 
    * @return the show_date_time 
    */ 
    public JPanel getShow_date_time() { 
     return show_date_time; 
    } 

    /** 
    * @param show_date_time the show_date_time to set 
    */ 
    public void setShow_date_time(JPanel show_date_time) { 
     this.show_date_time = show_date_time; 
    } 

    /** 
    * @return the label1 
    */ 


    /** 
    * @param label1 the label1 to set 
    */ 


    /** 
    * @return the label 
    */ 
    public JLabel getLabel() { 

     return label; 
    } 

    /** 
    * @param label the label to set 
    */ 
    public void setLabel(JLabel label) { 
     this.label = label; 
    } 

    /** 
    * @return the panel 
    */ 
    public JPanel getPanel() { 
     return panel; 
    } 

    /** 
    * @param panel the panel to set 
    */ 
    public void setPanel(JPanel panel) { 
     this.panel = panel; 
    } 
} 
+0

[鏈接] http://txtup.co/ThUkx [鏈接] – JFetz2191 2012-07-08 06:02:39

+0

DATE_TIME已經延伸'JFrame',不需要另外的幀 - 'frame1'。將該按鈕添加到「show_date_time」面板而不是框架。不要使用空白布局。 – tenorsax 2012-07-08 06:15:04

+0

我已經使用show_date_time面板來顯示按鈕,但它不顯示 – JFetz2191 2012-07-08 06:17:31

回答

3

這裏看看這個修改後的代碼:

import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Font; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public final class Date_Time extends JFrame{ 

    private static final long serialVersionUID = 1L; 
    private JPanel show_date_time = new JPanel(); 
    private JLabel time = new JLabel("Time:"); 
    private JLabel show_time = new JLabel("Show Time"); 

    private DateFormat dateFormat2 = 
       new SimpleDateFormat("h:mm:ss a"); 
    private JButton button1 = new JButton("USELESS");   
    private java.util.Date date2; 
    private JLabel label; 
    private JPanel panel; 

    public Date_Time(){   

     //this.setResizable(false); 
     getContentPane().add(show_Time_date()); 
    }  

    public JPanel show_Time_date(){ 

     getShow_date_time().setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
     /* 
     * Always set this Opaque property of the 
     * said JComponent to true, before 
     * you change the Background Colour 
     * of the said component in question. 
     * In some Look And Feels it's set to 
     * false by default, so you won't 
     * see any colour, in such situations. 
     */ 
     getShow_date_time().setOpaque(true); 
     getShow_date_time().setBackground(Color.ORANGE);   

     Font f; 
     f = new Font("SansSerif", Font.PLAIN, 15); 

     getTime().setFont(f); 
     getShow_date_time().add(getTime()); 
     setShow_time(new JLabel("")); 

     updateDateTime(); 

     getShow_time().setFont(f); 
     getShow_date_time().add(getShow_time()); 
     getShow_date_time().add(button1); 

     return getShow_date_time(); 
    } 

    public static void main(String[] args) { 

     /* 
     * Calls like pack()/setSize()/setVisible() 
     * must be done from inside the Event Dispatch Thread 
     * or the EDT in short. And not from your 
     * Main Thread. Please read Concurrency in Swing 
     * in detail from this link : 
     * http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html 
     */ 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       Date_Time main_win=new Date_Time(); 
       main_win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       main_win.setSize(250, 75); 
       main_win.setVisible(true); 
      } 
     });   
    } 

    public void updateDateTime() 
    { 
     Thread th = new Thread(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       while(true) 
       { 
        date2 = new java.util.Date(); 
        final String dateTime = dateFormat2.format(date2); 
        /* 
        * Any updates to the GUI, must also be done 
        * on the EDT - Event Dispatch Thread. 
        */ 
        SwingUtilities.invokeLater(new Runnable() 
        { 
         public void run() 
         { 
          getShow_time().setText(dateTime); 
         } 
        });      
       } 
      } 
     }); 
     th.start(); 
    } 

    /** 
    * @return the show_time 
    */ 
    public JLabel getShow_time() { 

     return show_time; 
    } 

    /** 
    * @param show_time the show_time to set 
    */ 
    public void setShow_time(JLabel show_time) { 
     this.show_time = show_time; 
    } 

    /** 
    * @return the time 
    */ 
    public JLabel getTime() { 
     return time; 
    } 

    /** 
    * @param time the time to set 
    */ 
    public void setTime(JLabel time) { 
     this.time = time; 
    } 

    /** 
    * @return the show_date_time 
    */ 
    public JPanel getShow_date_time() { 
     return show_date_time; 
    } 

    /** 
    * @param show_date_time the show_date_time to set 
    */ 
    public void setShow_date_time(JPanel show_date_time) { 
     this.show_date_time = show_date_time; 
    } 

    /** 
    * @return the label1 
    */ 


    /** 
    * @param label1 the label1 to set 
    */ 


    /** 
    * @return the label 
    */ 
    public JLabel getLabel() { 

     return label; 
    } 

    /** 
    * @param label the label to set 
    */ 
    public void setLabel(JLabel label) { 
     this.label = label; 
    } 

    /** 
    * @return the panel 
    */ 
    public JPanel getPanel() { 
     return panel; 
    } 

    /** 
    * @param panel the panel to set 
    */ 
    public void setPanel(JPanel panel) { 
     this.panel = panel; 
    } 
} 
+1

請學習java命名約定並遵守它們。 – kleopatra 2012-07-08 09:34:51

+0

我希望LOL,現在一切都很好,儘管我仍然想在代碼中改變很多東西,但是可能OP會無法理解發生的事情,他/她的原始代碼:-) – 2012-07-08 09:49:56

+1

Always難以取捨;更多[這裏](http://stackoverflow.com/a/11384153/230513)。 – trashgod 2012-07-08 15:13:24

3

由於@nIcE牛的筆記,你的線程不正確同步,invokeLater()是一個正確的解決方案。不幸的是,你的線程也不斷運行,從而妨礙了性能。你應該考慮這些替代品:

  • 差:出處理器的循環:

    Thread.yield(); // prevent thread from starving 
    
  • 更好:添加合適的延遲invokeLater()後,趕上InterruptedException

    try { 
        Thread.sleep(500); 
    } catch (InterruptedException ex) { 
        Logger.getLogger(Date_Time.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    
  • 最佳:使用javax.swing.Timer,如圖所示here

+2

很好的捕獲,大聲笑,當我看到你的答案,我意識到我做了什麼大BLUNDER,我從來沒有想過在這種情況下使用'計時器':-)你提到的鏈接屬於我最喜歡的問題/答案:-) – 2012-07-08 16:14:21