tags:

views:

39

answers:

2

Hey there, everyone.

I'm currently enrolled in a Client/Server Programming class (i.e. Java 2) in college. For our first lab assignment, we're being asked to create a simple GUI to emulate a basic instant messaging chat client. This client won't have any actual functionality, just a simple window. This window, however has to look similar to a window developed by my professor for a Visual Basic class. Now, I KNOW that with my basic knowledge, I'll never be able to get my code to look exactly like the example, but I'd like to get as close as possible.

My real question is: what LayoutManager (or combination thereof) should I be using to get as close to this as possible? I've only put in the first couple components onto my lab project, but I'm curious if I'm doing this right... Or if I'm using the smartest approach. Thanks for any and all help!

EDIT: OH, before I forget, here's the sample code that I already have now. Pick it apart at your leisure! Any and all criticisms are welcome, both for my code, and the approach I've tried!

import java.awt.*;
import javax.swing.*;

public class Chat {
    public static void main(String[] args) {
        /*
         * The main() method is what creates the Frame for use by the user. It set's a default size to 300x600,
         * which is a pretty standard size for a chat client. It also adds a title to the Frame, which adds a
         * cool element there!
         */
        ChatClient GUI = new ChatClient();
        GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GUI.setSize(300, 600);
        GUI.setTitle("Lab 1: Chat");
        GUI.setVisible(true);
        GUI.start();
    }
}

class ChatClient extends JFrame {
    public void start() {
        setLayout(new BorderLayout());

        /*
         * Divide the main Frame into three separate Panels. The North Panel holds the username, server, and port
         * information, along with the associated Buttons.
         */

        JPanel northPanelGroup = new JPanel(new GridLayout(3, 3, 1, 1));
        JPanel centerPanelGroup = new JPanel(new GridLayout(1, 1, 1, 1));
        JPanel southPanelGroup = new JPanel(new GridLayout(1, 3, 1, 1));

        /*
         * Obviously, add the Panel Groups into the main Frame
         */
        add(BorderLayout.NORTH, northPanelGroup);
        add(BorderLayout.CENTER, centerPanelGroup);
        add(BorderLayout.SOUTH, southPanelGroup);

        /*
         * Begin adding the username, server, and port information into the North Panel
         */
        JPanel usernamePanel = new JPanel();
        usernamePanel.add(new JLabel("Username"));
        usernamePanel.add(new JTextField(10));
        northPanelGroup.add(usernamePanel);

        JPanel serverPanel = new JPanel();
        serverPanel.add(new JLabel("Server"));
        serverPanel.add(new JTextField(10));
        northPanelGroup.add(serverPanel);

        JPanel portPanel = new JPanel();
        portPanel.add(new JLabel("Port"));
        portPanel.add(new JTextField(5));
        northPanelGroup.add(portPanel);
    }
}
+1  A: 

You should use Mattise in Netbeans IDE if you're having trouble with GUI creation :) Honestly though, if you want to do it programatically, you should have a look at MigLayout ( http://www.miglayout.com/ ) which is the easiest and most flexible layout manager going.

http://blogs.sun.com/roumen/resource/matisse6.PNG

Chris Dennett
MigLayout look cool. But honestly, I already happy with the GUI code generated by Netbeans IDE. Hence, will not try that out :)
Yan Cheng CHEOK
Hmm, perhaps. I do like the look of MiGLayout, however since I have to give the source code to my professor so he can run it and grade my work, I'm not sure that it will work. However, I'm definitely going to look into using this more often for other Java projects I have in mind!About NetBeans, though. This is pretty good as a drag-and-drop style GUI editor, however it adds a LOT of extra source code that I really don't want/need. Any way to trim that down any? I just need simple Swing code...
1n73rn37_j3d1
Maybe you can use the UI editor and then look at the source code to get the gist of what is going on -- copy and take what you need and plonk it in another project :)
Chris Dennett
+1  A: 

I'm a big fan of SpringLayout. It requires a bit of thinking ahead and you end up writing quite a bit of code, but it offers and unrivalled level of control.

With SpringLayout you define every component's position as an x-y offset from another component (eg the left side of component B is 5 pixels from the right side of component A). With careful planning you can move stuff around easily and get your UI pixel perfect.

Qwerky
I'll have to check that one out for future projects in the class. I read through the API on it, and it seems to be pretty much what I need! I'll also have to checkout MiGLayout, but I guess I'll be using that mostly for my own side projects.
1n73rn37_j3d1