Den*_*ill 9 java swing label jtogglebutton
在a中使用时Windows 7
JToolBar
,JToggleButton
有时会截断其标签文本.
请参阅下面的代码中的示例.以大写字母开头的切换按钮'W'
将被截断; 一个以空格(或甚至是小写'w'
)开头的人不会.
这只发生在Windows
?有人可以解释为什么会这样吗?
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6386636
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/////////////////////// new class
public class Truncation_Example extends JToolBar {
private static final long serialVersionUID = 1L;
/////////////////////// object attributes
JToggleButton toggle_Good;
JToggleButton toggle_Bad;
/////////////////////// constructors
public Truncation_Example() {
toggle_Good = new JToggleButton(new Action_Good());
toggle_Bad = new JToggleButton(new Action_Bad());
this.add(toggle_Good);
this.add(toggle_Bad);
}
/////////////////////// inner classes
public class Action_Good extends AbstractAction {
private static final long serialVersionUID = 1L;
public Action_Good() {
putValue(Action.NAME, " Wrap Good "); // note added space to prevent truncation
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Toggle: " + toggle_Good.getText());
}
}
public class Action_Bad extends AbstractAction {
private static final long serialVersionUID = 1L;
public Action_Bad() {
putValue(Action.NAME, "Wrap Bad"); // label will be truncated if it begins with 'W'
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Toggle: " + toggle_Bad.getText());
}
}
/////////////////////// main
public static void main(String[] args) {
UIManager.put("ToggleButton.select", Color.GREEN);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Truncation_Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolBar = new Truncation_Example();
frame.add(toolBar, BorderLayout.NORTH);
frame.setSize(500, 400);
frame.setVisible(true);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
这个问题取决于 LookAndFeel,让我解释一下原因......
如果安装了 MetalLookAndFeel(默认情况下),此示例确实会截断文本。对于任何其他 L&F(Basic、Windows、Nimbus,甚至在我自己的 L&F)中,我都没有看到这个问题。似乎 MetalLookAndFeel 在 MetalButtonUI 或 L&F 常量中存在某种错误,导致按钮文本渲染不正确。
我不确定它可能是什么 - 您可以简单地调试 MetalButtonUI 类来查看尺寸计算中发生的情况。不管怎样,我怀疑即使你找到了这个问题的根源,你也会改变任何事情。
归档时间: |
|
查看次数: |
638 次 |
最近记录: |