我的appointmentViewWeeks
构造函数从其他类接收参数并创建GUI。如何在main
不知道year
and值的情况下从我的方法中调用它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] 删除。
我来说两句