0

我正在尝试制作 GUI,但无法正确设置布局

4

3 回答 3

2

这可以使用由 a 组成的复合或嵌套布局来完成BorderLayout,其中 aFlowLayoutPAGE_START约束中,aGridLayout用于约束中的两个文本区域CENTER

像这样的东西:

ToolBarAnd2AreasLayout

OTOH,您可以换掉FlowLayoutfor a JToolBar(看起来更好)和GridLayoutfor a JSplitPane(更有用,因为可以将窗格设置为用户当时需要的任何大小)。


鉴于现在问题中的示例图片(..paints 一千个单词),由 4 行标签、文本字段、按钮、按钮组成的顶部区域似乎最好在GroupLayout. 或者 3 个GridLayout实例(标签、字段和按钮各一个),LINE_STARTCENTERLINE_ENDBorderLayout

下面是后者的一个例子:

在此处输入图像描述

import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class ToolBarAnd2AreasLayout {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new TitledBorder("BorderLayout()"));

                JPanel controls = new JPanel(new BorderLayout(4,4));
                controls.setBorder(new TitledBorder("BorderLayout(4,4)"));

                JPanel labels = new JPanel(new GridLayout(0,1));
                labels.setBorder(new TitledBorder("GridLayout(0,1)"));
                controls.add(labels, BorderLayout.LINE_START);
                JPanel fields = new JPanel(new GridLayout(0,1));
                fields.setBorder(new TitledBorder("GridLayout(0,1)"));
                controls.add(fields, BorderLayout.CENTER);
                JPanel buttons = new JPanel(new GridLayout(0,2,2,2));
                buttons.setBorder(new TitledBorder("GridLayout(0,2,2,2)"));
                controls.add(buttons, BorderLayout.LINE_END);

                for (int ii=0; ii<4; ii++) {
                    labels.add(new JLabel("Label " + (ii+1)));
                    fields.add(new JTextField(5));
                    buttons.add(new JButton("Button " + ((ii*2) + 1)));
                    buttons.add(new JButton("Button " + ((ii*2) + 2)));
                }
                gui.add(controls, BorderLayout.PAGE_START);

                JPanel input = new JPanel(new GridLayout(0,1,2,2));
                input.setBorder(new TitledBorder(
                        "GridLayout(0,1,2,2)"));
                for (int ii=0; ii<2; ii++) {
                    input.add(new JScrollPane(new JTextArea(5,35)));
                }
                gui.add(input, BorderLayout.CENTER);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
于 2013-10-04T08:05:38.100 回答
1

尝试将 GridBagLayout 与 GridBagConstraints 一起使用。当您知道使用它时,您只需要: http ://www-mips.unice.fr/Doc/Java/Tutorial/uiswing/layout/gridbagExample.html

于 2013-10-04T08:12:41.593 回答
1

首先,您需要定义主框架的布局。

尝试在 Vigenere 构造函数的顶部添加这一行

    public Vigenere() {
        setLayout(new GridLayout(2, 1));

        JPanel topPanel = new JPanel(new GridLayout(1, 2));
        JPanel p1 = new JPanel(new GridLayout(5, 9));
        p1.add(new JLabel("Source File"));
        p1.add(jtfSourceFile);
        p1.add(new JLabel("Results File"));
        p1.add(jtfResultsFile);
        p1.add(new JLabel("Key Code"));
        p1.add(jtfKeyCode);
        p1.add(new JLabel("Compare"));
        p1.add(jtfCompare);

        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        p2.add(jbtOpen);
        p2.add(jbtSave);
        p2.add(jbtKey);
        p2.add(jbtCompare);
        p2.add(jbtEncrypt);
        p2.add(jbtDecrypt);
        p2.add(jbtClear);
        p2.add(jbtQuit);

        topPanel.add(p1);
        topPanel.add(p2);


        JPanel p3 = new JPanel(new GridLayout(2, 1));
        jtfSource.setBorder(BorderFactory.createLineBorder(Color.black));
        jtfResults.setBorder(BorderFactory.createLineBorder(Color.black));
        p3.add(jtfSource);
        p3.add(jtfResults);

        add(topPanel);
        //add(p2);
        add(p3);

        pack();

    }

并从那里继续;)

于 2013-10-04T07:34:16.087 回答