Miglayout 按钮溢出约束
我正在使用 Miglayout 为我的一个面板创建类似表格的布局。我需要所有面板的固定宽度为 200 像素。当我在面板中添加组件时,一切正常,但是当我尝试插入具有长文本的按钮(因此需要超过 200 px 的空间来显示)时,按钮会溢出其单元格并与相邻按钮重叠。这段代码应该说明我的问题:
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进行测试)
I am using a Miglayout to create a table-like layout for one of my panels. I need all my panels to have a fixed width of 200 pixels. When i add components in the panel everything works OK but when i try to insert a button that has a long text (and therefore needs more space than 200 px to display) the button overflows its cell and overlaps neighboring buttons. This code should demonstrate my problem:
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);
}
}
What i would like is the button to display all the text it can fit in the 200 pixels and then maybe some trailing periods like "Button with ver..."
Does any one have an idea on how to achieve this?
(you can get miglayout from here for testing)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚下载了布局来查看。您的解决方案是否只是:
它对我有用。
Just downloaded the layout to check it out. Is your solution just:
It worked for me.