Java JTable 实现日历的示例

2025-05-29 0 70

效果图:

Java JTable 实现日历的示例

主要思想:日历最核心的功能就是能显示某年某月对应的日期和星期几。因此只要实现传入具体的年份和月份,得到一组存放了日期的数组a[ ]即可。其中数组的大小设置成42,要考虑的问题是当月的第一天对应星期几。日期数组中的前七个,肯定包含了当月的第一天,把这一天找到,将“1”填入,后面的日期依次累加直到加完该月最后一天为止。

MyCalendar类:

得到用于显示日期数组a[ ]

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59
import java.util.Calendar;

public class MyCalendar {

String day[];

int year = 2020,month=0;

public String[] getDay() {

return day;

}

public void setDay(String[] day) {

this.day = day;

}

public int getYear() {

return year;

}

public void setYear(int year) {

this.year = year;

}

public int getMonth() {

return month;

}

public void setMonth(int month) {

this.month = month;

}

private boolean isLeapYear() {

if(this.year%4==0 && this.year%100!=0){

return true;

}else

if(this.year%400==0){

return true;

}else

return false;

}

//获得显示数组

public String[] getCalendar(){

Calendar calendar=Calendar.getInstance();

String a[]=new String[42];

calendar.set(year,month-1,1);

int weekday=calendar.get(Calendar.DAY_OF_WEEK)-1;

int day=0;

int days = 31;

if (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11)

days = 30;

if (this.month == 2 && isLeapYear())

days = 29;

if (this.month == 2 && !isLeapYear())

days = 28;

for(int i = weekday,n=1;i< weekday +days;i++){

a[i]=String.valueOf(n);

n++;

}

return a;

}

}

MyFrame类:

创造显示面板,主要用到JTable

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147
public class MyFrame extends JFrame implements ActionListener {

//存储数据

MyCalendar calendar = new MyCalendar();

JComboBox choiceYear,choiceMonth;

JTable table = null;

JPanel root = new JPanel();

JLabel lyear, lmonth;

private Object[] name = {"日","一","二","三","四","五","六"};

private TableModel tableModel = new DefaultTableModel(name,6);

// private static int row = 6;

// private static int column = 7;

public MyFrame(String title) {

super(title);

this.setContentPane(root);

root.setLayout(new BorderLayout());

//年月选择栏

choiceYear=new JComboBox();

choiceMonth=new JComboBox();

lyear=new JLabel("年");

lmonth=new JLabel("月 ");

for(int i=1990;i<2050;i++)

choiceYear.addItem(i);

choiceYear.addActionListener(this);

for(int i=1;i<=12;i++)

choiceMonth.addItem(i);

choiceMonth.addActionListener(this);

JPanel pNorth=new JPanel();

pNorth.add(choiceYear);

pNorth.add(lyear);

pNorth.add(choiceMonth);

pNorth.add(lmonth);

root.add(pNorth,BorderLayout.NORTH);

// 表格初始化

setYearAndMonth( 1990, 1);

}

//设置年月日

public void setYearAndMonth(int y,int m){

calendar.setYear(y);

calendar.setMonth(m);

String day[]=calendar.getCalendar();

Vector<Object> rowData = new Vector<>();

int row = 0;

int column = 0;

for(int i = 0; i< 42; i++) {

row = i / 7;

column = i % 7;

tableModel.setValueAt(day[i], row, column);

}

// 创建 JTable,直接重写 isCellEditable(),设为不可编辑

table = new JTable(tableModel){

@Override

public boolean isCellEditable(int row, int column)

{

return false;

}

};

JScrollPane scrollPane = new JScrollPane(table);

root.add(scrollPane, BorderLayout.CENTER);

// 添加到主界面

table.setFillsViewportHeight(true);

table.setRowSelectionAllowed(true); // 整行选择

table.setRowHeight(30);

}

public void actionPerformed(ActionEvent e){

//选择年份

if (e.getSource()==choiceYear){

calendar.setYear((Integer) choiceYear.getSelectedItem());

String day[]=calendar.getCalendar();

Vector<Object> rowData = new Vector<>();

int row = 0;

int column = 0;

for(int i = 0; i< 42; i++) {

row = i / 7;

column = i % 7;

tableModel.setValueAt(day[i], row, column);

}

table = new JTable(tableModel){

@Override

public boolean isCellEditable(int row, int column)

{

return false;

}

};

JScrollPane scrollPane = new JScrollPane(table);

root.add(scrollPane, BorderLayout.CENTER);

// 添加到主界面

table.setFillsViewportHeight(true);

table.setRowSelectionAllowed(true); // 整行选择

table.setRowHeight(30);

}

//选择月份

else if (e.getSource()==choiceMonth){

calendar.setMonth((Integer) choiceMonth.getSelectedItem());

String day[]=calendar.getCalendar();

Vector<Object> rowData = new Vector<>();

int row = 0;

int column = 0;

for(int i = 0; i< 42; i++) {

row = i / 7;

column = i % 7;

tableModel.setValueAt(day[i], row, column);

}

}

table = new JTable(tableModel){

@Override

public boolean isCellEditable(int row, int column)

{

return false;

}

};

JScrollPane scrollPane = new JScrollPane(table);

root.add(scrollPane, BorderLayout.CENTER);

// 添加到主界面

table.setFillsViewportHeight(true);

table.setRowSelectionAllowed(true); // 整行选择

table.setRowHeight(30);

}

}

ShowView类:

用于显示窗口,照抄即可,无需理解。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39
import java.awt.Container;

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class ShowView

{

private static void createGUI()

{

// 语法:因为MyFrame是JFrame的子类,所以可以这么写

JFrame frame = new MyFrame("日历");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// 设置窗口的其他参数,如窗口大小

frame.setSize(400, 300);

// 显示窗口

frame.setVisible(true);

}

public static void main(String[] args)

{

// 此段代码间接地调用了 createGUI()

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run()

{

createGUI();

}

});

}

}

以上就是Java JTable 实现日历的示例的详细内容,更多关于Java JTable 实现日历的资料请关注快网idc其它相关文章!

原文链接:https://www.cnblogs.com/qianji-7/p/13759500.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java JTable 实现日历的示例 https://www.kuaiidc.com/116666.html

相关文章

发表评论
暂无评论