2012-04-28 104 views
2

我一直在這裏關注本教程:Link to tutorial。我似乎無法讓應用程序正常顯示。當我運行應用程序,我希望看到像教程CalendarCanvas屏幕,但我得到這個:Java ME日曆不顯示

enter image description here

這裏是我的代碼,我使用的是標準MIDP類。

類CreateCalendar:

import java.util.Date; 
import java.util.Calendar; 
import javax.microedition.lcdui.*; 
import javax.microedition.midlet.MIDlet; 


public class CreateCalendar 
{ 
    /** 
    * Array of strings which holds data for the month and day 
    * for the calendar application. 
    */ 
    static final String[] month_labels = new String[] 
    { 
     "January", "Febuary", "March", "April", "May", "June", "July", "August", "Sepetember", "October", "November", "Decemeber" 
    }; 
    static final String[] weekdays_labels = new String[] 
    { 
     "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun" 
    }; 

    public int startWeekday = 0; 
    public int padding = 1; 
    public int borderWidth = 4; 
    public int borderColor = 0x009900; 

    /** 
    * Weekday Labels 
    */ 
    public Font weekdayFont = Font.getDefaultFont(); 
    public int weekdayBackgroundColor = 0x009900; 
    public int weekdayColor = 0xffffff; 

    /** 
    * Month/Year Labels 
    */ 
    public Font headerFont = Font.getDefaultFont(); 
    public int headerBackgroundColor = 0x009900; 
    public int headerColor = 0xffffff; 

    /** 
    * Cells Labels 
    */ 
    public Font font = Font.getDefaultFont(); 
    public int foreColor = 0xffffff; 
    public int backgroundColor = 0x009900; 
    public int selectedBackgroundColor = 0xCCFF00; 
    public int selectedForegroundColor = 0xffffff; 

    /** 
    * Size properties 
    */ 
    int width = 0; 
    int height = 0; 
    int headerHeight = 0; 
    int weekHeight = 0; 
    int cellWidth = 0; 
    int cellHeight = 0; 

    /** 
    * Internal time properties 
    */ 
    long currentTimeStamp = 0; 
    Calendar calendar = null; 
    int weeks = 0; 

    public CreateCalendar(Date date) 
    { 
    calendar = Calendar.getInstance(); 
    setDate(date); 
    initialize(); 
    } 

    public Date getSelectedDate() 
    { 
     return calendar.getTime(); 
    } 

    public void setDate(Date d) 
    { 
     currentTimeStamp = d.getTime(); 
     calendar.setTime(d); 
     this.weeks = (int)Math.ceil(((double)getStartWeekday() + getMonthDays())/7); 
    } 

    public void setDate(long timestamp) 
    { 
     setDate(new Date(timestamp)); 
    } 

    public void initialize() 
    { 
     this.cellWidth = font.stringWidth("MM") + 2 * padding; 
     this.cellHeight = font.getHeight() + 2 * padding; 
     this.headerHeight = headerFont.getHeight() + 2 * padding; 
     this.weekHeight = weekdayFont.getHeight() + 2 * padding; 
     this.width = 7 * (cellWidth + borderWidth) + borderWidth; 
     initHeight(); 
    } 

    void initHeight() 
    { 
     this.height = headerHeight + weekHeight + this.weeks * (cellHeight + borderWidth) + borderWidth; 
    } 

    int getMonthDays() 
    { 
     int month = calendar.get(Calendar.MONTH); 
     switch (month) 
     { 
      case 3: 
      case 5: 
      case 8: 
      case 10: 
       return 30;  
      case 1: 
       int year = calendar.get(Calendar.YEAR); 
       return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 29 : 28; 
      default: 
       return 31; 
     } 
    } 

    int getStartWeekday() 
    { 
     Calendar c = Calendar.getInstance(); 
     c.set(Calendar.MONTH, calendar.get(Calendar.MONTH)); 
     c.set(Calendar.YEAR, calendar.get(Calendar.YEAR)); 
     c.set(Calendar.DAY_OF_MONTH, 1); 
     return (c.get(Calendar.DAY_OF_WEEK) + 5) % 7; 
    } 

    public void KeyPressed(int key) 
    { 
     switch(key) 
     { 
      case Canvas.UP: 
       go(-7); 
       break; 
      case Canvas.DOWN: 
       go(7); 
       break; 
      case Canvas.RIGHT: 
       go(1); 
       break; 
      case Canvas.LEFT: 
       go(-1); 
       break; 
     } 
    } 

    void go(int delta) 
    { 
     int prevMonth = calendar.get(Calendar.MONTH); 
     setDate(currentTimeStamp + 864000000 * delta); 
     if(calendar.get(Calendar.MONTH) != prevMonth) 
     { 
      initHeight(); 
     } 
    } 

    public void paint(Graphics g) 
    { 
     g.setColor(backgroundColor); 
     g.fillRect(0, 0, width, height); 
     g.setFont(headerFont); 
     g.setColor(headerColor); 
     g.drawString(month_labels[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.YEAR), width/2, padding, Graphics.TOP | Graphics.HCENTER); 
     g.translate(0, headerHeight); 
     g.setColor(weekdayBackgroundColor); 
     g.fillRect(0, 0, width, weekHeight); 
     g.setColor(weekdayColor); 
     g.setFont(weekdayFont); 

     for(int i = 0; i < 7; i++) 
     { 
      g.drawString(weekdays_labels[(i + startWeekday) % 7], borderWidth + i * (cellWidth + borderWidth) + cellWidth/2, padding, Graphics.TOP | Graphics.HCENTER); 
     } 

     g.translate(0, weekHeight); 
     g.setColor(borderColor); 

     for(int i = 0; i <= weeks; i++) 
     { 
      g.fillRect(0, i * (cellHeight + borderWidth), width, borderWidth); 
     } 
     for(int i = 0; i <=7; i++) 
     { 
      g.fillRect(i * (cellWidth + borderWidth), 0, borderWidth, height - headerHeight - weekHeight); 
     } 

     int days = getMonthDays(); 
     int dayIndex = (getStartWeekday() - this.startWeekday + 7) % 7; 
     g.setColor(foreColor); 
     int currentDay = calendar.get(Calendar.DAY_OF_MONTH); 

     for(int i = 0; i < days; i++) 
     { 
      int weekday = (dayIndex + i) % 7; 
      int row = (dayIndex + i)/7; 
      int x = borderWidth + weekday * (cellWidth + borderWidth) + cellWidth/2; 
      int y = borderWidth + row * (cellHeight + cellWidth) + padding; 

      if(i + 1 == currentDay) 
      { 
       g.setColor(selectedBackgroundColor); 
       g.fillRect(borderWidth + weekday * (cellWidth + borderWidth), borderWidth + row * (cellHeight + borderWidth), cellWidth, cellHeight); 
       g.setColor(selectedForegroundColor); 
      } 

      g.drawString("" + (i + 1), x, y, Graphics.TOP | Graphics.HCENTER); 

      if(i + 1 == currentDay) 
      { 
       g.setColor(foreColor); 
      } 
     } 
     g.translate(0, - headerHeight - weekHeight); 
    } 

    private Date getTime() { 
     throw new UnsupportedOperationException("Not yet implemented"); //TODO get current Time 
    } 

類CalFrontEnd(擴展的MID​​let):在類CreateCalendar

public class CalFrontEnd extends MIDlet 
{ 

    public CreateCalendar calendar; 
    protected Display display; 
    protected Form mainForm; 

    public CalFrontEnd() 
    { 

    } 

    public void startApp() 
    { 
     calendar = new CreateCalendar(new Date()); 
     calendar.headerFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE); 
     calendar.weekdayFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM); 
     calendar.weekdayBackgroundColor = 0xccccff; 
     calendar.weekdayColor = 0x0000ff; 
     calendar.headerColor = 0xffffff; 
     calendar.initialize(); 


    display.getDisplay(this).setCurrent(
    new intCalendar(this)); 

    } 

    public void pauseApp() 
    { 
    } 

    public void destroyApp(boolean destroy) 
    { 
    notifyDestroyed(); 
    } 
}} 
+0

最可能出錯的第一件事是方法名稱'KeyPressed'。 Java是區分大小寫的,並且您參考的教程,它有'keyPressed',以小寫'k'開始,這對於匹配Canvas API非常重要。另外,用你的[代碼片段](http://www.sscce.org/「最好是Short,Self Contained,Correct(Compilable),Example」),不能說出'new intCalendar(this) ' - 這使得無法確定發生了什麼問題 – gnat 2012-04-28 18:51:50

回答

1

代碼看起來非常問題

prior question你提到幾個小變量名稱的差異做了代碼從教程,但從你的代碼片段顯示,這是不是。

要找到重用教程代碼的方法,最直接的方法將如下所示。

  1. 複製從教程中的源代碼 - 文件CalendarWidget.java和CalendarCanvas.java
    複印原樣,如果需要只調整包語句。

  2. 修改CalFrontEnd樣,如果需要如下

    • ,添加import語句CalendarCanvas
    • 與CalendarCanvas簡單的調用取代當前的代碼中的startApp,像這樣:
       
      public void startApp() { 
          Display.getDisplay(this).setCurrent(
            new CalendarCanvas(this)); 
      }
  3. 測試代碼,調整並修復它,直到MIDlet顯示您期望的CalendarCanvas

  4. 完成上述步驟後,請繼續修改代碼以進一步滿足您的需求。
    不要忘記測試你所做的改變,以確保事情的確如你所期望的那樣工作。