2017-07-05 10 views
1

私はjCalendarをカレンダーにロードしてからインデックスの日を使用しましたが、毎月の問題が異なるので選択できません。私は21をクリックすると、私は10Jcalendarで選択したアイテムを取得する方法

Calendar cal = Calendar.getInstance(); 

    cal.setTime(jCalendar1.getDate()); 
    int day = cal.get(Calendar.DAY_OF_MONTH); 

    JPanel jpanel = jCalendar1.getDayChooser().getDayPanel(); 
    Component compo[] = jpanel.getComponents(); 
    compo[day].setBackground(Color.red); 

答えて

0
public class CalendarTest2 extends JFrame { 
    private static final long serialVersionUID = 1L; 

    public CalendarTest2() { 
     Calendar cal = Calendar.getInstance(); 
     JCalendar jCalendar1 = new JCalendar(); 
     cal.setTime(jCalendar1.getDate()); 
     int dayToBeSelected = cal.get(Calendar.DAY_OF_MONTH); 
     dayToBeSelected = 21; 

     JPanel jpanel = jCalendar1.getDayChooser().getDayPanel(); 
     Component compo[] = jpanel.getComponents(); 
     for (Component comp : compo) { 
      if (!(comp instanceof JButton)) 
       continue; 

      JButton btn = (JButton) comp; 
      if (btn.getText().equals(String.valueOf(dayToBeSelected))) 
       comp.setBackground(Color.red); 
     } 
     add(jpanel); 
    } 

    public static void main(String[] args) { 
     CalendarTest2 test = new CalendarTest2(); 
     test.setVisible(true); 
     test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     test.setSize(800, 800); 
    } 
} 

代わりのボタンを選択するための」によるインデックス、 てボタンにアクセスしようとするアクセスを選択していますテキスト(日番号)が記載されています。理由は、月のカレンダーは、7x7ファッションで配置された49個のボタンを使用して表示されますas shownです。 例)インデックス0は常に「日曜日」ボタンを指します。

+0

ありがとうございます。しかし、私はこれが選択された日付のインデックスを取得するとは思わない。選択したアイテムを色付けしようとしています。 – Reyfik

+0

ああ..選択した日のボタンの色を変更しますか? – Nandha

+0

はい私はそれを意味する – Reyfik

関連する問題