可调整大小的 Swing 布局,其中按钮根据可变尺寸排列

发布于 2024-11-08 12:02:25 字数 714 浏览 0 评论 0原文

我想使用 Java Swing 进行布局,如下图所示。

布局草图
(来源:braun-abstatt.de

左边是一个 JPanel,它是通过 PaintComponent() 绘制的,当窗口大小调整时,图形会自动缩放。 (问题不是关于那个面板的。那个已经完成了。)

现在我需要一些按钮(黑框,在 Photoshop 中添加用于绘图)到前面提到的 JPanel 的右侧。顶部和底部的红色区域的高度(旁边应该只有空白区域)是按照 CONSTANT_FACTOR * getHeight() 的方式计算的。左侧每个隔间旁边应该有一组按钮,排列在相应隔间的中心(参见蓝线)。

包含按钮的 JPanel 了解 CONSTANT_FACTOR 和隔间的数量,因此应该可以将此信息提供给布局管理器。

我最适合使用哪个布局管理器来实现此布局?我读过所有不同的布局管理器,但我不太清楚哪一个或哪一个组合最适合这种情况。

I would like to make a layout using Java Swing which looks like the following drawing.

layout sketch
(source: braun-abstatt.de)

On the left is a JPanel which is drawn through paintComponent() in a way that the graphics automatically scale when the window is resized. (The question isn't about that panel. That one's already done.)

Now I need some buttons (the black boxes, added in Photoshop for the drawing) to the right of the JPanel mentioned before. The height of the reddish areas at the top and bottom, next to which there should be just empty space, is calculated along the lines of CONSTANT_FACTOR * getHeight(). Next to each compartment on the left, there should be a group of buttons, lined up to the center of the respective compartment (see the blue lines).

The JPanel containing the buttons knows about the CONSTANT_FACTOR and the number of compartments, so it should be possible to feed this information into a layout manager.

Which layout manager would I best use to achieve this layout? I've read about all the different layout managers, but I can't quite figure out which one or which combination of them best fits in this case.

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

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

发布评论

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

评论(3

前事休说 2024-11-15 12:02:25

例如,通过使用不同的 LayoutManager,一个非常简单的和简单的容器,花费不超过15-20分钟:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class ThinLineFrame {
    private JFrame frame = new JFrame();
    private JScrollPane scrollPane;
    private JPanel panel = new JPanel();
    private JPanel panelNorth = new JPanel();
    private JPanel panelCenter = new JPanel();
    private JPanel panelCenterCh1 = new JPanel();
    private JPanel panelCenterCh2 = new JPanel();
    private JPanel panelCenterCh3 = new JPanel();
    private JPanel panelCenterCh4 = new JPanel();
    private JPanel panelCenterCh5 = new JPanel();
    private JPanel panelSouth = new JPanel();

    public ThinLineFrame() {
        panelNorth.setBackground(Color.red.darker());
        panelNorth.setPreferredSize(new Dimension(80, 30));
        //
        panelCenter.setBackground(Color.darkGray);
        panelCenter.setLayout(new GridLayout(5, 1, 2, 2));
        //
        panelCenterCh1.setLayout(new BorderLayout());
        JButton panelCenterCh1Button = new JButton();
        panelCenterCh1.add(panelCenterCh1Button, BorderLayout.CENTER);
        //
        JButton panelCenterCh2Button1 = new JButton();
        JButton panelCenterCh2Button2 = new JButton();
        panelCenterCh2.setLayout(new GridLayout(2, 1, 2, 2));
        panelCenterCh2.add(panelCenterCh2Button1);
        panelCenterCh2.add(panelCenterCh2Button2);
        //
        JButton panelCenterCh3Button1 = new JButton();
        JButton panelCenterCh3Button2 = new JButton();
        panelCenterCh3.setLayout(new GridLayout(2, 1, 2, 2));
        panelCenterCh3.add(panelCenterCh3Button1);
        panelCenterCh3.add(panelCenterCh3Button2);
        //
        JButton panelCenterCh4Button1 = new JButton();
        JButton panelCenterCh4Button2 = new JButton();
        panelCenterCh4.setLayout(new GridLayout(2, 1, 2, 2));
        panelCenterCh4.add(panelCenterCh4Button1);
        panelCenterCh4.add(panelCenterCh4Button2);
        //
        panelCenterCh5.setLayout(new BorderLayout());
        JButton panelCenterCh5Button = new JButton();
        panelCenterCh5.add(panelCenterCh5Button, BorderLayout.CENTER);
        //
        panelCenter.add(panelCenterCh1);
        panelCenter.add(panelCenterCh2);
        panelCenter.add(panelCenterCh3);
        panelCenter.add(panelCenterCh4);
        panelCenter.add(panelCenterCh5);
        //
        panelSouth.setBackground(Color.red.darker());
        panelSouth.setPreferredSize(new Dimension(80, 30));
        //
        panel.setLayout(new BorderLayout(2, 2));
        panel.add(panelNorth, BorderLayout.NORTH);
        panel.add(panelCenter, BorderLayout.CENTER);
        panel.add(panelSouth, BorderLayout.SOUTH);
        //
        scrollPane = new JScrollPane(panel);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(80, 600));
        frame.setLocation(100, 150);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ThinLineFrame dlg = new ThinLineFrame();
            }
        });
    }
}

For example, by use of a different LayoutManager, a very easy and simple container, takes no more than 15-20 minutes:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class ThinLineFrame {
    private JFrame frame = new JFrame();
    private JScrollPane scrollPane;
    private JPanel panel = new JPanel();
    private JPanel panelNorth = new JPanel();
    private JPanel panelCenter = new JPanel();
    private JPanel panelCenterCh1 = new JPanel();
    private JPanel panelCenterCh2 = new JPanel();
    private JPanel panelCenterCh3 = new JPanel();
    private JPanel panelCenterCh4 = new JPanel();
    private JPanel panelCenterCh5 = new JPanel();
    private JPanel panelSouth = new JPanel();

    public ThinLineFrame() {
        panelNorth.setBackground(Color.red.darker());
        panelNorth.setPreferredSize(new Dimension(80, 30));
        //
        panelCenter.setBackground(Color.darkGray);
        panelCenter.setLayout(new GridLayout(5, 1, 2, 2));
        //
        panelCenterCh1.setLayout(new BorderLayout());
        JButton panelCenterCh1Button = new JButton();
        panelCenterCh1.add(panelCenterCh1Button, BorderLayout.CENTER);
        //
        JButton panelCenterCh2Button1 = new JButton();
        JButton panelCenterCh2Button2 = new JButton();
        panelCenterCh2.setLayout(new GridLayout(2, 1, 2, 2));
        panelCenterCh2.add(panelCenterCh2Button1);
        panelCenterCh2.add(panelCenterCh2Button2);
        //
        JButton panelCenterCh3Button1 = new JButton();
        JButton panelCenterCh3Button2 = new JButton();
        panelCenterCh3.setLayout(new GridLayout(2, 1, 2, 2));
        panelCenterCh3.add(panelCenterCh3Button1);
        panelCenterCh3.add(panelCenterCh3Button2);
        //
        JButton panelCenterCh4Button1 = new JButton();
        JButton panelCenterCh4Button2 = new JButton();
        panelCenterCh4.setLayout(new GridLayout(2, 1, 2, 2));
        panelCenterCh4.add(panelCenterCh4Button1);
        panelCenterCh4.add(panelCenterCh4Button2);
        //
        panelCenterCh5.setLayout(new BorderLayout());
        JButton panelCenterCh5Button = new JButton();
        panelCenterCh5.add(panelCenterCh5Button, BorderLayout.CENTER);
        //
        panelCenter.add(panelCenterCh1);
        panelCenter.add(panelCenterCh2);
        panelCenter.add(panelCenterCh3);
        panelCenter.add(panelCenterCh4);
        panelCenter.add(panelCenterCh5);
        //
        panelSouth.setBackground(Color.red.darker());
        panelSouth.setPreferredSize(new Dimension(80, 30));
        //
        panel.setLayout(new BorderLayout(2, 2));
        panel.add(panelNorth, BorderLayout.NORTH);
        panel.add(panelCenter, BorderLayout.CENTER);
        panel.add(panelSouth, BorderLayout.SOUTH);
        //
        scrollPane = new JScrollPane(panel);
        frame.add(scrollPane, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(80, 600));
        frame.setLocation(100, 150);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ThinLineFrame dlg = new ThinLineFrame();
            }
        });
    }
}
稚气少女 2024-11-15 12:02:25

您应该尝试查看 MigLayout。这是一个超级灵活的布局管理器,也非常简单。

代码看起来像这样:

MigLayout layout = new MigLayout("flowy");
panel.setLayoutManager(layout);
panel.add(button1);
panel.adD(button2);
etc..

尝试将 debug, flowy 添加到构造函数中,以直观地了解正在发生的情况。

You should try looking at MigLayout. It's a super flexible LayoutManager that is also very simple.

The code would look something like:

MigLayout layout = new MigLayout("flowy");
panel.setLayoutManager(layout);
panel.add(button1);
panel.adD(button2);
etc..

Try adding debug, flowy to the constructor to get a visual idea of what is going on.

骄兵必败 2024-11-15 12:02:25

GBC 没有锚点,只有普通的 GridBagConstraints 和首选大小。

具有固定大小的居中 JButton:

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MainWithFixSize {
    public static void main(String[] argv) throws Exception {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        GridBagLayout gbl = new GridBagLayout();
        container.setLayout(gbl);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        JButton component = new JButton();
        component.setPreferredSize(new Dimension(25, 25));
        gbl.setConstraints(component, gbc);
        container.add(component);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(40, 90));
        frame.pack();
        frame.setVisible(true);
    }

    private MainWithFixSize() {
    }
}

GBC without an anchor, just with plain vanilla GridBagConstraints and preferred size.

Centered JButton with fixed size:

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MainWithFixSize {
    public static void main(String[] argv) throws Exception {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        GridBagLayout gbl = new GridBagLayout();
        container.setLayout(gbl);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        JButton component = new JButton();
        component.setPreferredSize(new Dimension(25, 25));
        gbl.setConstraints(component, gbc);
        container.add(component);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(40, 90));
        frame.pack();
        frame.setVisible(true);
    }

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