views:

55

answers:

2

I have a following code :

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Main extends JPanel implements ActionListener {

    protected JTextArea output;
    protected JTextField fileInput, dateInput;
    protected JPanel topBar, bottomBar, centerPanel;
    protected JButton load, sortName, sortNSD, sortDate, show;

    public Main() {
        super(new BorderLayout());
        topBar = new JPanel(new FlowLayout());
        centerPanel = new JPanel();
        bottomBar = new JPanel(new FlowLayout());
        this.add(topBar, BorderLayout.NORTH);
        this.add(centerPanel, BorderLayout.CENTER);
        this.add(bottomBar, BorderLayout.SOUTH);

        load = new JButton("Load");
        sortName = new JButton("Sort by Name");
        sortNSD = new JButton("Sort by N,S,D");
        sortDate = new JButton("Sort by Date");
        show = new JButton("Show");

        output = new JTextArea(5, 40);
        fileInput = new JTextField(25);
        dateInput = new JTextField(8);
        output.setEditable(false);
        JScrollPane scroller = new JScrollPane(output);
        Label outputLabel = new Label("Output:");

        topBar.add(sortName);
        topBar.add(sortNSD);
        topBar.add(sortDate);
        topBar.add(dateInput);
        topBar.add(show);

        centerPanel.add(scroller);

        bottomBar.add(fileInput);
        bottomBar.add(load);
    }

    public void actionPerformed(ActionEvent evt) {
    }

    private static void createWindow() {
        JFrame frame = new JFrame("Zd2_1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Main());
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createWindow();
            }
        });
    }
}

which gives me this output : http://img202.imageshack.us/img202/7359/92250063.gif . How can I improve my layout to :
- look more pro (introduce borders around buttons ?)
- keep constant size
- how do you add the label ?
I'm not too good at layouts :/


EDIT

Found hov to bleck the size :

    frame.setResizable(false);
    frame.setSize(330, 480);
+2  A: 

One possibility is to use a titled border instead of a label:

// Label outputLabel = new Label("Output:");
centerPanel.setBorder(BorderFactory.createTitledBorder("Output"));

alt text

Note that the default layout of JPanel is FlowLaytout, so these are sufficient:

topBar = new JPanel();
centerPanel = new JPanel();
bottomBar = new JPanel();

Addendum: As an alternative to a fixed size, you can allow the center panel to resize with the window by using GridLayout:

centerPanel = new JPanel(new GridLayout());
trashgod
+1  A: 

To add the label, you can use the .add(outputLabel) method wherever you want to insert the label. You may also want to explore the "Look And Feel" documentation if you want to make it look more like a native operating system application and less like a Java application. Here are two webpages that might help:

http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

D.R.
Another resource would be: http://download.oracle.com/javase/tutorial/uiswing/components/frame.html#setDefaultLookAndFeelDecorated
D.R.