Frage

So I am making a Calendar program for my final project in a programming class and to add the days listed in the month I have a whole bunch of JLabels as instance variables (42 for a 6 by 7 grid)

JLabel day1;
JLabel day2;
JLabel day3;
JLabel day4;
JLabel day5;
JLabel day6;
JLabel day7;
JLabel day8;
JLabel day9;
JLabel day10;
etc.

then I have a method that gets the current month I have set and based off of what that returns I have a whole bunch of if statements to set what numbers go to which (I am planning to have the 42 in the set locations and just change their contents based off the month instead of moving the locations of 31)

if (Threads.getMonth() == 0)
    {
        day1 = new JLabel("");
        day2 = new JLabel("");
        day3 = new JLabel("");
        day4 = new JLabel("1");
        day5 = new JLabel("2");
        day6 = new JLabel("3");
        day7 = new JLabel("4");
        day8 = new JLabel("5");
        day9 = new JLabel("6");
        day10 = new JLabel("7");
etc.

my more experienced friend recommended creating the object within the if statements so this is what that looks like. Then after those twelve if statements I have the setting of boundaries and adding it to the frame.

day10.setBounds(100, 100, 500,500);
add(day10);

I had it at that location simply to test that it appeared and used day 10 because no matter which month the 10th day always had an actual value.

Although whenever I run this program the JLabel fails to show up and I cannot get my program to function without being able to do this. If it helps I have around 20 classes (one for each month, a main, a threads class, a gui, and a few others for some other functions that I am still working on.)

This bit of code is coming from the gui class. If it might have a play on this the way I have the classes interact is that the main class calls the threads class the threads get the date and open the class for that month that class opens the gui and then all button presses are run through a handler class that shoots the data to the threads class for getting more done and then you just press the x button to exit.

My gui class name is:

public class Gui extends JFrame

When I call the class I set the flow layout to null and it has worked for everything else(jcomboboxes, jbuttons).

When I use the add(day#) statement since it is extending jframe it doing that should add it to the frame and has worked for all other things including jlabels except for these.

War es hilfreich?

Lösung

You need to add you labels to your panel

//adds label to panel
myPanel.add(label);

Then add panel to frame

//adds panel to frame
myFrame.add(myPanel);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top