Need help transforming this from a Scanner class to a GUI cl
Need help transforming this from a Scanner class to a GUI class. I need to use a scroll bar for the month and a TextField for the year that must be filled in if the month is February and to use a read only TextField for the line of the poem that you output.
This program needs to be modified so that it instead of using Scanner it uses GUI. If you compile and run the program below it uses the Scanner class to input the data, now I need it changed so that JPanels appears to input the data. So this program has to have panel(JPanel) popup that allows the user to pick the Month using a scroll bar, then in the next panel input the year via TextField and after the year is entered to output the different line depending on what month and year are entered.
import java.util.Scanner;
public class Days {
public static void main(String[] args)
{
String months[]={\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",\"August\",\"September\",\"October\",\"November\",\"December\"};
String line[]=new String[5];
line[0]=\"Thirty days hath September\";
line[1]=\"April, June, and November\";
line[2]=\"All the rest have thirty-one\";
line[3]=\"with February\'s 28 to make it fun\";
line[4]=\"Leap Year happening one in four, Gives February one day more.\";
//Creates Scanner object for keyboard input
Scanner s=new Scanner(System.in);
Scanner set=new Scanner(System.in);
String month=\"\";
while(true)
{
//Prompt user for month and input data
System.out.println(\"Enter a valid month name\");
month=set.nextLine();
if(validateMonth(month,months))
{
break;
}
}
//Prompt user for year and input data
System.out.println(\"Enter the year\");
int year=Integer.parseInt(set.nextLine());
//Determine whether the user inputted January
if(month.equalsIgnoreCase(\"January\"))
{
System.out.println(line[2]);
}
//Determine whether user inputted Feburary
else if(month.equalsIgnoreCase(\"February\"))
{
if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
{
System.out.println(line[4]);
}
else
{
System.out.println(line[3]);
}
}
//Determine which month the user inputted and retrieve correlating line
else if(month.equalsIgnoreCase(\"March\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"April\"))
{
System.out.println(line[1]);
}
else if(month.equalsIgnoreCase(\"May\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"June\"))
{
System.out.println(line[1]);
}
else if(month.equalsIgnoreCase(\"July\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"August\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"September\"))
{
System.out.println(line[0]);
}
else if(month.equalsIgnoreCase(\"October\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"November\"))
{
System.out.println(line[1]);
}
else if(month.equalsIgnoreCase(\"December\"))
{
System.out.println(line[2]);
}
}
//Validate the months entered
private static boolean validateMonth(String month,String months[])
{
boolean result=false;
for(int i=0;i<12;++i)
{
if(month.equalsIgnoreCase(months[i]))
{
result=true;
break;
}
}
return result;
}
}
Solution
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Days {
public static void main(String[] args)
{
String months[]={\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",\"August\",\"September\",\"October\",\"November\",\"December\"};
String line[]=new String[5];
line[0]=\"Thirty days hath September\";
line[1]=\"April, June, and November\";
line[2]=\"All the rest have thirty-one\";
line[3]=\"with February\'s 28 to make it fun\";
line[4]=\"Leap Year happening one in four, Gives February one day more.\";
//Creates Scanner object for keyboard input
Scanner s=new Scanner(System.in);
Scanner set=new Scanner(System.in);
String month=\"\";
while(true)
{
//Prompt user for month and input data
//System.out.println(\"Enter a valid month name\");
month = (String) JOptionPane.showInputDialog(null, \"Choose now...\",
\"month Poem\", JOptionPane.QUESTION_MESSAGE, null, months,
months[1]);
if(validateMonth(month,months))
{
break;
}
}
//Prompt user for year and input data
//System.out.println(\"Enter the year\");
int year=Integer.parseInt(JOptionPane.showInputDialog(\"Year\"));
//Determine whether the user inputted January
if(month.equalsIgnoreCase(\"January\"))
{
System.out.println(line[2]);
}
//Determine whether user inputted Feburary
else if(month.equalsIgnoreCase(\"February\"))
{
if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
{
JOptionPane.showMessageDialog(null, line[4]);
//System.out.println(line[4]);
}
else
{JOptionPane.showMessageDialog(null, line[3]);
//System.out.println(line[3]);
}
}
//Determine which month the user inputted and retrieve correlating line
else if(month.equalsIgnoreCase(\"March\"))
{
JOptionPane.showMessageDialog(null, line[2]);
//System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"April\"))
{
JOptionPane.showMessageDialog(null, line[1]);
//System.out.println(line[1]);
}
else if(month.equalsIgnoreCase(\"May\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"June\"))
{
System.out.println(line[1]);
}
else if(month.equalsIgnoreCase(\"July\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"August\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"September\"))
{
System.out.println(line[0]);
}
else if(month.equalsIgnoreCase(\"October\"))
{
System.out.println(line[2]);
}
else if(month.equalsIgnoreCase(\"November\"))
{
System.out.println(line[1]);
}
else if(month.equalsIgnoreCase(\"December\"))
{
System.out.println(line[2]);
}
}
//Validate the months entered
private static boolean validateMonth(String month,String months[])
{
boolean result=false;
for(int i=0;i<12;++i)
{
if(month.equalsIgnoreCase(months[i]))
{
result=true;
break;
}
}
return result;
}
}



