2014-04-02 57 views
0

我試圖在Java中使用get set方法實現當前日期。這是一個Java bean,我應該能夠在GUI中導入時獲取日期。我的問題是它不顯示當前日期,它只是顯示Jlabel文本。JLabel中的日期(Java bean)

下面是我的代碼:

package datebean; 

import javax.swing.border.EtchedBorder; 
import javax.swing.*; 
import java.awt.FlowLayout; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

/** 
* A JavaBean for displaying dates. 
*/ 
public class DateBean extends JPanel { 
    Date date = new Date(); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
    JLabel lblDay = new JLabel("dd"), 
      lblMonth = new JLabel("mm"), 
      lblYear = new JLabel("yy"); 

    public DateBean() { 
     this.setLayout(new FlowLayout()); 
     this.setBorder(new EtchedBorder()); 
     this.add(lblDay); 
     this.add(lblMonth); 
     this.add(lblYear); 
    } 

    public void setDay(String day) { 
     day = dateFormat.format(date); 
     lblDay.setText(day); 
    } 

    public String getDay(String day){ 
     return day; 
    } 

    public void setMonth(String month) { 
     month = dateFormat.format(date); 
     lblMonth.setText(month); 
    } 

    public String getMonth(String month){ 
     return month; 
    } 

    public void setYear(String year) { 
     year = dateFormat.format(date); 
     lblYear.setText(year); 
    } 

    public String getYear(String year){ 
     return year; 
    }  
} 

回答

0

在我看來,你不知道,你想要做什麼......

您可以添加

setDay(null); 
啓動

給你的構造函數,看到一些變化(可能不是你想要的)。

1

您可以使用新的java.time.LocalTime類(在Java8中添加)用於邏輯/計算目的,並使用正常的JLable作爲圖形部分,而不是創建一些瘋狂的自動標籤。

不管怎麼說,這是基於你的概念的例子:

import javax.swing.*; 
import java.time.LocalDate; 
import java.time.format.DateTimeFormatter; 

public class DateLabel extends JLabel { 
    private LocalDate   date; 
    private DateTimeFormatter dtf; 

    /** 
    * Constructor 
    * @param day  The day of the date 
    * @param month The month of the date 
    * @param year  The year of the date 
    * @param pattern The format of the date 
    */ 
    public DateLabel(final int day, final int month, final int year, final String pattern) { 
     date = LocalDate.of(year, month, day); 
     dtf = DateTimeFormatter.ofPattern(pattern); 
     redraw(); 
    } 

    public final int getDay() {return date.getDayOfMonth();} 
    public final int getMonth() {return date.getMonthValue();} 
    public final int getYear() {return date.getYear();} 

    public final void setDay(final int value) { 
     date = LocalDate.of(date.getYear(), date.getMonthValue(), value); 
     redraw(); 
    } 

    public final void setMonth(final int value) { 
     date = LocalDate.of(date.getYear(), value, date.getDayOfMonth()); 
     redraw(); 
    } 

    public final void setYear(final int value) { 
     date = LocalDate.of(value, date.getMonthValue(), date.getDayOfMonth()); 
     redraw(); 
    } 

    private final void redraw() { 
     super.setText(date.format(dtf)); 
    } 
    /** 
    * Method is deprecated, please use {@link #setDay(int)}, {@link #setMonth(int)}, 
    * and {@link #setYear(int)} instead 
    * @param text 
    */ 
    @Deprecated 
    public final void setText(final String text) { 
     super.setText(text); 
    } 

} 

和示例用法:

public static void main(String[] args) { 
    JFrame wnd = new JFrame("DateLabel Test"); 
    wnd.setLocation(200, 200); 
    wnd.setSize(300, 300); 
    wnd.setLayout(null); 
    wnd.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

    DateLabel lblDate1 = new DateLabel(24, 12, 2014, "dd.MM.yyyy"); 
    lblDate1.setLocation(10, 10); 
    lblDate1.setSize(100, 15); 
    wnd.add(lblDate1); 

    DateLabel lblDate2 = new DateLabel(24, 12, 2014, "MM/dd/yyyy"); 
    lblDate2.setLocation(10, 30); 
    lblDate2.setSize(100, 15); 
    wnd.add(lblDate2); 

    wnd.setVisible(true); 

    lblDate2.setDay(lblDate1.getDay() + 7); 
    } 
} 

是這樣的:

enter image description here

+0

謝謝你的答覆。當我導入java.time.LocalDate; import java.time.format.DateTimeFormatter; 它爲什麼說沒有找到包? – Roshan

+1

正如我在上面發佈的帖子中提到的,這個包是在Java8(jdk 1.8.0)中添加的。也許你的系統上安裝了較舊的jdk(和jre)? – ifloop

+0

對不起,我沒有專注。 – Roshan