//这是主Second文件
package yan;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import yan1.YearMonth;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Second extends JFrame implements ActionListener{
int year=2023;
int month=4;
private int y=0;
private int x=0;
private int n=0;
private static int monthday;
private int dayofweek;//
JComboBox jComboBox_1=new JComboBox();
JComboBox jComboBox_2=new JComboBox();
Font an=new Font("华文楷体",Font.BOLD,25);
public void Sec() {//建造窗体
draw();
year();
month();
weekword();
getyearmonth();
getweekday();
function();
}
public void draw() { //建造界面和标题
this.setLayout(null);
setTitle("2022141461175_夏弋昕");
setSize(800,800);
setLocation(200, 100);
}
public void year() {//写出年
for(int i=0;i<11;i++) {
jComboBox_1.addItem(i+2013);
jComboBox_1.setFont(an);
}
jComboBox_1.setBounds(100,10,100,70);
jComboBox_1.setMaximumRowCount(4);
this.add(jComboBox_1);
}
public void month() {//写出月
for(int i=0;i<12;i++) {
jComboBox_2.addItem(i+1);
jComboBox_2.setFont(an);
}
jComboBox_2.setBounds(400,10,100,70);
jComboBox_2.setMaximumRowCount(4);
this.add(jComboBox_2);
}
public void weekword() {//写其他文字
JLabel jLabel_1=new JLabel("年:");
jLabel_1.setForeground(Color.blue);//设置文字1的颜色 setForeground
jLabel_1.setFont(an);//设置文字1的字体 setFont
jLabel_1.setBounds(10,10,100,100);//设置文字1的距离
JLabel jLabel_2=new JLabel("月:");
jLabel_2.setForeground(Color.blue);//设置文字1的颜色 setForeground
jLabel_2.setFont(an);//设置文字1的字体 setFont
jLabel_2.setBounds(300,10,400,100);//设置文字1的距离
JLabel jLabel_3=new JLabel(" 一 二 三 四 五 六 日");
jLabel_3.setFont(an);
jLabel_3.setBounds(30, 70, 700, 60);
this.add(jLabel_1);
this.add(jLabel_2);
this.add(jLabel_3);
}
public void function() {//算日历
getyearmonth();
this.setLayout(null);
for(int i=0;i<42;i++){
x+=50;
if(i>=dayofweek-1&&n<monthday) { n++;
JLabel jLabel_i=new JLabel(" "+n);
jLabel_i.setBounds(x,100+y,100,100);
this.add(jLabel_i);
jLabel_i.setFont(an);
setVisible(true);
}
if((i+1)%7==0) {
y+=100;
x=0;
}
setVisible(true);
}
YearMonth yearmonth= new YearMonth(year,month,dayofweek,monthday);
}
public void getyearmonth() {//得到下拉框中的值
year= Integer.parseInt(jComboBox_1.getSelectedItem().toString());
month=Integer.parseInt(jComboBox_2.getSelectedItem().toString());
YearMonth yearmonth= new YearMonth(year,month,dayofweek,monthday);
}
public int mont(int a) {//计算月份天数
int days = 0;
switch(a) {
case 1:days=31;break;
case 2:days=28;break;
case 3:days=31;break;
case 4:days=30;break;
case 5:days=31;break;
case 6:days=30;break;
case 7:days=31;break;
case 8:days=31;break;
case 9:days=30;break;
case 10:days=31;break;
case 11:days=30;break;
case 12:days=31;break;
}
return days;
}
public void change() {//改变
jComboBox_1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
// TODO 自动生成的方法存根
if(e.getStateChange()==ItemEvent.SELECTED) {
function();
}
}
});
}
public void getweekday() {
LocalDate date=LocalDate.of(year,month, 1);
DayOfWeek weekday=date.getDayOfWeek();
dayofweek=weekday.getValue();
monthday=mont(month);
}
public void clear() {
this.removeAll();
this.repaint();
}
public static void main(String[] args) {
Second two=new Second();
two.Sec();
two.change();
System.out.println(monthday);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
}
//这是封装文件
package yan1;
public class YearMonth {
private int year;
private int month;
private int monthday;
private int dayofweek;
public YearMonth (){
}
public YearMonth (int year, int month,int dayofweek, int monthday){
this.dayofweek=dayofweek;
this.monthday=monthday;
this.year=year;
this.month=month;
}
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;
}
public int getdayofweek() {
return dayofweek;
}
public void setdayofweek(int dayofweek) {
this.dayofweek=dayofweek;
}
public int getmonthday() {
return monthday;
}
public void setmonthday(int monthday) {
this.monthday=monthday;
}
}
想要实现下拉框选择的同时显示月份日历
但是只有最初的2013年1月的日历
再选择年份没用
新学java
如何解决