使用swing组件JAVA的GUI布局

使用swing组件JAVA的GUI布局,java,swing,user-interface,Java,Swing,User Interface,这就是我想要实现的目标 我使用了网格布局,这就是我所做的 我的代码是这样的(如果需要,不是完整的代码可以提供) 如果您能提供任何帮助,我们将不胜感激 GridLayout就是这样做的,它在网格中布局组件,其中每个单元格根据需求(即宽度/列和高度/行)占可用空间的百分比 看看基本布局管理器的示例以及它们的功能 我建议你去看看。它是默认库中最灵活(也是最复杂)的布局管理器 例如 import java.awt.BorderLayout; import java.awt.Dimension; im

这就是我想要实现的目标

我使用了网格布局,这就是我所做的

我的代码是这样的(如果需要,不是完整的代码可以提供)


如果您能提供任何帮助,我们将不胜感激

GridLayout
就是这样做的,它在网格中布局组件,其中每个单元格根据需求(即宽度/列和高度/行)占可用空间的百分比

看看基本布局管理器的示例以及它们的功能

我建议你去看看。它是默认库中最灵活(也是最复杂)的布局管理器

例如

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout31 {

    public static void main(String[] args) {
        new TestLayout31();
    }

    public TestLayout31() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            JLabel lblRes = new JLabel("Resident Name");
            JLabel lblRoomNo = new JLabel("RoomNo");
            JLabel lblAge = new JLabel("Age");
            JLabel lblGender = new JLabel("Gender");
            JLabel lblCare = new JLabel("Care level");

            JTextField fldRes = new JTextField("john smith", 20);
            JTextField fldRoomNo = new JTextField(10);
            JComboBox cmbAge = new JComboBox(new Object[]{51});
            JComboBox cmbGener = new JComboBox(new Object[]{"M", "F"});
            JComboBox cmbCare = new JComboBox(new Object[]{"Low"});

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

            gbc.gridx++;
            gbc.gridwidth = 4;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(fldRes, gbc);

            gbc.gridx = 7;
            gbc.gridwidth = 1;
            gbc.fill = GridBagConstraints.NONE;
            add(lblRoomNo, gbc);

            gbc.gridx++;
            add(fldRoomNo, gbc);

            gbc.gridy++;
            gbc.gridx = 1;
            add(lblAge, gbc);
            gbc.gridx++;
            add(cmbAge, gbc);
            gbc.gridx++;
            add(lblGender, gbc);
            gbc.gridx++;
            add(cmbGener, gbc);
            gbc.gridx++;
            gbc.gridwidth = 2;
            add(lblCare, gbc);
            gbc.gridx += 2;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(cmbCare, gbc);

        }
    }

}

复合布局示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout31 {

    public static void main(String[] args) {
        new TestLayout31();
    }

    public TestLayout31() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            JLabel lblRes = new JLabel("Resident Name");
            JLabel lblRoomNo = new JLabel("RoomNo");
            JLabel lblAge = new JLabel("Age");
            JLabel lblGender = new JLabel("Gender");
            JLabel lblCare = new JLabel("Care level");

            JTextField fldRes = new JTextField("john smith", 20);
            JTextField fldRoomNo = new JTextField(10);
            JComboBox cmbAge = new JComboBox(new Object[]{51});
            JComboBox cmbGener = new JComboBox(new Object[]{"M", "F"});
            JComboBox cmbCare = new JComboBox(new Object[]{"Low"});

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

            gbc.gridx++;
            gbc.gridwidth = 4;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(fldRes, gbc);

            gbc.gridx = 7;
            gbc.gridwidth = 1;
            gbc.fill = GridBagConstraints.NONE;
            add(lblRoomNo, gbc);

            gbc.gridx++;
            add(fldRoomNo, gbc);

            gbc.gridy++;
            gbc.gridx = 1;
            add(lblAge, gbc);
            gbc.gridx++;
            add(cmbAge, gbc);
            gbc.gridx++;
            add(lblGender, gbc);
            gbc.gridx++;
            add(cmbGener, gbc);
            gbc.gridx++;
            gbc.gridwidth = 2;
            add(lblCare, gbc);
            gbc.gridx += 2;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(cmbCare, gbc);

        }
    }

}
另一种选择是使用复合布局。也就是说,您将UI的每个部分分离到单独的容器中,集中于它们各自的布局需求

例如,您有两行字段,每行字段之间没有真正的关联,因此,您不必试图找出如何使字段对齐,而可以分别关注每行

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout31 {

    public static void main(String[] args) {
        new TestLayout31();
    }

    public TestLayout31() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            JPanel topPane = new JPanel(new GridBagLayout());

            JLabel lblRes = new JLabel("Resident Name");
            JLabel lblRoomNo = new JLabel("RoomNo");
            JLabel lblAge = new JLabel("Age");
            JLabel lblGender = new JLabel("Gender");
            JLabel lblCare = new JLabel("Care level");

            JTextField fldRes = new JTextField("john smith", 20);
            JTextField fldRoomNo = new JTextField(10);
            JComboBox cmbAge = new JComboBox(new Object[]{51});
            JComboBox cmbGener = new JComboBox(new Object[]{"M", "F"});
            JComboBox cmbCare = new JComboBox(new Object[]{"Low"});

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.insets = new Insets(1, 1, 1, 1);
            topPane.add(lblRes, gbc);

            gbc.gridx++;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            topPane.add(fldRes, gbc);

            gbc.gridx++;
            topPane.add(lblRoomNo, gbc);

            gbc.gridx++;
            topPane.add(fldRoomNo, gbc);

            JPanel bottomPane = new JPanel(new GridBagLayout());

            gbc.gridx = 0;
            bottomPane.add(lblAge, gbc);
            gbc.gridx++;
            bottomPane.add(cmbAge, gbc);
            gbc.gridx++;
            bottomPane.add(lblGender, gbc);
            gbc.gridx++;
            bottomPane.add(cmbGener, gbc);
            gbc.gridx++;
            bottomPane.add(lblCare, gbc);
            gbc.gridx++;
            bottomPane.add(cmbCare, gbc);

            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(topPane, gbc);
            gbc.gridy++;
            add(bottomPane, gbc);

        }
    }        
}

这将使以后修改UI变得更容易,如果您必须…

JFrame=new JFrame();
JFrame frame = new JFrame();
JPanel contentPane = new JPanel();
JPanel northPane   = new JPanel();
JPanel centerPane  = new JPanel();
JPanel southPane   = new JPanel();
contentPane.setLayout(new BorderLayout());
northPane.setLayout(  new GridLayout(1, 6));
southPane.setLayout(  new GridLayout(1, 7));
contentPane.add(northPane,  BorderLayout.NORTH );
contentPane.add(centerPane, BorderLayout.CENTER);
contentPane.add(southPane,  BorderLayout.SOUTH );
frame.setContentPane(contentPane);
JLabel            residentNameLabel = new JLabel("Resident name ");
JTextField        residentNameText  = new JTextField();
JLabel            roomNoLabel       = new JLabel("RoomNo ");
JTextField        roomNoText        = new JTextField();
JLabel            emptyLabel0       = new JLabel("   ");
JLabel            emptyLabel1       = new JLabel("   ");
JLabel            emptyLabel2       = new JLabel("   ");
JLabel            emptyLabel3       = new JLabel("   ");
JLabel            ageLabel          = new JLabel("Age ");
JComboBox<String> ageComboBox       = new JComboBox<String>();
ageComboBox.addItem("50");
ageComboBox.addItem("51");
ageComboBox.addItem("52");
ageComboBox.addItem("53");
ageComboBox.addItem("54");
ageComboBox.addItem("55");
JLabel            genderLabel       = new JLabel("Gender ");
JComboBox<String> genderComboBox    = new JComboBox<String>();
genderComboBox.addItem("M");
genderComboBox.addItem("F");
JLabel            careLevelLabel    = new JLabel("Care Level ");
JComboBox<String> careLevelComboBox = new JComboBox<String>();
genderComboBox.addItem("low");;
genderComboBox.addItem("medium");
genderComboBox.addItem("high");
residentNameLabel.setHorizontalAlignment(JLabel.RIGHT);
roomNoLabel.setHorizontalAlignment(JLabel.RIGHT);
ageLabel.setHorizontalAlignment(JLabel.RIGHT);
genderLabel.setHorizontalAlignment(JLabel.RIGHT);
careLevelLabel.setHorizontalAlignment(JLabel.RIGHT);
northPane.add(emptyLabel0      );
northPane.add(residentNameLabel);
northPane.add(residentNameText );
northPane.add(roomNoLabel      );
northPane.add(roomNoText       );
northPane.add(emptyLabel1      );
centerPane.add(emptyLabel2     );
southPane.add(ageLabel         );
southPane.add(ageComboBox      );
southPane.add(genderLabel      );
southPane.add(genderComboBox   );
southPane.add(careLevelLabel   );
southPane.add(careLevelComboBox);
southPane.add(emptyLabel3      );
contentPane.setBorder(BorderFactory.createTitledBorder("Personal Data"));
frame.addWindowListener(new WindowAdapter() {
@Override
    public void windowClosing(WindowEvent evt) {
        System.exit(0);
    }
});
frame.setVisible(true);
frame.pack();
JPanel contentPane=新的JPanel(); JPanel northPane=新的JPanel(); JPanel centerPane=新的JPanel(); JPanel southPane=新的JPanel(); setLayout(新的BorderLayout()); 设置布局(新的网格布局(1,6)); setLayout(新的GridLayout(1,7)); contentPane.add(northPane,BorderLayout.NORTH); 添加(centerPane、BorderLayout.CENTER); 添加(southPane,BorderLayout.SOUTH); frame.setContentPane(contentPane); JLabel residentNameLabel=新的JLabel(“居民姓名”); JTextField residentNameText=新的JTextField(); JLabel roomNoLabel=新JLabel(“RoomNo”); JTextField roomNoText=新的JTextField(); JLabel emptyLabel0=新JLabel(“”); JLabel emptylabel 1=新JLabel(“”); JLabel emptyLabel2=新JLabel(“”); JLabel emptyLabel3=新JLabel(“”); JLabel ageLabel=新JLabel(“年龄”); JComboBox ageComboBox=新JComboBox(); ageComboBox.addItem(“50”); ageComboBox.addItem(“51”); ageComboBox.addItem(“52”); ageComboBox.addItem(“53”); ageComboBox.addItem(“54”); ageComboBox.addItem(“55”); JLabel genderLabel=新JLabel(“性别”); JComboBox genderComboBox=新JComboBox(); 性别调查箱。附加项目(“M”); 性别调查箱。附加项目(“F”); JLabel careLevelLabel=新的JLabel(“护理级别”); JComboBox careLevelComboBox=新的JComboBox(); genderComboBox.addItem(“低”);; genderComboBox.addItem(“中等”); genderComboBox.addItem(“高”); residentNameLabel.setHorizontalAlignment(JLabel.RIGHT); roomNoLabel.setHorizontalAlignment(JLabel.RIGHT); ageLabel.setHorizontalAlignment(JLabel.RIGHT); genderLabel.setHorizontalAlignment(JLabel.RIGHT); careLevelLabel.setHorizontalAlignment(JLabel.RIGHT); 添加(0); 添加(residentNameLabel); 添加(residentNameText); 添加(roomNoLabel); 添加(roomNoText); 添加(空标签1); 中心窗格。添加(清空标签2); southPane.add(ageLabel); 添加(ageComboBox); 添加(性别标签); southPane.add(genderComboBox); 添加(careLevelLabel); 添加(careLevelComboBox); 添加(空标签3); contentPane.setboorder(BorderFactory.createTitledBorder(“个人数据”)); frame.addWindowListener(新的WindowAdapter(){ @凌驾 公共无效窗口关闭(WindowEvent evt){ 系统出口(0); } }); frame.setVisible(true); frame.pack();
对于设计静态GUI元素,尤其是表单,您会发现使用表单设计器直观地开发用户界面要容易得多。如果您可以使用此选项,我将使用Scene Builder 1.1创建FXML文件,然后将其加载到Java FX 2.2中,为GUI表单生成场景对象图。使用Scene Builder极大地加快了表单的开发速度,并将几乎所有的GUI创建代码从应用程序中取出,并将其移动到一个更易于维护的XML文件中。@scottb 1-问题的哪一部分让您认为它与Java FX有关?2-依我看,在依赖工具之前,应该学会手工编写UI代码。它提供了对布局管理器如何工作以及如何相互协作的更好理解。是的,我每天都使用表单编辑器,我也花了很多时间手工创建UI。@MadProgrammer:别生气,读到上面写着“如果您可以使用此选项”的部分。大多数问题都有不止一个解决方案,建议替代方法是有效的,所以。。。即使备选方案是对你个人世界观的冒犯,你也应该这样做。@scottb如果被冒犯了,很抱歉,我很好奇,当问题是基于Swing的时候,你为什么会建议使用JavaFX,考虑到Eclipse和NetBeans都提供了基于Swing的表单设计器。感谢您,每个布局都有特定的特性,虽然我倾向于大量使用
GridBagLayout
,但不要低估其他布局,因为它们可能会用更少的代码产生相同的预期结果。例如,在第二个示例中,我可以使用配置为单列和两行的
GridLayout
来布局顶部和底部面板。。。