mardi 24 février 2015

How to make a multicomponent Swing Class in Java



So I am trying to make multiple JLabels inside a JPanel then add it to a JFrame, I am using the construction to set the text of the JLabels (16 of them) but the text is not being set. Why is this? Thanks :)



package GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;

public class Window extends JFrame {
/**
* Create the frame.
*/
GridBagConstraints layout = new GridBagConstraints();

public Window()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
setLayout(new GridBagLayout());

InfoPane stats = new InfoPane();
layout.gridx = 0;
layout.gridy = 0;
layout.ipadx = 50;
add(stats.InfoPaneBox("John", "100", "UNSP", "Ukraine", "9\" Knife", "05", "£123", "18"), layout);

setVisible(true);
}
}


Here is the multi-component jpanel that i want to add to the JFrame. Also What is the best way to go around making customised components in Classes and then to add them to a main JFrame?



package GUI;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;



public class InfoPane
{

JPanel container = new JPanel();

JLabel one = new JLabel("Name:");
JLabel two = new JLabel("Health:");
JLabel three = new JLabel("Gang:");
JLabel four = new JLabel("Location:");
JLabel five = new JLabel("Weapon:");
JLabel six = new JLabel("Police Noriety:");
JLabel seven = new JLabel("Money:");
JLabel eight = new JLabel("Gang Noriety:");

JLabel nameJL = new JLabel();
JLabel healthJL = new JLabel();
JLabel gangJL = new JLabel();
JLabel locationJL = new JLabel();
JLabel weaponJL = new JLabel();
JLabel policeNorietyJL = new JLabel();
JLabel moneyJL = new JLabel();
JLabel gangNorietyJL = new JLabel();


public JPanel InfoPaneBox(String name, String health, String gang, String location, String weapon, String police, String money, String gangn)
{
nameJL.setText(name);
healthJL.setText(health);
gangJL.setText(gang);
locationJL.setText(location);
weaponJL.setText(weapon);
policeNorietyJL.setText(police);
moneyJL.setText(money);
gangNorietyJL.setText(gangn);

container.add(one);
container.add(two);
container.add(three);
container.add(four);
container.add(five);
container.add(six);
container.add(seven);
container.add(eight);
container.add(nameJL);
container.add(healthJL);
container.add(gangJL);
container.add(locationJL);
container.add(weaponJL);
container.add(policeNorietyJL);
container.add(moneyJL);
container.add(gangNorietyJL);
one.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredSoftBevelBorder(), "Stats", TitledBorder.LEFT, TitledBorder.TOP));
one.setPreferredSize(new Dimension(150,400));

return container;
}
}



Aucun commentaire:

Enregistrer un commentaire