简体   繁体   English

我如何将我的JLabel与我的JTextFields水平对齐?

[英]how do i align my JLabels with my JTextFields Horizontally?

i am having trouble with my panel specifically aligning my labels with my text fields horizontally. 我的面板出现问题,特别是将标签与文本字段水平对齐。 i am thinking a grid layout but i am not sure. 我正在考虑网格布局,但我不确定。 this is the code i currently have. 这是我目前拥有的代码。

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 is how my panel should look like 这是我的面板的外观

这就是我想要的

and this is what i have right now. 这就是我现在所拥有的。

这就是我所拥有的

Start by having a look at Laying Out Components Within a Container . 首先看一下容器中的组件布局

JPanel by default uses a FlowLayout , which isn't really going to do what you want, instead, I recommend either a GridLayout or, preferably, a GridBagLayout JPanel默认使用FlowLayout ,它实际上并不会做您想要的事情,相反,我建议使用GridLayout或最好是GridBagLayout

在此处输入图片说明

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);

Have a look at How to Use GridLayout and How to Use GridBagLayout for more details 看看如何使用GridLayout如何使用GridBagLayout了解更多详细信息

i am thinking a grid layout 我在想网格布局

Sound good to me. 对我来说听起来不错。

Read the section from the Swing tutorial on How to Use GridLayout for a working example. 阅读Swing教程中有关如何使用GridLayout的部分 ,以获取工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM