|
Java help please
ok, I am nearly finished but cannot get the last part of the code to work.
I need to display the days of the selected month and year in my text area
like
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 ..........
I need help with this, please. Here is the code so far.
Everything else is working, but perhaps someone can tell
me why the month is being displayed twice in the pull down
list? I have been at this too long and am starting to see
double and triple, growing grey hairs, etc...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Calendar4 implements ActionListener
{
private JFrame outputFrame;
private Container container;
private JLabel yearLabel, monthLabel;
private JTextField inputYear;
private JButton myButton, calcButton;
private ButtonGroup myButtonGroup;
private JTextArea myTextArea;
private JComboBox inputMonth;
String days[] = {"Su ", " Mo ", " Tu ", " We ", " Th ", " Fr ", " Sa "};
String months[] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
int DaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static void main( String [] args )
{
Calendar4 myFrameDemo = new Calendar4();
}
int userMonth;
int userYear;
int firstDay;
public Calendar4()
{
outputFrame = new JFrame();
outputFrame.setSize(500,400);
outputFrame.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);
container = outputFrame.getContentPane();
container.setLayout( new FlowLayout() );
container.setBackground(Color.RED);
yearLabel = new JLabel( "Enter Year (After 1900)" );
container.add( yearLabel );
inputYear = new JTextField( "", 10 );
container.add( inputYear );
inputYear.addActionListener( this );
monthLabel = new JLabel( "Select Month" );
container.add( monthLabel );
inputMonth = new JComboBox(months);
for (int i = 0; i < 12; i++)
inputMonth.addItem(months[i]);
container.add( inputMonth );
inputMonth.setSelectedIndex(0);
inputMonth.addActionListener(this);
calcButton = new JButton( "Press To Display Calendar" );
container.add( calcButton);
calcButton.addActionListener( this );
myTextArea = new JTextArea(10,40);
container.add( new JScrollPane(myTextArea));
outputFrame.setVisible(true);
}
public void actionPerformed( ActionEvent event )
{
if ( event.getSource() == calcButton )
{
{
userYear = Integer.parseInt(inputYear.getText(), 10);
{
if (isLeapYear(userYear))
myTextArea.append("Is a leap year\n");
else
myTextArea.append("Is not a leap year\n");
}
if (userYear > 1899)
{
userYear = userYear;
}
else
myTextArea.append("\nYear must be 1900 or after\n");
}
userMonth = inputMonth.getSelectedIndex();
for (int i = 0; i < 7; i++)
myTextArea.append(" " + days[i]);
}
}
public boolean isLeapYear( int userYear ) {
if (userYear%4==0 && userYear%100!=0 || userYear%400==0)
return true;
else
return false;
}
int NumberRowsNeeded(int userYear, int userMonth)
{
int numCells;
firstDay = CalcFirstOfMonth(userYear, userMonth);
if ((userMonth == 1) && (firstDay == 0) && !IsLeapYear(userYear))
return (4);
numCells = firstDay + DaysInMonth[userMonth];
if ((userMonth == 1) && (IsLeapYear(userYear))) numCells++;
return ((numCells <= 35) ? 5 : 6);
}
int CalcFirstOfMonth(int userYear, int userMonth)
{
int firstDay;
int i;
firstDay = CalcJanuaryFirst(userYear);
for (i = 0; i < userMonth; i++)
firstDay += DaysInMonth[i];
if ((userMonth > 1) && IsLeapYear(userYear)) firstDay++;
return (firstDay % 7);
}
boolean IsLeapYear(int userYear)
{
if ((userYear % 100) == 0) return((userYear % 400) == 0);
return ((userYear % 4) == 0);
}
int CalcJanuaryFirst(int userYear)
{
return (((userYear - 1900) + 365));
}
}
__________________
|