使用哪个布局管理器?

发布于 11-03 00:45 字数 130 浏览 8 评论 0原文

我想用 java 创建一个 JPanel ,其布局类似于下面的布局。有什么想法吗?

在此处输入图像描述

I'd like to create a JPanel in java with a layout similar to the one below. Any ideas?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南汐寒笙箫2024-11-10 00:45:11

使用垂直 BoxLayout 并设置每行上项目的对齐方式。 (使每一行都有自己的 JPanel

另一种选择是使用 SpringLayoutGridBoxLayout 并为每个缩进设置间距。

Use a vertical BoxLayout and set the alignment of items on each row. (Make each row its own JPanel)

Another option is to use a SpringLayout or a GridBoxLayout and setup spacing for each indentation.

归途2024-11-10 00:45:11

看看 miglayout。有了这个布局管理器,您将永远不必询问在特定情况下使用哪个布局管理器:-)

Take a look at miglayout. With this layout manager, you will never have to ask which layout manager to use for a specific situation :-)

固执像三岁2024-11-10 00:45:11

这是使用 jgoodies forms FormLayout(来自 google 的库)的实现:

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

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

public class FormSample {

    private static JPanel generatePanel() {

        FormLayout layout = new FormLayout(
                "3dlu, 15dlu, max(50dlu;p), 2dlu, p, 3dlu", // column definition
                "5dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 5dlu" // row definition
        );

        JPanel panel = new JPanel(layout);

        CellConstraints cc = new CellConstraints();
        int row = 0;
        panel.add(new JLabel("Amplitude"), cc.xyw(2, row * 4 + 2, 4));
        panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
        panel.add(new JLabel("V"), cc.xy(5, row * 4 + 4));
        row++;
        panel.add(new JLabel("Off-set"), cc.xyw(2, row * 4 + 2, 4));
        panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
        panel.add(new JLabel("V"), cc.xy(5, row * 4 + 4));
        row++;
        panel.add(new JLabel("Frequentie"), cc.xyw(2, row * 4 + 2, 4));
        panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
        panel.add(new JLabel("Hz"), cc.xy(5, row * 4 + 4));
        row++;
        panel.add(new JLabel("Fase"), cc.xyw(2, row * 4 + 2, 4));
        panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
        panel.add(new JLabel("rad"), cc.xy(5, row * 4 + 4));

        return panel;
    }


    public static void main(String[] args) {
        // create frame
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // add content panel
        frame.setContentPane(generatePanel());

        // shring/expand to optimal size
        frame.pack();

        // center frame
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension resolution = toolkit.getScreenSize();
        Dimension frameSize = frame.getSize();
        int xLocation = resolution.width / 2 - frameSize.width / 2;
        int yLocation = resolution.height / 2 - frameSize.height / 2;
        frame.setLocation(xLocation, yLocation);

        // show frame
        frame.setVisible(true);
    }
}

This is the implementation using jgoodies forms FormLayout (library from google):

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

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

public class FormSample {

    private static JPanel generatePanel() {

        FormLayout layout = new FormLayout(
                "3dlu, 15dlu, max(50dlu;p), 2dlu, p, 3dlu", // column definition
                "5dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 2dlu, p, 5dlu" // row definition
        );

        JPanel panel = new JPanel(layout);

        CellConstraints cc = new CellConstraints();
        int row = 0;
        panel.add(new JLabel("Amplitude"), cc.xyw(2, row * 4 + 2, 4));
        panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
        panel.add(new JLabel("V"), cc.xy(5, row * 4 + 4));
        row++;
        panel.add(new JLabel("Off-set"), cc.xyw(2, row * 4 + 2, 4));
        panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
        panel.add(new JLabel("V"), cc.xy(5, row * 4 + 4));
        row++;
        panel.add(new JLabel("Frequentie"), cc.xyw(2, row * 4 + 2, 4));
        panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
        panel.add(new JLabel("Hz"), cc.xy(5, row * 4 + 4));
        row++;
        panel.add(new JLabel("Fase"), cc.xyw(2, row * 4 + 2, 4));
        panel.add(new JTextField(), cc.xy(3, row * 4 + 4));
        panel.add(new JLabel("rad"), cc.xy(5, row * 4 + 4));

        return panel;
    }


    public static void main(String[] args) {
        // create frame
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // add content panel
        frame.setContentPane(generatePanel());

        // shring/expand to optimal size
        frame.pack();

        // center frame
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension resolution = toolkit.getScreenSize();
        Dimension frameSize = frame.getSize();
        int xLocation = resolution.width / 2 - frameSize.width / 2;
        int yLocation = resolution.height / 2 - frameSize.height / 2;
        frame.setLocation(xLocation, yLocation);

        // show frame
        frame.setVisible(true);
    }
}
美人如玉2024-11-10 00:45:11

我使用 GroupLayout 管理器创建了一个示例。缩进
组件是使用 LayoutStyle.ComponentPlacement.INDENT 创建的
元件放置类型。

package com.zetcode;

import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;


public class GroupLayoutIndent extends JFrame {

    public GroupLayoutIndent() {

        initUI();

        setTitle("Indents");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void initUI() {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        JLabel ampLbl = new JLabel("Amplitude");
        JLabel offLbl = new JLabel("Off-set");
        JLabel freqLbl = new JLabel("Frequency");
        JLabel phsLbl = new JLabel("Phase");
        JLabel unit1 = new JLabel("V");
        JLabel unit2 = new JLabel("V");
        JLabel unit3 = new JLabel("Hz");
        JLabel unit4 = new JLabel("rad");

        JTextField field1 = new JTextField(12);
        JTextField field2 = new JTextField(12);
        JTextField field3 = new JTextField(12);
        JTextField field4 = new JTextField(12);

        gl.setAutoCreateGaps(true);
        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup()
                .addComponent(ampLbl)
                .addGroup(gl.createSequentialGroup()
                        .addPreferredGap(ampLbl, field1,
                                LayoutStyle.ComponentPlacement.INDENT)
                        .addComponent(field1)
                        .addComponent(unit1))
                .addComponent(offLbl)
                .addGroup(gl.createSequentialGroup()
                        .addPreferredGap(offLbl, field2,
                                LayoutStyle.ComponentPlacement.INDENT)
                        .addComponent(field2)
                        .addComponent(unit2))
                .addComponent(freqLbl)
                .addGroup(gl.createSequentialGroup()
                        .addPreferredGap(freqLbl, field3,
                                LayoutStyle.ComponentPlacement.INDENT)
                        .addComponent(field3)
                        .addComponent(unit3))     
                .addComponent(phsLbl)
                .addGroup(gl.createSequentialGroup()
                        .addPreferredGap(phsLbl, field4,
                                LayoutStyle.ComponentPlacement.INDENT)
                        .addComponent(field4)
                        .addComponent(unit4))                 
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(ampLbl)
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(field1)
                        .addComponent(unit1))
                .addComponent(offLbl)
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(field2)
                        .addComponent(unit2))   
                .addComponent(freqLbl)
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(field3)
                        .addComponent(unit3))
                .addComponent(phsLbl)
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(field4)
                        .addComponent(unit4))
        );

        gl.linkSize(field1, field2, field3, field4);

        pack();
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GroupLayoutIndent ex = new GroupLayoutIndent();
                ex.setVisible(true);
            }
        });
    }
}

GroupLayout 缩进

I have created an example with the GroupLayout manager. Indented
components are created with the LayoutStyle.ComponentPlacement.INDENT
component placement type.

package com.zetcode;

import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.BASELINE;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;


public class GroupLayoutIndent extends JFrame {

    public GroupLayoutIndent() {

        initUI();

        setTitle("Indents");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void initUI() {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        JLabel ampLbl = new JLabel("Amplitude");
        JLabel offLbl = new JLabel("Off-set");
        JLabel freqLbl = new JLabel("Frequency");
        JLabel phsLbl = new JLabel("Phase");
        JLabel unit1 = new JLabel("V");
        JLabel unit2 = new JLabel("V");
        JLabel unit3 = new JLabel("Hz");
        JLabel unit4 = new JLabel("rad");

        JTextField field1 = new JTextField(12);
        JTextField field2 = new JTextField(12);
        JTextField field3 = new JTextField(12);
        JTextField field4 = new JTextField(12);

        gl.setAutoCreateGaps(true);
        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createParallelGroup()
                .addComponent(ampLbl)
                .addGroup(gl.createSequentialGroup()
                        .addPreferredGap(ampLbl, field1,
                                LayoutStyle.ComponentPlacement.INDENT)
                        .addComponent(field1)
                        .addComponent(unit1))
                .addComponent(offLbl)
                .addGroup(gl.createSequentialGroup()
                        .addPreferredGap(offLbl, field2,
                                LayoutStyle.ComponentPlacement.INDENT)
                        .addComponent(field2)
                        .addComponent(unit2))
                .addComponent(freqLbl)
                .addGroup(gl.createSequentialGroup()
                        .addPreferredGap(freqLbl, field3,
                                LayoutStyle.ComponentPlacement.INDENT)
                        .addComponent(field3)
                        .addComponent(unit3))     
                .addComponent(phsLbl)
                .addGroup(gl.createSequentialGroup()
                        .addPreferredGap(phsLbl, field4,
                                LayoutStyle.ComponentPlacement.INDENT)
                        .addComponent(field4)
                        .addComponent(unit4))                 
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(ampLbl)
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(field1)
                        .addComponent(unit1))
                .addComponent(offLbl)
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(field2)
                        .addComponent(unit2))   
                .addComponent(freqLbl)
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(field3)
                        .addComponent(unit3))
                .addComponent(phsLbl)
                .addGroup(gl.createParallelGroup(BASELINE)
                        .addComponent(field4)
                        .addComponent(unit4))
        );

        gl.linkSize(field1, field2, field3, field4);

        pack();
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GroupLayoutIndent ex = new GroupLayoutIndent();
                ex.setVisible(true);
            }
        });
    }
}

GroupLayout indent

倚栏听风2024-11-10 00:45:11

GroupLayout 是 NetBeans(以及其他一些 IDE,如果我没记错的话)中 GUI 设计工具的默认布局管理器。

GroupLayout is the default layout manager for the GUI-design tool in NetBeans (and some other IDEs, if I'm not mistaken).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文