Swing Java中的Jcalender

Swing Java中的Jcalender,java,swing,swingutilities,swingbuilder,Java,Swing,Swingutilities,Swingbuilder,//我正在swing中创建自定义jcalender //我面临有关日期设置的问题 //i、 实际月份从星期二开始,从星期三开始 //我发现有关日历模型中设置值的问题 //上面的代码运行良好,但在显示实际日期时出现问题 //请建议我做必要的修改 import java.awt.Color; public class Calendar extends JFrame { private JPanel contentPane; String[] years = { "2008"

//我正在swing中创建自定义jcalender

//我面临有关日期设置的问题 //i、 实际月份从星期二开始,从星期三开始 //我发现有关日历模型中设置值的问题

//上面的代码运行良好,但在显示实际日期时出现问题 //请建议我做必要的修改

import java.awt.Color;


public class Calendar extends JFrame {

    private JPanel contentPane;

     String[] years = { "2008", "2009", "2010" ,"2011","2012","2013","2014"};

      JComboBox comboBox = new JComboBox(years);

      String[] months = { "January", "February", "March", "April", "May", "June", "July", "August",
          "September", "October", "November", "December" };

      JList list = new JList(months);

      JScrollPane scrollPane = new JScrollPane(list);

      CalendarModel model = new CalendarModel();

      JTable table = new JTable(model);

      public Calendar() {
            super();

            getContentPane().setLayout(null);
            comboBox.setBounds(10, 10, 100, 30);
            comboBox.setSelectedIndex(5);
            comboBox.addItemListener(new ComboHandler());
            scrollPane.setBounds(200, 10, 150, 100);
            list.setSelectedIndex(10);
            list.addListSelectionListener(new ListHandler());
            table.setBounds(10, 150, 550, 200);
            model.setMonth(comboBox.getSelectedIndex() + 1998, list.getSelectedIndex());
            getContentPane().add(comboBox);
            getContentPane().add(scrollPane);
            table.setGridColor(Color.black);
            table.setShowGrid(true);
            getContentPane().add(table);

            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            setSize(530,300);
            setVisible(true);


          }


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Calendar frame = new Calendar();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public class ComboHandler implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            System.out.println("ItemState change Method called ");
          model.setMonth(comboBox.getSelectedIndex() + 1998, list.getSelectedIndex());
          table.repaint();
        }
      }

      public class ListHandler implements ListSelectionListener {
        public void valueChanged(ListSelectionEvent e) {
          model.setMonth(comboBox.getSelectedIndex() + 1998, list.getSelectedIndex());
          table.repaint();
        }
      }
    }
    class CalendarModel extends AbstractTableModel {
      String[] days = { "Sun" , "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

      int[] numDays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

      String[][] calendar = new String[7][7];

      public CalendarModel() {

          //setting name of days (mon,tues)
        for (int i = 0; i < days.length; ++i)
        {
            System.out.println("day[i] *** "+days[i]);
            calendar[0][i] = days[i];

        }
        for (int i = 1; i < 7; ++i){
          for (int j = 0; j < 7; ++j)
          {
              System.out.println("calendar[i][j] value i ** "+i +" j value "+ j );
            calendar[i][j] = " ";
          }
        }

      }

      public int getRowCount() {
        return 7;
      }

      public int getColumnCount() {
        return 7;
      }

      public Object getValueAt(int row, int column) {
          System.out.println("get Value at ** "+calendar[row][column]); 
        return calendar[row][column];
      }

      /*
      public void setValueAt(Object value, int row, int column) {
          System.out.println("set Value at ** "+(String) value);
        calendar[row][column] = (String) value;
      }*/

      public void setMonth(int year, int month) {
        for (int i = 1; i < 7; ++i)
        {
          for (int j = 0; j < 7; ++j)
          {
            calendar[i][j] = " ";
          }
        }
        java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
        cal.set(year, month, 1);
        int offset = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK) - 1;
        offset += 7;
        int num = daysInMonth(year, month);
        for (int i = 0; i < num; ++i) {
            System.out.println("offset *** "+Integer.toString(i+1));
          calendar[offset / 7][offset % 7] = Integer.toString(i+1);
          ++offset;
        }
      }

      public boolean isLeapYear(int year) {
          System.out.println("Is Leap Year *** ");
        if (year % 4 == 0)
          return true;
        return false;
      }

      public int daysInMonth(int year, int month) {
          System.out.println("day In month*** ");
        int days = numDays[month];
        if (month == 1 && isLeapYear(year))
          ++days;
        System.out.println("days *** "+days);
        return days;
      }

}
导入java.awt.Color;
公共类日历扩展JFrame{
私有JPanel内容窗格;
字符串[]年={“2008”、“2009”、“2010”、“2011”、“2012”、“2013”、“2014”};
JComboBox组合框=新的JComboBox(年);
字符串[]个月={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”,
“九月”、“十月”、“十一月”、“十二月”};
JList列表=新JList(月);
JScrollPane scrollPane=新的JScrollPane(列表);
CalendarModel=新CalendarModel();
JTable table=新的JTable(模型);
公历(){
超级();
getContentPane().setLayout(null);
组合框.立根(10,10,100,30);
组合框。设置所选索引(5);
addItemListener(新的ComboHandler());
滚动窗格.立根(200,10,150,100);
列表。设置所选索引(10);
addListSelectionListener(新的ListHandler());
表.立根(10150550200);
model.setMonth(comboBox.getSelectedIndex()+1998,list.getSelectedIndex());
getContentPane().add(组合框);
getContentPane().add(滚动窗格);
table.setGridColor(颜色为黑色);
表.设置显示网格(真);
getContentPane().add(表);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置大小(530300);
setVisible(真);
}
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
日历框=新日历();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
公共类ComboHandler实现ItemListener{
公共无效itemStateChanged(ItemEvent e){
System.out.println(“调用ItemState更改方法”);
model.setMonth(comboBox.getSelectedIndex()+1998,list.getSelectedIndex());
表1.repaint();
}
}
公共类ListHandler实现ListSelectionListener{
public void值已更改(ListSelectionEvent e){
model.setMonth(comboBox.getSelectedIndex()+1998,list.getSelectedIndex());
表1.repaint();
}
}
}
类CalendarModel扩展了AbstractTableModel{
String[]days={“Sun”、“Mon”、“Tue”、“Wed”、“Thu”、“Fri”、“Sat”};
int[]numDays={31,28,31,30,31,30,31,30,31,31,31};
字符串[][]日历=新字符串[7][7];
公共日历模型(){
//设置天数名称(周一、周二)
对于(int i=0;i
在setMonth方法中尝试此方法

 public void setMonth(int year, int month) 
改变

 calendar[offset / 7][offset % 7] = Integer.toString(i+1);


在setMonth方法中尝试此方法

 public void setMonth(int year, int month) 
改变

 calendar[offset / 7][offset % 7] = Integer.toString(i+1);


在setMonth方法中尝试此方法

 public void setMonth(int year, int month) 
改变

 calendar[offset / 7][offset % 7] = Integer.toString(i+1);


在setMonth方法中尝试此方法

 public void setMonth(int year, int month) 
改变

 calendar[offset / 7][offset % 7] = Integer.toString(i+1);


试试这个。。在偏移量计算中使用-2而不是-1

 java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
    cal.set(year, month, 1);
    int offset = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK)- 2 ;
    offset += 7;
    int num = daysInMonth(year, month);
    for (int i = 0; i < num; ++i) {
        System.out.println("offset *** "+Integer.toString(i+1));
      calendar[offset / 7][offset % 7] = Integer.toString(i+1);
      ++offset;
java.util.GregorianCalendar cal=new java.util.GregorianCalendar();
校准设置(年、月、1);
int offset=cal.get(java.util.GregorianCalendar.DAY\u OF\u WEEK)-2;
偏移量+=7;
int num=daysInMonth(年,月);
对于(int i=0;i
试试这个..使用-2