【问题标题】:how do i align my JLabels with my JTextFields Horizontally?我如何将我的 JLabels 与我的 JTextFields 水平对齐?
【发布时间】:2015-10-30 03:18:50
【问题描述】:

我的面板无法将标签与文本字段水平对齐。我正在考虑网格布局,但我不确定。这是我目前拥有的代码。

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

这就是我的面板的样子

这就是我现在所拥有的。

【问题讨论】:

    标签: java swing layout layout-manager


    【解决方案1】:

    首先查看Laying Out Components Within a Container

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

    查看How to Use GridLayoutHow to Use GridBagLayout 了解更多详情

    【讨论】:

      【解决方案2】:

      我正在考虑一个网格布局

      听起来不错。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-07
        • 2018-04-10
        • 1970-01-01
        • 1970-01-01
        • 2018-08-11
        • 1970-01-01
        相关资源
        最近更新 更多