Search code examples
javaswingjpaneljtextfield

size and location of Text Field on the Panel


package testerapplication;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.swing.*;

public   class  TesterApplication {
    static JPanel CenterPanel;
    static JPanel UpperPanel;
    static JPanel CreationPanel;
    static JPanel UpCreationPanel;
    static JPanel DownCreationPanel;
    static JPanel MenuPanel;
    static JFrame frame;
    JTextField RoadNameTextField;
    JTextField BusNameTextField;
    JTextField StopNameTextField;
    TesterApplication(){
        JFrame frame=new JFrame("Bus Map");

        CenterPanel = new JPanel();
        CenterPanel.setBackground(Color.black);
        UpperPanel=new JPanel();
        UpperPanel.setLayout(new BoxLayout(UpperPanel,BoxLayout.Y_AXIS));

        MenuPanel=new JPanel();
        MenuPanel.setBackground(Color.red);
        CreationPanel=new JPanel();
        CreationPanel.setBackground(Color.blue);

        UpCreationPanel=new JPanel();
        UpCreationPanel.setBackground(Color.blue);
        DownCreationPanel=new JPanel();
        DownCreationPanel.setBackground(Color.green);

        //build text fields and put them into panel
        BusNameTextField= new JTextField("Bus ",10);
        StopNameTextField= new JTextField("Stope ",10);
        RoadNameTextField= new JTextField("Street ",10);

        UpCreationPanel.add(BusNameTextField);
        UpCreationPanel.setLayout(new BoxLayout(UpCreationPanel,BoxLayout.X_AXIS));

        DownCreationPanel.add(BorderLayout.WEST,StopNameTextField);
        DownCreationPanel.add( RoadNameTextField);

        CreationPanel.setLayout(new BoxLayout(CreationPanel,BoxLayout.Y_AXIS));
        CreationPanel.add(UpCreationPanel);
        CreationPanel.add(DownCreationPanel);

        UpperPanel.add(MenuPanel);
        UpperPanel.add(CreationPanel);

        frame.getContentPane().add(BorderLayout.NORTH, UpperPanel);
        frame.getContentPane().add(BorderLayout.CENTER,CenterPanel);
        frame.setSize(600, 480);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    public static void main(String[] args){
        new TesterApplication();
    }

}

enter image description here

[IMG]http://i60.tinypic.com/2yo3n21.png[/IMG]

the question is, or better are. Why BusNameTextField occupation all the window, why StopNameTextField don't go to the WEST-end of the window, and is it normal that there is no Blue UpCreationPanel, meanwhile Green DownCreationPanel is present.

My purpose is to get three small TextField all placed on the left, "Stope" and "Street" go one after another


Solution

  • By default a JPanel uses a FlowLayout.

    If you want the components displayed vertically then you can use a vertical BoxLayout or a GridBagLayout.

    And fix your code to follow the Java naming conventions. Variable name do NOT start with an upper case character. "CenterPanel" should be "centerPanel" etc..

    Finally don't use static variables. If you are using static variable for panels of components, the designe of your program is wrong.

    Read the section from the Swing tutorial on How to Use Layout Managers for more information about layouts and better examples of how to structure a program, including creating the GUI on the Event Dispatch Thread.