netbeans 我可以使用JButton的名称来编写for语句吗?

6tqwzwtp  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(150)

我在NetBeans中只为一年编写了一个日历,对于GUI,我计划为一年设置一个典型的日历微调器。这种组合将确定一周中的哪一天是一个月的第一天和最后一天。
我编写的代码的问题是使用日历日按钮的名称作为变量。也许有一种方法可以为NetBeans准备一个语句来读取按钮名称?
对于这个日历,我创建了一个有49个按钮的JPanel(日历最多有7行星期),我尝试将每个按钮命名为“N(number)”(数字从1到49),这样我就可以创建一个for函数,使一个月的最后一天之后的那一天的位置成为下一个月的第一天的位置。
Image of the JPanel
这方面的代码将从2022年1月开始(不计算闰年)。日历的年份必须在“YearC”变量中指定(在这种情况下为2022)。还必须指定的另一个值是在“FirstD”变量中在一年的第一天之前日历中的空位置的数目(在2022年1月的情况下是5)。最后,我使用了一个微调器来表示月份,因为用户必须连续地选择月份,代码才能工作。
代码如下所示:

public void WeekPos(){
        String MonIntCal = MonthCal.getValue().toString();
        int DayN = 0;
        int jub = 0;
        int DayC = 0;
        int FirstD = 6;
        String YearC = "2022";
        YearCal.setText(YearC);
        String Nam = "B"+DayC;
        int LastJan = 0;
        int LastFeb = 0;
        int LastMar = 0;
        int LastApr = 0;
        int LastMay = 0;
        int LastJun = 0;
        int LastJul = 0;
        int LastAug = 0;
        int LastSep = 0;
        int LastOct = 0;
        int LastNov = 0;
        
        switch(MonIntCal){
            case "January": DayN = 31; jub = 1;
            case "February": DayN = 28; jub = 2;
            case "March": DayN = 31; jub = 3;
            case "April": DayN = 30; jub = 4;
            case "May": DayN = 31; jub = 5;
            case "June": DayN = 30; jub = 6;
            case "July": DayN = 31; jub = 7;
            case "August": DayN = 31; jub = 8;
            case "September": DayN = 30; jub = 9;
            case "October": DayN = 31; jub = 10;
            case "November": DayN = 30; jub = 11;
            case "December": DayN = 31; jub = 12;
        }
        switch(jub){
            case 1: for (int i = 1; i<FirstD; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>DayN+FirstD; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN; i=i+1){
                         DayC = 5+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                    }
                    LastJan = DayN+5;
            case 2: for (int i = 1; i<LastJan; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastJan; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastJan+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastFeb = DayC;
                        }    
                    };
            case 3: for (int i = 1; i<LastFeb; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastFeb; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastFeb+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastMar = DayC;
                        }    
                    };
            case 4: for (int i = 1; i<LastMar; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastMar; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastMar+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastApr = DayC;
                        }    
                    };
            case 5: for (int i = 1; i<LastApr; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastApr; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastApr+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastMay = DayC;
                        }    
                    };
            case 6: for (int i = 1; i<LastMay; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastMay; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastMay+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastJun = DayC;
                        }    
                    };
            case 7: for (int i = 1; i<LastJun; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastJun; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastJun+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastJul = DayC;
                        }    
                    };
            case 8: for (int i = 1; i<LastJul; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastJul; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastJul+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastAug = DayC;
                        }    
                    };
            case 9: for (int i = 1; i<LastAug; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastAug; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastAug+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastSep = DayC;
                        }    
                    };
            case 10: for (int i = 1; i<LastSep; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastSep; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastSep+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastOct = DayC;
                        }    
                    };
            case 11: for (int i = 1; i<LastOct; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastOct; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastOct+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                        if(i==DayN){
                            LastNov = DayC;
                        }    
                    };
            case 12: for (int i = 1; i<LastNov; i=i+1){
                        DayC = i;
                        Nam.setText(null);  
                    }
                    for (int i = 49; i>LastNov; i=i-1){
                        DayC = i;
                        Nam.setText(null);
                    }
                    for (int i = 0; i<DayN+1; i=i){
                        DayC = LastNov+i;
                        if(DayC>49){
                         DayC=DayC-49;   
                        }
                        Nam.setText(i.toString());
                    };    
        }        
    }
e5nqia27

e5nqia271#

为什么不将按钮存储在数组或ArrayList中,以便可以通过循环访问它们呢?我坚信无论如何都要通过循环来创建它们。

相关问题