0

I have a very simple game I'm creating as a novice project. It's based on an online card game called "Castlewars". There are two players, each with a tower which is affected by cards they and their opponent play. At the moment I have the basic framework of collections and classes I need to make the game operate at a very simple level, but I'm having problems displaying these effects to the user. I have the following code, which should update, amongst other things, two jLabels on a jFrame GUI (constructed in NetBeans 7.4) which represent the player's towers:

private void adjustScreen(){
    System.out.println (Integer.toString(jLabel1.getSize().height));
    jLabel1.setSize(100, (playerRed.getTower().currentHeight() * 2));
    System.out.println(Integer.toString(playerRed.getTower().currentHeight() * 2));
    System.out.println (Integer.toString(jLabel1.getSize().height));
    jLabel2.setSize(100, (playerBlue.getTower().currentHeight() * 2));
    jLabel5.setText(Integer.toString(playerBlue.getTower().currentHeight()));
    jLabel6.setText(Integer.toString(playerBlue.getGold()));
    jLabel9.setText(Integer.toString(playerRed.getTower().currentHeight()));
    jLabel10.setText(Integer.toString(playerRed.getGold()));
    if (TurnBlue){
        jPanel21.setBackground(inPlay);
        jPanel10.setBackground(outPlay);
    }else{
        jPanel10.setBackground(inPlay);
        jPanel21.setBackground(outPlay);
    }
}

When I run it, i get the following output:

  1. (Initial label height) - 200
  2. (adjusted Tower().height) - 100
  3. (adjusted label height) - 100

Which suggests that what I'm trying to do is working on some level, but the actual labels visually stay the same size.

At start (the initiation of the game should have set the labels to half their visible size): At start (the initiation of the game should have set the labels to half their visible size After a couple of plays - the Blue's new tower height is shown in the top left After a couple of plays - the change to Blue's tower height is shown in the top left

I've played around with enabling and disabling the resize property, both on the label and on the frame, and I did once manage to get it to resize, but it then shifted the other components of the frame in an unfortunate way. As you can see, the "Cards" at the bottom do seem to resize themselves, although I am not explicitly instructing them to do this (maybe an effect of the text length in the "Cards"?) What am I doing wrong?

Orphid
  • 2,722
  • 2
  • 27
  • 41
  • As far as I can tell, all of the code is in Swing and there is no JavaFX code at all, so the [javafx] tag and the JavaFX reference in the title should be removed? – jewelsea Mar 24 '14 at 21:37

1 Answers1

2

After re-reading the question I realized the answer is not really an answer to the question but more of a list of suggestions. Rather than deleting it, I'll leave it up in case someone can gain something from it :-)

"What am I doing wrong?"

Welcome to the world of "Why I should use Layout Managers"

There's an ocean of problems that may arise from null layouts and trying to set size and location to everything. Swing was made to be used with Layout Managers.

  • Some layout managers will respect preferred sizes and some wont.

    preferred sizes

  • Use the correct layout manager and make use of nested JPanel with different layout managers to get your desired result

    nest panels

  • Make use of EmptyBorder and vgap and hgap for empty space.

  • Don't set size or location to anything. Let the layout managers take care of that for you.

  • Go over Laying out Components withing a Container to learn the different layout managers.


EDIT

If you're using Netbeans GUI BUilder take a look at this answer for some help with how to use different layout managers using the design tool.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I'm going to mark this as the answer, although in the end I've chucked the GUI builder idea, and just gone for creating a layered graphic applet in Eclipse. – Orphid Mar 25 '14 at 22:38