在 a 中使用时Windows 7
JToolBar
, aJToggleButton
有时会截断其标签文本。
请参阅下面代码中的示例。以大写字母开头的切换按钮'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);
}
});
}
}