如何将JLabel与JTextFields水平对齐?

时间:2015-10-30 03:18:50

标签: java swing layout layout-manager

我在使用我的面板时遇到问题,特别是水平地将我的标签与文本字段对齐。我正在考虑网格布局,但我不确定。这是我目前的代码。

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

public class PropertyGUITest
{
    public static void main(String[] args)
    {
        PropertyGUI obj = new PropertyGUI();
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        obj.setSize(400,300);
        obj.setVisible(true);
        obj.setLocation(100,50);
    }
}

class PropertyGUI extends JFrame
{
    private int countFrames = 0;
    public PropertyGUI()
    {
        super("Property GUI");

        JMenuBar bar = new JMenuBar();
        setJMenuBar(bar);

        JMenu readMenu = new JMenu("Read Files");
        bar.add(readMenu);

        JMenu addMenu = new JMenu("Add");
        bar.add(addMenu);

        JMenuItem newFrame1=new JMenuItem("Add Owner");
        addMenu.add(newFrame1);

        JMenuItem newFrame2=new JMenuItem("Add Residential Property");
        addMenu.add(newFrame2);

        JMenuItem newFrame3=new JMenuItem("Add Commercial Property");
        addMenu.add(newFrame3);

        JMenu finishMenu = new JMenu("Finish");
        bar.add(finishMenu);

        JDesktopPane theDesktop = new JDesktopPane();
        add(theDesktop);

        JMenuItem writeItem = new JMenuItem("Write Owners");
        finishMenu.add(writeItem);

        JMenuItem readpItem = new JMenuItem("Read Properties");
        readMenu.add(readpItem);

        JMenuItem readoItem = new JMenuItem("Read Owners");
        readMenu.add(readoItem);

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent event)
                {
                    //System.exit(0);
                    dispose();
                }
            }
        );
        finishMenu.add(exitItem);

        newFrame1.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    countFrames++;
                    JInternalFrame jf = new JInternalFrame("Add Owner",true,true,true,true);
                    theDesktop.add(jf);
                    jf.setVisible(true);
                    jf.pack();
                    jf.setSize(400,200);
                    jf.setLocation(countFrames*10,countFrames*20);

                    CustomPanel panel1 = new CustomPanel();
                    jf.add(panel1);

                    panel1.setLayout(new GridLayout(5,2));
                }
            }
        );

        newFrame2.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    countFrames++;
                    JInternalFrame jf = new JInternalFrame("Add Residential Property",true,true,true,true);
                    theDesktop.add(jf);
                    jf.setVisible(true);
                    jf.pack();
                    jf.setSize(400,200);
                    jf.setLocation(countFrames*10,countFrames*20);

                    CustomPanel panel1 = new CustomPanel();
                    jf.add(panel1);

                    panel1.setLayout(new GridLayout(5,2));
                }
            }
        );

        newFrame3.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    countFrames++;
                    JInternalFrame jf = new JInternalFrame("Add Commercial Property",true,true,true,true);
                    theDesktop.add(jf);
                    jf.setVisible(true);
                    jf.pack();
                    jf.setSize(400,200);
                    jf.setLocation(countFrames*10,countFrames*20);

                    CustomPanel panel1 = new CustomPanel();
                    jf.add(panel1);

                    panel1.setLayout(new GridLayout(5,2));

                }
            }
        );
    }

    class CustomPanel extends JPanel
    {
        JTextField tf1;
        JTextField tf2;
        JTextField tf3;
        JTextField tf4;
        JTextField tf5;
        JLabel label1;
        JLabel label2;
        JLabel label3;
        JLabel label4;
        JLabel label5;
        JLabel label6;
        JButton button1;


        public CustomPanel()
        {


            label1 = new JLabel("Name");
            add(label1);
            tf1 = new JTextField();
            add(tf1);

            label2 = new JLabel("Street");
            add(label2);
            tf2 = new JTextField();
            add(tf2);

            label3 = new JLabel("City");
            add(label3);
            tf3 = new JTextField();
            add(tf3);

            label4 = new JLabel("State");
            add(label4);
            tf4 = new JTextField();
            add(tf4);

            label5 = new JLabel("Zip");
            add(label5);
            tf5 = new JTextField(); 
            add(tf5);

            label6 = new JLabel("Submit when done");
            add(label6);

            button1 = new JButton("Submit");
            add(button1);
        }
    }
}

这是我的面板应该是什么样子

This what I want

这就是我现在所拥有的。

This what I have

2 个答案:

答案 0 :(得分:1)

首先看一下Laying Out Components Within a Container

默认情况下,

JPanel使用的是FlowLayout,它不会真正做你想做的事情,相反,我推荐GridLayout,或者最好是GridBagLayout 1}}

enter image description here

setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(2, 2, 2, 2);

add(new JLabel("Name"), gbc);
gbc.gridy++;
add(new JLabel("Street"), gbc);
gbc.gridy++;
add(new JLabel("City"), gbc);
gbc.gridy++;
add(new JLabel("State"), gbc);
gbc.gridy++;
add(new JLabel("Zip"), gbc);
gbc.gridy++;
add(new JLabel("Submit when done"), gbc);

JTextField[] fields = new JTextField[5];
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
add((fields[0] = new JTextField(10)), gbc);
gbc.gridy++;
add((fields[1] = new JTextField(10)), gbc);
gbc.gridy++;
add((fields[2] = new JTextField(10)), gbc);
gbc.gridy++;
add((fields[3] = new JTextField(10)), gbc);
gbc.gridy++;
add((fields[4] = new JTextField(10)), gbc);
gbc.gridy++;
JButton btn = new JButton("Submit");
add(btn, gbc);

有关详细信息,请查看How to Use GridLayoutHow to Use GridBagLayout

答案 1 :(得分:1)

  

我正在考虑网格布局

听起来不错。

阅读How to Use GridLayout上的Swing教程中的部分以获取工作示例。