【问题标题】:Swing panels with different sizes不同尺寸的摇摆板
【发布时间】:2016-08-02 20:31:43
【问题描述】:

我正在尝试将组件放入具有不同尺寸的面板中。但是,我意识到 GridLayout 将大小相同的部分分开。如何实现如下图所示

enter image description here

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

class PanelDemo {

    PanelDemo() {

        // Create a new JFrame container. Use the default
        // border layout.
        JFrame jfrm = new JFrame("Use Three JPanels with Different Sizes");

        // Specify FlowLayout manager.
        jfrm.getContentPane().setLayout(new GridLayout(1, 3));

        // Give the frame an initial size.
        jfrm.setSize(900, 300);

        // Terminate the program when the user closes the application.
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create the first JPanel.
        JPanel jpnl = new JPanel();

        jpnl.setLayout(new GridLayout(2, 4));

        // Set the preferred size of the first panel.
        jpnl.setPreferredSize(new Dimension(500, 300));

        // Make the panel opaque.
        jpnl.setOpaque(true);

        // Add a blue border to the panel.
        jpnl.setBorder(
                BorderFactory.createLineBorder(Color.BLUE));

        // Create the second JPanel.
        JPanel jpnl2 = new JPanel();

        //jpnl2.setLayout(new FlowLayout());
        // Set the preferred size of the second panel.
        jpnl2.setPreferredSize(new Dimension(300, 300));

        // Make the panel opaque.
        jpnl2.setOpaque(true);

        jpnl2.setBorder(
                BorderFactory.createLineBorder(Color.RED));

        JPanel jpnl3 = new JPanel();
        jpnl3.setOpaque(true);
        jpnl3.setPreferredSize(new Dimension(100, 300));
        jpnl3.setBorder(
                BorderFactory.createLineBorder(Color.ORANGE));

        // Add the panels to the frame.
        jfrm.getContentPane().add(jpnl);
        jfrm.getContentPane().add(jpnl3);
        jfrm.getContentPane().add(jpnl2);

        // Display the frame.
        jfrm.setVisible(true);
    }

    public static void main(String args[]) {
        // Create the frame on the event dispatching thread.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new PanelDemo();
            }
        });
    }
}

【问题讨论】:

  • BorderLayout using LINE_START, CENTER & LINE_END 约束可以做到这一点。但如果 GUI 可调整大小,中心面板将获得额外的空间..
  • 顺便说一句 - 每个面板都有什么?如果是“组件”,最好允许用于面板的布局提供有关首选尺寸的提示,而不是显式设置它。然后不要调用jfrm.setSize(900, 300); - 只需调用jfrm.pack();(毕竟添加组件)..
  • BoxLayout?网格包布局?阅读 Layout Managers 上的 Swing 教程。每个布局管理器都有不同的规则来调整框架大小的大小。无论您使用什么布局都取决于您的确切要求。与每个布局管理器一起玩,然后确定最适合您的要求。
  • 我会将带有 gridlayout(2,4) 的按钮放入第一个,第二个只是为了显示一点空白区域,第三个是按钮 gridlayout(2,2)。我应该总是/通常使用pack而不是setSize吗? @安德鲁汤普森
  • pack() 将使 GUI 的大小达到所需的大小,以容纳组件和框架装饰。 setSize(..) 只是猜测该大小应该是多少。根据您的描述,很明显不需要第二个面板。可以使用应用于第一个面板的EmptyBorder 来实现。给我几分钟,我会创建布局..

标签: java swing jpanel layout-manager


【解决方案1】:

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

public class TwoPanelWithButtonsLayout {

    private JComponent ui = null;
    private Insets buttonMargin = new Insets(10,10,10,10);

    TwoPanelWithButtonsLayout() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        int gap = 5;

        JPanel buttons1 = new JPanel(new GridLayout(2, 4, gap, gap));
        // 50 is the gap on right, alter as needed
        buttons1.setBorder(new EmptyBorder(0, 0, 0, 50)); 
        for (int ii=1; ii<9; ii++) {
            buttons1.add(getBigButton("" + ii));
        }
        ui.add(buttons1, BorderLayout.CENTER);

        JPanel buttons2 = new JPanel(new GridLayout(2, 2, gap, gap));
        for (int ii=1; ii<5; ii++) {
            buttons2.add(getBigButton("" + ii));
        }
        ui.add(buttons2, BorderLayout.LINE_END);
    }

    private JButton getBigButton(String text) {
        JButton b = new JButton(text);
        Font f = b.getFont();
        b.setFont(f.deriveFont(f.getSize()*3f));
        b.setMargin(buttonMargin);
        return b;
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                TwoPanelWithButtonsLayout o = new TwoPanelWithButtonsLayout();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

【讨论】:

  • 顺便问一下,你更喜欢表单构建器(ide的GUI制造商)还是手写来制作GUI?如果你更喜欢表单生成器,你能告诉我你在用什么吗?
  • 我手工制作我的 GUI。表单构建器适用于原型设计,但除此之外就没什么了。此外,要有效地使用它们,您需要了解在任何情况下如何使用和组合布局。
  • 感谢您的帮助,但我需要更多。我已经编辑了我的问题。你能看一眼吗?
  • “我已经编辑了我的问题” 最好提出一个新问题。如果该方法与新问题相关,请随意根据我的回答。
【解决方案2】:

您可以使用FlowLayout,但如果他们调整您的框架大小,那么它将包裹面板。

【讨论】:

猜你喜欢
  • 2020-12-26
  • 1970-01-01
  • 2012-11-20
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 2016-08-18
相关资源
最近更新 更多