如何使用 MigLayout 在面板上绘制分隔符 [英] How to draw a separator across a panel using MigLayout

查看:34
本文介绍了如何使用 MigLayout 在面板上绘制分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 MigLayout 新手问题.我想知道如何从标签的末尾跨面板的宽度绘制分隔符.

This is a MigLayout-newbie question. I'd like to know how to draw a separator from the end of a label across the width of a panel.

这是我的示例代码:

package com.ndh.swingjunk;

import java.awt.*;
import javax.swing.*;
import net.miginfocom.swing.MigLayout;

class MyJFrame extends JFrame {
    public MyJFrame() {
        super();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel label = new JLabel("foo");
        label.setFont(new Font("Tahoma", Font.BOLD, 11));
        JSeparator separator = new JSeparator();
        JLabel anotherLabel = new JLabel("some other label");
        anotherLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
        JButton button1 = new JButton("button 1");
        JButton button2 = new JButton("button 2");
        JPanel panel = new JPanel(new MigLayout());
        panel.add(label);
        panel.add(separator, "growx, wrap");
        panel.add(anotherLabel);
        panel.add(button1);
        panel.add(button2);
        getContentPane().add(panel);
        pack();
    }
}

public class SeparatorLayoutQuestion {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {new MyJFrame().setVisible(true);}});}}

结果如下:

这是非常蹩脚的.我希望分隔符从Foo"的末端延伸到面板的末端,一直延伸到按钮 2.

which is terribly lame. I'd like the separator to stretch from the end of "Foo" to the end of the panel, all the way across button 2.

推荐答案

这对我有用,我不得不使用拆分",以便标签和分隔符可以共享同一个单元格:

Here's what worked for me, I had to use "split" so the label and separator could share the same cell:

JPanel panel = new JPanel(new MigLayout());
panel.add(label, "split 2, span");
panel.add(separator, "growx, wrap");
panel.add(anotherLabel);
panel.add(button1);
panel.add(button2);

这篇关于如何使用 MigLayout 在面板上绘制分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆