Problem with setLocation
Hi,
I'm having some problems with the setLocation method (well I think that's where the problem is anyway). In my other JPanels for the GUI I have used BorderLayout and they work fine, however, this JPanel is not being displayed:
JPanel left = new JPanel();
left.setLayout(null);
left.setBackground(Color.blue);
ImageIcon Board = new ImageIcon("C:/Documents and Settings/Board.jpg");
Board_Logo = new JLabel(Board);
JLabel Title = new JLabel("Title");
Counter = new ImageIcon("C:/Documents and Settings/Counter.jpg");
white = new JLabel(Counter);
Board_Logo.setLocation(5,95);
white.setLocation(10,10);
Title.setLocation(297,482);
left.add(Board_Logo);
left.add(white);
left.add(Title);
contentPane.add(left, BorderLayout.WEST);
Can anyone help me please?
> Can anyone help me please?
Post "compileable and executable demo code" that shows what you are doing and what the problem is. And don't use images in your labels, just use text for the demo. We are not going to sit here guessing what you may or may not be doing wrong.
And don't forget to use the "Code Formatting Tags".
ok here is my code:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.IOException;
class GUIPROB extends JFrame {
GUIPROB(){
super("GUI");
MainScreen();
setSize(890,500);
setVisible(true);
}
protected void MainScreen() {
setVisible(true);
setSize(870,500);
Container contentPane = getContentPane();
JPanel left = new JPanel();
left.setLayout(null);
//Image here
//ImageIcon image = new ImageIcon("Image");
JLabel ImageLabel = new JLabel("Image");
JLabel Title = new JLabel("Text");
//Image here
//Image2 = new ImageIcon("Image2");
JLabel ImageLabel2 = new JLabel("Image2");
ImageLabel.setBounds(5,95,460, 375);
ImageLabel2.setBounds(10,10, 30, 40);
Title.setBounds(300,400, 200, 10);
left.add(ImageLabel);
left.add(ImageLabel2);
left.add(Title);
left.setSize(504,413);
contentPane.add(left, BorderLayout.WEST);
JPanel middlePanel = new JPanel();
middlePanel.setLayout(new BorderLayout());
JLabel name = new JLabel("This panel works");
middlePanel.add(name, BorderLayout.SOUTH);
contentPane.add(middlePanel, BorderLayout.CENTER);
}
}
And the class that calls it:
class GUIAPP {
public static void main(String[] args) {
GUIPROB newGUI = new GUIPROB();
newGUI.MainScreen();
}
}
the problem is you need setSize for the components of a null layout
when adding 'left' panel, it is not being added to a null layout, so the layout
manager needs its 'preferredSize'
change this line
left.setSize(504,413);
to this
left.setPreferredSize(new Dimension(504,413));