View Full Version : Require assistance with a contentPane problem please.

03-21-2012, 09:33 PM
Hi All,

I would appreciate it if someone could assist with a contentPane problem that I have. I have written a 2D game that works when I run it from my IDE (netneans). It will not work correctly when I run the Executable Jar File outside of my IDE. Basiclly if I call ‘f.pack();’ (f is the JFrame) everything works fine but when I run the game outside of the IDE the I get a transparent JFrame which I cannot do anything with. If I remove ‘f.pack();’ the game works outside of the IDE (likewise inside) but I have no contentPane which is holding: an inventorylabel – this is just a label, a JPanel – this shows Icon which the player collides with during the game to show what the player is holding and a JTextArea – this gives the player information such as “you need to speak to......”.
I have tried adding a BorderLayout to the contentPane which will not allow me to add the three different components needed or change the text / picture etc when a collision occurs. I also need the three components in a row to the EAST and it will not allos me to set the horizontal axis (but will allow the vertical)
I’ve tried calling validate(), show(), and various other methods from the API but to no avail.

The Board class is just a class which looks for collision detections from other classes and updates the JPanel and JtextArea when a collision occurs. The Frame class creates a new instance of Board and holds my main method which calls run() from my Board class. As the Board class is 1600 lines long I have just inserted the area of code where I think my problem may be, let me know if you need more and I will post it. Apologies if I have my [CODE] tags wrong.
//Other Instance Variables
JFrame f = new JFrame();
JTextArea textArea;
JPanel inventoryPanel;
JLabel inventoryLabel;
Container contentPane;
public Board()
//Other variables inisalised in the constructor
inventoryPanel = new JPanel();
inventoryLabel = new JLabel("Inventory");
textArea = new JTextArea(text[0]);
contentPane = f.getContentPane();
f.setPreferredSize(new Dimension(800, 550));
inventoryLabel.setBounds(610, 20, 170, 40);
inventoryPanel.setBounds(610, 80, 170, 200);
textArea.setBounds(610, 300, 170, 225);

public void paint(Graphics g)
//Paints other objects from other classes
public void run()
f.add(new Board());
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
f.setSize(width/2, height/2);
f.setSize(new Dimension(800, 590));//600, 585 for programming otherwise
f.setLocationRelativeTo(null);// Centre JFRAME

03-31-2012, 10:57 AM