[英]Java/JButton: How do I get rid of ellipses on JButton?
I'm having this weird issue and it doesn't seem to be happening on Mac systems, but on Windows. 我有这个奇怪的问题,它似乎不是在Mac系统上发生,而是在Windows上。 I'm working on this GUI and am trying to make all of the buttons the same length (the length of the longest button) but for some reason on Windows, one of the buttons will not display the entire string and will truncate it with an ellipsis (...).
我正在使用这个GUI,我正在尝试使所有按钮长度相同(最长按钮的长度)但由于某些原因在Windows上,其中一个按钮将不会显示整个字符串并将其截断为省略号(...)。
Here is the method I am using to check and set the longest button label: 这是我用来检查和设置最长按钮标签的方法:
//Temp button to hold the longest button for resizing
JButton longestButton = new JButton();
//Find size of the largest of the 6 buttons
for(int i = 0; i < 6; i++){
if (buttons[i].getText().length() > longestButton.getText().length()){
longestButton = buttons[i];
}
}
//Match size of 6 hex buttons to the size of the longest one
for(int i = 0; i < 6; i++){
buttons[i].setPreferredSize(longestButton.getPreferredSize());
}
Anybody have some insight into this? 有人对此有所了解吗?
Don't use setPreferredSize()
when you really mean to override getPreferredSize()
. 当你真的想要覆盖
getPreferredSize()
时,不要使用setPreferredSize()
getPreferredSize()
。 BoxLayout
works well for this: "For a top-to-bottom box layout, the preferred width of the container is that of the maximum preferred width of the children." BoxLayout
适用于此:“对于从上到下的盒子布局,容器的首选宽度是子容器的最大首选宽度。” In the example below, BorderLayout.LINE_END
adopts the container's preferred width. 在下面的示例中,
BorderLayout.LINE_END
采用容器的首选宽度。 Each button overrides getMaximumSize()
to effectively remove the upper limit on the button's width. 每个按钮都会覆盖
getMaximumSize()
以有效删除按钮宽度的上限。 The result works well across platforms. 结果适用于各种平台。
import java.awt.*;
import javax.swing.*;
/**
* @see https://stackoverflow.com/a/20085489/230513
*/
public class TestViewer {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ButtonPanel(), BorderLayout.LINE_END);
frame.pack();
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
private static class ButtonPanel extends Box {
public ButtonPanel() {
super(BoxLayout.Y_AXIS);
this.add(createButton("Button 1"));
this.add(createButton("Button 2"));
this.add(createButton("Long Button 3"));
this.add(createButton("Button 4"));
this.add(createButton("Button 5"));
}
private JButton createButton(String name) {
final JButton b = new JButton(name) {
@Override
public Dimension getMaximumSize() {
return new Dimension(
Short.MAX_VALUE, getPreferredSize().height);
}
};
b.setAlignmentX(0.5f);
return b;
}
}
}
Instead of finding the button with the biggest .getText().length(), you could find the button with the widest preferredSize(). 您可以找到具有最宽的preferredSize()的按钮,而不是找到具有最大.getText()。length()的按钮。
for(int i = 0; i < 6; i++){
if (buttons[i].getPreferredSize().getWidth() > longestButton.getPreferredSize().getWidth()){
longestButton = buttons[i];
}
}
This fixes the issue vandale mentioned in the comments 这修复了评论中提到的问题vandale
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.