|
|
#1 (permalink) |
|
hyrev
is dog tired
GTR Register Member
Join Date: Apr 2004
Location: Not in Japan anymore - :(
Cars owned: Top Secret built R34 GT-R
Posts: 4,234
|
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)); } }
__________________
|
|
|
|
|
|
#2 (permalink) |
|
moz
has no status.
GTR Register Member
Join Date: Mar 2005
Location: Finland
Cars owned: R32 GT-R
Posts: 281
|
First, the months are displayed twice because of this:
The combobox is created with a string[] parameter of values. Code:
inputMonth = new JComboBox(months); Code:
for (int i = 0; i < 12; i++)
{
inputMonth.addItem(months[i]);
}
Code:
JComboBox(Object[] items) Creates a JComboBox that contains the elements in the specified array Code:
inputMonth = new JComboBox(); // No parameter
for (int i = 0; i < 12; i++) // Fill in values
{
inputMonth.addItem(months[i]);
}
Code:
inputMonth = new JComboBox(months); ![]()
__________________
|
|
|
|
|
|
#3 (permalink) |
|
moz
has no status.
GTR Register Member
Join Date: Mar 2005
Location: Finland
Cars owned: R32 GT-R
Posts: 281
|
This works, just the formatting of the output needs sorting from the DisplayCalendar() method, I'm not to great with formatting. I'm sure you'll manage
![]() Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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
};
protected String[][] calendar =
{
{ " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " " },
{ " ", " ", " ", " ", " ", " ", " " }
};
public static void main(String[] args)
{
new Calendar4();
}
public Calendar4()
{
outputFrame = new JFrame();
outputFrame.setSize(500, 400);
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_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);
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));
inputYear.setText("2008");
outputFrame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == calcButton)
{
myTextArea.setText("");
if (!YearInputIsValid()) return;
int userYear = Integer.parseInt(inputYear.getText(), 10);
myTextArea.append(isLeapYear(userYear) ? "Is a leap year.\n" : "Is not a leap year.\n");
fillCalendar(userYear, inputMonth.getSelectedIndex());
DisplayCalendar();
}
}
private void DisplayCalendar()
{
for (int i = 0; i < calendar.length; i++)
{
for (int j = 0; j < calendar[i].length; j++)
myTextArea.append(calendar[i][j] + " ");
myTextArea.append("\n");
}
}
private boolean YearInputIsValid()
{
if (inputYear.getText().equals(""))
{
myTextArea.append("Please enter a valid year value.\n");
return false;
}
try
{
Integer.parseInt(inputYear.getText(), 10);
}
catch (Exception e)
{
myTextArea.append("The entered values must consist only of numbers,\n");
return false;
}
if (Integer.parseInt(inputYear.getText(), 10) < 1899)
{
myTextArea.append("Year must be 1900 or after\n");
return false;
}
return true;
}
private void InitialiseCalendar()
{
for (int i = 0; i < calendar.length; i++)
{
for (int j = 0; j < calendar[i].length; j++)
calendar[i][j] = " ";
}
}
private void fillCalendar(int userYear, int userMonth)
{
InitialiseCalendar();
int xNum = (CalcFirstOfMonth(userYear, userMonth));
int yNum = 0;
int numDays = DaysInMonth[userMonth] + ((isLeapYear(userYear) && (userMonth == 1)) ? 1 : 0);
int day = 1;
do
{
calendar[yNum][xNum] = String.valueOf(day);
xNum++;
if (xNum > 6)
{
xNum = 0;
yNum += 1;
}
day++;
} while (day <= numDays);
}
private int CalcFirstOfMonth(int userYear, int userMonth)
{
int startDay;
if ((userMonth < 0) || (userMonth > 11))
{
return (-1);
}
startDay = CalcJanuaryFirst(userYear);
for (int i = 0; i < userMonth; i++)
{
startDay += DaysInMonth[i];
}
if ((userMonth > 1) && isLeapYear(userYear))
{
startDay++;
}
return (startDay % 7);
}
private int CalcJanuaryFirst(int userYear)
{
return ((5 + (userYear - 1582) + CalcLeapYears(userYear)) % 7);
}
private int CalcLeapYears(int userYear)
{
int leapYears;
int hundreds;
int fourHundreds;
leapYears = (userYear - 1581) / 4;
hundreds = (userYear - 1501) / 100;
leapYears -= hundreds;
fourHundreds = (userYear - 1201) / 400;
leapYears += fourHundreds;
return (leapYears);
}
private boolean isLeapYear(int userYear)
{
if ((userYear % 4 == 0) && (userYear % 100 != 0) || (userYear % 400 == 0))
{
return true;
}
return false;
}
}
CalendarTable Hope you don't mind the changes to some of your code, I get carried away sometimes Maybe you can just take what you need to fix the original code.
__________________
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|