I'm currently trying to create what I thought was going to be an easy Java program. I want to create a GUI that prompts the user to enter the day of the week and will spit out the chores they have to do that day. I also wanted check boxes next to the chores to be able to mark off what is completed. I can get the code to run fine in the console window (minus the check boxes obviously) but I don't know how to make my code run on the JFrame I created. I know there is also a way to add labels, buttons, textboxes, etc. without actually writing the code and just plotting them on the JFrame, but I can't get my frame to appear without running the code, so it's not editable.
This is where my code sits so far:
/**
* @author AMoore
* @date 4/4/24
* The purpose of this program is to prompt a user to enter a day of the week, give them the daily chores, and allow them to check them off.
*
*/
import java.util.Scanner;
import javax.swing.JFrame;
public class ChoreChartBuilder {
JFrame frame;
public ChoreChartBuilder() {
initComponenent();
}
public void initComponenent() {
frame = new JFrame("Daily Chore Chart");
frame.setSize(600,600);
frame.setVisible(true);
}
public static void main(String[] args) {
new ChoreChartBuilder();
Scanner input = new Scanner(System.in);
String mon = "Monday";
String tue = "Tuesday";
String wed = "Wednesday";
String thu = "Thursday";
String fri = "Friday";
String sat = "Saturday";
String sun = "Sunday";
System.out.println("");
System.out.println("Please enter the day of the week: ");
String day = input.nextLine();
input.close();
if (day.equalsIgnoreCase(mon)) {
System.out.println("Monday Chores: ");
}
else if (day.equalsIgnoreCase(tue)) {
System.out.println("Tuesday Chores: ");
}
else if (day.equalsIgnoreCase(wed)) {
System.out.println("Wednesday Chores: ");
}
else if (day.equalsIgnoreCase(thu)) {
System.out.println("Thursday Chores: ");
}
else if (day.equalsIgnoreCase(fri)) {
System.out.println("Friday Chores: ");
}
else if (day.equalsIgnoreCase(sat)) {
System.out.println("Saturday Chores: ");
}
else if (day.equalsIgnoreCase(sun)) {
System.out.println("Sunday Chores: ");
}
else {
System.out.println("A day of the week was not entered. Goodbye.");
}
}
}
Here is a complete compilable example using a java GUI.
import javax.swing.*;
import java.util.List;
import java.awt.BorderLayout;
public class DayChooser{
void buildGui(){
JFrame frame = new JFrame();
List<String> days = List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" );
JComboBox<String> box = new JComboBox<>(days.toArray(new String[0]));
JList<String> chores = new JList<>(new DefaultListModel<>());
box.addActionListener( evt->{
DefaultListModel<String> model = (DefaultListModel<String>)chores.getModel();
model.clear();
model.addElement(box.getSelectedItem() + " chores");
model.addElement("refactor code");
model.addElement("write tests");
model.addElement("Do java swing tutorial");
});
//add some junk for layout purposes.
DefaultListModel<String> model = (DefaultListModel<String>)chores.getModel();
model.addElement("chores by day");
model.addElement("1.");
model.addElement("2.");
model.addElement("3.");
model.addElement("4.");
frame.add(box, BorderLayout.NORTH);
frame.add(chores, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args){
DayChooser dc = new DayChooser();
SwingUtilities.invokeLater( dc::buildGui );
}
}
Your program flow should react to gui events. Eg. In this example you select a day and it populates a list. You'll probably want to make a class to represent a chore.