2015-12-13 41 views
1

我在写简单的日历应用程序。我有一个函数来绘制不同的月份,但不知怎的,其中一些被搞乱了。这里: enter image description hereGridLayout搞乱我的标签

正如你可以看到5月(5)和8月(8)不正确显示。在不同的年份,不同的月份被打破。这里的函数负责绘制这些:

JPanel month = new JPanel(); 

month.setBackground(Color.white); 
month.setPreferredSize(new Dimension(150,180)); 
month.setLayout(new GridLayout(6,6)); 
for (int j=0; j<=6; j++) //adding days of week labels 
{ 
     JLabel dayNameLabel = new JLabel(days[j]); 
     dayNameLabel.setFont(new Font("Monospace", Font.ITALIC, 12)); 
     dayNameLabel.setOpaque(true); 
     dayNameLabel.setForeground(Color.gray.darker()); 
     dayNameLabel.setBackground(Color.white); 
     month.add(dayNameLabel); 

} 
int dayOfWeek=new GregorianCalendar(myDate.year,argMonth, 1).get(Calendar.DAY_OF_WEEK); 
int howMuchDays=new GregorianCalendar(myDate.year,argMonth, 1).getActualMaximum(Calendar.DAY_OF_MONTH); 

for(int j=0;j<dayOfWeek-1;j++){ //Adding blank spaces so every day can be on it's spot 
    JLabel blank = new JLabel(" "); 
    month.add(blank); 
} 


for (int j = 1; j<howMuchDays+1;j++) 
{ 
     JLabel dayField = new JLabel(Integer.toString(j)); 
     dayField.setFont(new Font("Monospace", Font.BOLD, 12)); 
     dayField.setOpaque(true); 
     dayField.setBackground(Color.white); 
     if (new GregorianCalendar(myDate.year, argMonth, j).get(Calendar.DAY_OF_WEEK) == 7) 
      dayField.setForeground(Color.RED); //make sunday red 
     else 
      dayField.setForeground(Color.BLACK); 

     month.add(dayField); 
} 


add(month); 

我试图改变字体或发现了一些依赖,但是我还没有得到无处

回答

4

new GridLayout(6,6)?这是没有意义的,因为你不希望有一个6×6格。

难道你不是想用new GridLayout(0, 7)?这代表变量数列,但总是有7列,这应该是正是你需要的网格。

+0

哎呀,我的坏... – user3713267