Format of JPanel

I have set of code that seems somewhat messy. I was wondering how to tidy it up

JPanel inventory = new JPanel();
    JPanel options = new JPanel();
    JPanel planet = new JPanel();
    JPanel mainPanel = new JPanel();
    JFrame mainFrame = new JFrame("Inventory");
    inventTitle = new JLabel("Inventory");
    inventMoney = new JLabel("   - " + money);
    optTitle = new JLabel("Options");
    opt1Label = new JLabel("Mine Ice -1 P, +100 M");
    opt2Label = new JLabel("Heat Planet -500 M, +10T");
    opt3Label = new JLabel("BLANK");
    opt4Label = new JLabel("BLANK");
    opt5Label = new JLabel("BLANK");
    opt6Label = new JLabel("BLANK");
    opt7Label = new JLabel("BLANK");
    opt8Label = new JLabel("BLANK");
    opt9Label = new JLabel("BLANK");
    JButton opt1 = new JButton("1");
    JButton opt2 = new JButton("2");
    JButton opt3 = new JButton("3");
    JButton opt4 = new JButton("4");
    JButton opt5 = new JButton("5");
    JButton opt6 = new JButton("6");
    JButton opt7 = new JButton("7");
    JButton opt8 = new JButton("8");
    JButton opt9 = new JButton("9");

    mainPanel.setLayout(null);
    mainPanel.add(inventory);
    mainPanel.add(options);
    mainPanel.add(planet);
    mainPanel.setOpaque(true);
    mainPanel.setBackground(Color.WHITE);

    inventory.setLayout(new BoxLayout(inventory, 1));
    inventory.add(inventTitle);
    inventory.setOpaque(true);
    inventory.setBackground(Color.RED);
    inventory.setBounds(0, 0, 360, 400);
    inventory.add(inventMoney);

I saw somewhere a way of setting it like a method.

Answers


You can use a for loop instead of writing the same statements multiple times.

JLabel optLabel[] = new JLabel[9];
JButton opt[] = new JButton[9];

for (int i = 0; i < 9 ; i++) {
   optLabel[i] = new JLabel ("BLANK");
   opt[i] =  new JButton (Integer.valueOf (i + 1).toString());
}

Need Your Help

java.lang.SecurityException: Provider gps requires ACCESS_FINE_LOCATION permission

java android google-maps

I try to develope an Android map application, utilizing the Google maps API.

How to send any Object to WCF service

c# wcf web-services

I have a WCF service and application. My application occasionally sends some text messages (string[]) to the WCF service, which then parses it.