Hello... I am working on a project for my intro to Java class and I am stuck. We have to design a functioning calendar, and I am using GregorianCalendar to pull my Current Day, Month, Year, etc. For some reason the month is off by 1 month though (6 instead of 7). Also, I would much rather return the name of the month (July instead of 6) but I can't seem to figure it out. Lastly, I can't seem to get the number of days to fall on the buttons in the proper place. I know I'm asking a lot, but this assignment is really hard, and I'm trying to get as much help as I can because I am stuck...
Here is my code...
public class Calendar extends JFrame { public Calendar() { this.setSize(640,480); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); this.setTitle("Calendar"); JPanel borderPanel = new JPanel(); borderPanel.setLayout(new BorderLayout()); getContentPane().add(borderPanel); // Initialize GregorianCalendar GregorianCalendar cal = new GregorianCalendar(); int currYear = cal.get(GregorianCalendar.YEAR); int currDOM = cal.get(GregorianCalendar.DAY_OF_MONTH); int currMnth = cal.get(GregorianCalendar.MONTH); int nod, som; nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); //Number of Days som = cal.get(GregorianCalendar.DAY_OF_WEEK); // Start of the Month // Button Layout final int ROWS = 6; final int COLS = 7; JButton[][] days; JPanel calendar = new JPanel(); calendar.setLayout(new GridLayout(6,7)); days = new JButton[ROWS][COLS]; for(int row = 0; row < ROWS; row++) { for(int col = 0; col < COLS; col++) { days[row][col] = new JButton(""); calendar.add(days[row][col]); } } // Add Some Number to the Buttons int[] tableMonth = {31,28,31,30,31,30,31,31,30,31,30,31}; int startDay = som; int daysInMonth = tableMonth[currMnth]; for(int i = 0; i < daysInMonth; i++){ days[(startDay + i)/7][(startDay + i) % 7].setText("" + (i + 1)); } borderPanel.add(calendar, BorderLayout.CENTER); // North Panel - Current Month / Prev & Next Button JPanel month = new JPanel(); month.add(new JButton("Previous")); month.add(new JButton("The Month is: " + currMnth)); month.add(new JButton("Next")); borderPanel.add(month, BorderLayout.NORTH); // West Panel - Current Date JPanel year = new JPanel(); year.add(new JButton("The Year is: " + currYear )); borderPanel.add(year, BorderLayout.SOUTH); // South Panel - Current Year JPanel today = new JPanel(); today.add(new JButton("Today is: " + currMnth + "/" + currDOM + "/" + currYear)); borderPanel.add(today, BorderLayout.WEST); } public static void main(String[] args) { new Calendar(); } }
Thank you so much for any help!