我正在使用 Miglayout 为我的一个面板创建类似表格的布局。我需要所有面板的固定宽度为 200 像素。当我在面板中添加组件时,一切正常,但是当我尝试插入一个具有长文本的按钮(因此需要超过 200 像素的空间来显示)时,按钮会溢出其单元格并与相邻按钮重叠。这段代码应该演示我的问题:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
/**
* @author Savvas Dalkitsis
*/
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
JPanel content = new JPanel(new MigLayout("wrap 5","[200!]","[50!]"));
JButton b = new JButton("Button 1");
content.add(b,"growx");
b = new JButton("Button 2");
content.add(b,"growx");
b = new JButton("Button with a very long text which should not be visible");
content.add(b,"growx");
b = new JButton("Button 4");
content.add(b,"growx");
b = new JButton("Button 5");
content.add(b,"growx");
b = new JButton("Button 6");
content.add(b,"growx");
b = new JButton("Button 7");
content.add(b,"growx");
b = new JButton("Button 8");
content.add(b,"growx");
b = new JButton("Button 9");
content.add(b,"growx");
b = new JButton("Button 10");
content.add(b,"growx");
f.setContentPane(content);
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
我想要的是按钮来显示它可以容纳在 200 像素中的所有文本,然后可能是一些尾随句点,例如“带有版本的按钮......”
有没有人知道如何实现这一目标?
(您可以从这里获得 miglayout进行测试)