• Log in or sign up for an account here: https://www.sythe.org/login

Java Homework Question

shorty715

Member
Joined
Jul 7, 2011
Sythe Gold
0
Messages
43
I've been working on a Java project for school but I'm am having trouble with the JFrame Layout. The items are not being put where I am programming them to be, and sometimes they don't even show up unless I re-size the window.

Also, I have a random number generator that generates a number between 1 and 6. Would I be able to display a different JPanel depending on the number that the generator gave me? Thanks in advance!

Here's what I what the layout to look like
 
PHP Shit Show 2k26
Guessing you're not allowed to use Netbeans to create the GUI?

Just use a switch statement and change the layout/show a different frame for each case,

Code:
swtich(randNumInt) {
case 0 :
//set layout
break;
}
 
To expand on what iJava was saying about displaying the JPanels you could just call setVisible() on each of them to make one visible and one invisible instead of an if or switch statement. ( http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#setVisible(boolean) )
Code:
if(something) {
 panel1.setVisible(true);
 panel2.setVisible(false);
}
else {
 panel1.setVisible(false);
 panel2.setVisible(true);
}

For the layout stuff have a look at the Java tutorial for different layout managers. You will probably have to make use of more than one to do the kind of thing you showed there. You can always post further questions if you get stuck again preferably with some code or screenshot of GUI.

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
 
Two Factor Authentication User
Sythe Awards 2011 Winner
Cook
Cool Kid
Former OMM
RsProd
Sythe Awards 2012 Winner
Village Drunk
What type of layout are you currently using?

The panel thing can be worked out with a simple if after random number is generated and as ^ said, set the number to the label and display the panel that has it
 
duelarena.gg
Back
Top