Java-Display swing GUI

三阮

我的appointmentViewWeeks构造函数从其他类接收参数并创建GUI。如何在main不知道yearand值的情况下从我的方法中调用它month

public class appointmentViewWeeks extends Frame{

    public appointmentViewWeeks(String year,String month){

        List<LocalDate> mondays = new ArrayList<LocalDate>();
        Month m = Month.valueOf(monthIntToString(month).toUpperCase());

        LocalDate date = Year.of(Integer.valueOf(year)).atMonth(m).atDay(1).
             with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        Month mi = date.getMonth();
        while (mi == m) {
            mondays.add(date);
            date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
            mi = date.getMonth();
        }
        String[] mondaysList = mondays.toArray(new String[0]);

        JComboBox mondayList = new JComboBox(mondaysList);
        JLabel welcome = new JLabel("Please choose a monday in "+month+"/"+year);

        Container contentPane = getContentPane();
        contentPane.add(mondayList);
        contentPane.add(welcome);
    }

    public String monthIntToString(String month) {
         return new DateFormatSymbols().getMonths()[Integer.valueOf(month)-1];
    }

    public static void main(String[] args) {

        //windows feel and look
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | 
            InstantiationException | 
            IllegalAccessException | 
            UnsupportedLookAndFeelException e) {
                e.printStackTrace();
            }
        appointmentViewWeeks view = new appointmentViewWeeks();
        view.setVisible(true);
    }
}
古利

Strings除非您添加不带String参数的第二个构造函数,否则您将需要一年和一个月的时间来创建它(或仅两个)。

public appointmentViewWeeks() {
    //Code to create the object without using the String parameters...
    //Maybe just call your original constructor with some default values?
}

您还可以获取当前的年份和月份,并使用当前的构造函数创建它:

LocalDateTime date = LocalDateTime.now();
appointmentViewWeeks view = new appointmentViewWeeks(String.valueOf(date.getYear()),
                                                        String.valueOf(date.getMonth()));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章