-
Swing layouts
Hey yall,
Been trying to understand layouts for a while now,started with GridbagLayout and the only way I could get what I want is through sheer luck.
Now Im making a gui for a chat application Im working on and using BorderLayout to place components onto a frame and I can't seem to get what I want.
Can someone help me out please.
this is the panel Im adding onto the frame
Code:
public void createConfig(){
configPanel = new JPanel();
portNumber = new JTextField(5);
friend = new JTextField(5);
ipAddresText = new JTextField(5);
connect = new JButton("Connect");
JLabel portNumberLabel = new JLabel("Enter port number");
JLabel friendLabel = new JLabel("Enter name");
JLabel ipAddress = new JLabel("Enter ip address");
JLabel onlineFriends = new JLabel("Friends");
configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.Y_AXIS));
configPanel.setSize(500,500);
configPanel.setBackground(Color.LIGHT_GRAY);
//configPanel.add(portNumberLabel);
//configPanel.add(portNumber);
//configPanel.add(friendLabel);
//configPanel.add(friend);
//configPanel.add(ipAddress);
//configPanel.add(ipAddresText);
Dimension dimension = new Dimension(50,50);
friendsList.setPreferredSize(dimension);
configPanel.add(onlineFriends);
configPanel.add(friendsList);
}
this is the frame Im adding the panel onto
Code:
public void addToFrame() {
createConfig();
Container guiFrame = this.getContentPane();
guiFrame.setLayout(new BorderLayout());
guiFrame.add("Center", new Client(loginame));
guiFrame.add("North", new JLabel("welcome "+loginame));
guiFrame.add("East",configPanel);
this.setSize(300,300);
this.setVisible(true);
}
The client constructor creates an ok chat window but its the panel (createConfig()) that Im worried about.
Thanx in advance...
-
Have you gone through the tutorial?
Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Also
-- a contentPane has a default BorderLayout.
-- the add(...) overload you're using is obsolete as of version 1.1. Get with the times!
Container (Java Platform SE 6)
-- Don't use setSize(...) and avoid using setPreferredSize(...) in client code. Let the layout managers do their job.
db
-
thanx Ill take a look at it...