proper way to creation multiple similiar buttons/panels

Posted by JayAvon on Game Development See other posts from Game Development or by JayAvon
Published on 2011-11-25T01:52:38Z Indexed on 2011/11/25 2:19 UTC
Read the original article Hit count: 245

Filed under:

I have the below Code which i tried to do, but it only shows(the minus/plus button) on the last GirdLayout (Intelligence stat):

    JButton plusButton = new JButton("+");
    JButton minusButton = new JButton("-");

    statStrengthGridPanel = new JPanel(new GridLayout(1,3));
    statStrengthGridPanel.add(minusButton);
    statStrengthGridPanel.add(new JLabel("10"));
    statStrengthGridPanel.add(plusButton);

    statConstitutionGridPanel = new JPanel(new GridLayout(1,3));
    statConstitutionGridPanel.add(minusButton);
    statConstitutionGridPanel.add(new JLabel("10"));
    statConstitutionGridPanel.add(plusButton);

    statDexterityGridPanel = new JPanel(new GridLayout(1,3));
    statDexterityGridPanel.add(minusButton);
    statDexterityGridPanel.add(new JLabel("10"));
    statDexterityGridPanel.add(plusButton);

    statIntelligenceGridPanel = new JPanel(new GridLayout(1,3));
    statIntelligenceGridPanel.add(minusButton);
    statIntelligenceGridPanel.add(new JLabel("10"));
    statIntelligenceGridPanel.add(plusButton);

I know I can do something like I did for the Panel names(have multiple ones), but I did not want to do that for the Panels in the first place. I am trying to use best practice and not have my code be repetitive. Any suggestions??

The goal is to have 4 stats, to assign points to, with decrement and increment buttons(I decided against sliders). Eventually I will have them have upper and lower limits, decrement the "unused" label, and all of that good stuff, but I just want to not be repetitive. Thanks for any help.

© Game Development or respective owner

Related posts about java