首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套布局- BoxLayout inside

嵌套布局- BoxLayout inside
EN

Code Review用户
提问于 2013-06-04 12:22:26
回答 2查看 6.3K关注 0票数 1

我有这样的代码:

代码语言:javascript
复制
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.PAGE_AXIS));
TitledBorder tb2 = BorderFactory.createTitledBorder(null, "Control Panel", 
        TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION, myFont, new Color(0, 153, 0));
controlPanel.setBorder(tb2);

JPanel fromDate = new JPanel();
fromDate.setLayout(new BoxLayout(fromDate, BoxLayout.X_AXIS));
fromDate.add(new JLabel("From date: "));
JButton fromDateButton = new JButton("...");
fromDateButton.setMaximumSize(new Dimension(100,15));
fromDate.add(fromDateButton);
JPanel toDate = new JPanel();
toDate.setLayout(new BoxLayout(toDate, BoxLayout.X_AXIS));
toDate.add(new JLabel("Until date: "));
JButton toDateButton = new JButton("...");
toDateButton.setMaximumSize(new Dimension(100,15));
toDate.add(toDateButton);
controlPanel.add(Box.createRigidArea(new Dimension(0,10)));
controlPanel.add(fromDate);
controlPanel.add(Box.createRigidArea(new Dimension(0,10)));
controlPanel.add(toDate);
toDate.setAlignmentX(Component.LEFT_ALIGNMENT);
fromDate.setAlignmentX(Component.LEFT_ALIGNMENT);

gui.add(controlPanel, BorderLayout.WEST);

它产生了这个GUI (相关部分如下所示):

我想在旁边有一个标签和一个按钮。应该有更好的方法来实现这一点。有什么建议吗?

EN

回答 2

Code Review用户

回答已采纳

发布于 2013-06-04 14:03:02

创建表单时,通常需要一个GridBagLayout

这里有一个在你的问题中创建表单的方法。

代码语言:javascript
复制
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SimpleGridBagLayout implements Runnable {

    @Override
    public void run() {
        JFrame frame = new JFrame("GridBagLayout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.gridwidth = 2;
        gbc.gridx = 0;
        gbc.gridy = 0;

        JLabel titleLabel = new JLabel("Control Panel");
        panel.add(titleLabel, gbc);

        gbc.anchor = GridBagConstraints.LINE_START;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        gbc.gridy++;

        JLabel fromDateLabel = new JLabel("From date:");
        panel.add(fromDateLabel, gbc);

        gbc.gridx++;

        JButton fromDateButton = new JButton("...");
        panel.add(fromDateButton, gbc);

        gbc.gridx = 0;
        gbc.gridy++;

        JLabel toDateLabel = new JLabel("To date:");
        panel.add(toDateLabel, gbc);

        gbc.gridx++;

        JButton toDateButton = new JButton("...");
        panel.add(toDateButton, gbc);

        frame.add(panel);
        frame.setSize(250, 200);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SimpleGridBagLayout());
    }

}
票数 4
EN

Code Review用户

发布于 2013-09-13 16:52:09

在x3中加入胶水,以获得额外的空间,然后按钮就不会变大。

代码语言:javascript
复制
panel.add(Box.createGlue(), gbc);
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/26980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档