Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!

Fred Kleinschmidt

Bartender
+ Follow
since Oct 13, 2015
Cows and Likes
Cows
Total received
9
In last 30 days
0
Total given
0
Likes
Total received
132
Received in last 30 days
2
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Fred Kleinschmidt

If you know the size of the text string will not change, you can just use the first line of my example.

However, depending on what font you are using, '1' might be smaller than '0'
Also, if you change the label text from, say,  "5" to "12", the new label might be clipped unless you ask its parent to reconfigure itself.
That's what the other 4 lines are for. The first one gets the parent of the label, and if the parent exists it requests the parent to reconfigure itself.
It is necessary to check for null if there is a chance that you change the value before adding the label to some container.
10 hours ago
You need to keep a reference to the JLabel instance, and whenever you change the value of your variable, you then change the text of the label.
So, for example,

the revalidate() call is necessary because the size of the text might change with the new text so you need to have the parent of the label reconfigure its size.
11 hours ago
Your setDecision_variables() method is declared to take a single input parameter, a 3-d int array.
You call it on line 31 passing "[1][1][1]=1" which is certainly not a legal 3D int array.

Your setDecision_variables() is declared as a method that takes no parameters and returns a 3D int array.
On line 32 you call it with the illegal syntax "[1][1][1]" as an argument, but declared it as a method that takes no arguments.

Perhaps you mean something like:
Note that this is probably not the best way to do things. A better way would be do declare the methods taking three indices:
and similarly for the setter.
Note that you should also check the validity of the indices passed.
1 week ago
Also, note that you should probably not name your class "Integer" since Java has a standard class by that name.
4 weeks ago
That depends on how the City class is written. Is numOfResidents a public variable (hopefully not), or is it a private variable with a public getter method, such as getNumOfResidents() ?
4 weeks ago
If you want to assign different behaviors for different types of Sprites, you should consider making each different type of Sprite a subclass of Sprite.

Then it would not be necessary to check what kind Sprite it is in order to determine its behavior, since each subclass instance knows its own behavior.
1 month ago
If you parse numberOfAngles as an int, then your switch statement should use ints, not Strings:
For example,
should just be
1 month ago
And to print just the initials, it might be better (or more clear) to use charAt(0) instead of substring(0,1).
1 month ago
Also, you have inverted the order of arguments ot the add() method:
2 months ago
And why do you initialize counter to one, when nothing has been entered yet? At the end of the first trip through the loop, when you have entered one number, your value of counter will be two.
2 months ago
There are numerous problems with this coed.
Firs, if you are using Swing, you should not extend Frame, but rather JFrame.

Second, it is usually frowned upon for your program to extend JFrame, either.
It would be better to extend JPanel and have your main() method create a JFrame and put your panel in that JFrame.

Third, the first line of your paintComponent() method should be .
Fourth, you do not have a main() method.
Fifth, all of the GUI code should be executed on the EventThread, not the main thread.

You should start with something much simpler than this. For example, just write something that displays "Hello" in a JLabel that is displayed in your JFrame.
Once you get that to work, then you can go on to show your panel with its buttons, etc.
When you get that to work, then you can go on to add the listeners and get that part to work, too.
2 months ago
Do not call window.pack() until after you have finished creating the entire GUI.
Calling pack causes the JFrame to ask its children what size they prefer to be so it can determine how lay everything out properly.
You are calling pack() immediately after creating the JFrame, before you have added any children to it. Since id has no children yet,  it will decide that its best size is zero by zero.

And if you call pack() just after adding the jPanel to it, the same thing will occur, since you have not yet added anything to the jPanel.

Also, you are calling window.setSize() after calling pack(). so the effects of the pack() statement will be ignored.
You should let the layout manager figure out the proper size rather than calling setSize on the frame.


You should call pack() just before calling setVisible(), and that should be the last statement in the method that creates the entire GUI.
2 months ago
Oh - I just noticed that myself. The statement inside the loop should be print(), not println().
I would still print the newline even if the parameter is less than 1; otherwise the user might think the program is still thinking...
Of course, one could add an if-else and print an error message if the passed parameter is out of range.
2 months ago
If the value passed as x is less than Zero, the while loop will never be entered.
But maybe there should be a check in order to not print the newline.
2 months ago
Campbell's post is the right approach.

But for the purpose of answering your question, the reason your code does not open a second frame is that your run() method calls logWindow() which creates and tries to display the log window, and then returns immediately before you have had a chance to interact with that window, with authorization still true. The if-test in run() then fails (because authorization is still true), and so nothing else happens.

The contents of the log window might not show up, since you set it visible before you add any content to it.
The call to setVisible(true) should be done AFTER you finish building the contents.  
2 months ago