相关代码如下:
class ExitAction extends AbstractAction{
public ExitAction(){
super("退出",new ImageIcon(this.getClass().getResource("exit.gif")));
}
public void actionPerformed(ActionEvent e){
jMenuFileExit_actionPerformed(e);
}
}
jToolBar1.add(new ExitAction());只能显示图标,而没有显示“退出”的文字,而如果将传进去的图标设为null就显示出来了,这是为什么?
class ExitAction extends AbstractAction{
public ExitAction(){
super("退出",new ImageIcon(this.getClass().getResource("exit.gif")));
}
public void actionPerformed(ActionEvent e){
jMenuFileExit_actionPerformed(e);
}
}
jToolBar1.add(new ExitAction());只能显示图标,而没有显示“退出”的文字,而如果将传进去的图标设为null就显示出来了,这是为什么?
class OpenAction extends AbstractAction{
public OpenAction(){
super("打开",new ImageIcon("open.jpg"));
} public void actionPerformed(ActionEvent e){
...
}
}JToolBar toolbar = new JToolBar();
toolbar.add(new OpenAction());
???
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ToolBarFrameTest extends JFrame
{
public ToolBarFrameTest()
{
this.setPreferredSize(new Dimension(300, 200));
JToolBar toolBar = new JToolBar();
toolBar.add(new ExitAction());
add(toolBar, BorderLayout.NORTH);
}
private class ExitAction extends AbstractAction
{
public ExitAction()
{
super("退出", new ImageIcon("d:\\exit.jpg"));
putValue(Action.SHORT_DESCRIPTION, "退出"); //这里添加描述
}
public void actionPerformed(ActionEvent event)
{
}
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new ToolBarFrameTest();
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}
JToolBar的createActionComponent方法中
有一句
if (icon !=null) {
b.putClientProperty("hideActionText", Boolean.TRUE);
}
这句导致文字不显示!
这样决定可能是因为一般默认的工具栏不显示文字的"
可能是原因吧!