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, centerPanelGro开发者_如何学Goup);
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);
}
}
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.
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.
Do you need your component to be resizable? If not, just use null layout and specify coordinates and size of every component, either programmatically, or in a WYSIWYG editor via and IDE such as NetBeans or Eclipse with WindowsBuilder.
(Do make sure null layout is allowed by the requirements though, and if not, then you may want to find out precisely HOW every control is to resize when the main component is resized, and then MiGLayout is probably your best bet, by far. I don't think I've ever needed any older layout ever since MiG appeared! )
精彩评论