Adblock breaks this site

Java Homework Question

Discussion in 'Programming General' started by shorty715, Jan 14, 2012.

  1. shorty715

    shorty715 Member

    Joined:
    Jul 7, 2011
    Posts:
    43
    Referrals:
    0
    Sythe Gold:
    0
    Java Homework Question

    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
    [​IMG]
     
  2. iJava

    iJava .Previously known as RSGoldRush
    $200 USD Donor New

    Joined:
    Nov 21, 2011
    Posts:
    1,197
    Referrals:
    11
    Sythe Gold:
    485
    Discord Unique ID:
    220055593568829441
    Java Homework Question

    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;
    }
     
  3. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Java Homework Question

    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
     
  4. The Black Tux

    The Black Tux Veteran
    The Black Tux Donor Java Programmers PHP Programmers

    Joined:
    Apr 19, 2009
    Posts:
    10,306
    Referrals:
    30
    Sythe Gold:
    55
    Vouch Thread:
    Click Here
    Two Factor Authentication User Cool Kid Former OMM Cook RsProd Sythe Awards 2012 Winner Village Drunk
    Java Homework Question

    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
     
  5. shorty715

    shorty715 Member

    Joined:
    Jul 7, 2011
    Posts:
    43
    Referrals:
    0
    Sythe Gold:
    0
    Java Homework Question

    Thanks everyone for your help, I found a another program that let me design the layout much easier and I got the JPanel's to do what I want. Thanks again!
     
< GrandExchange Grabber | [Source] GetBetween without Regex >


 
 
Adblock breaks this site