我这个程序中的Action接口中的SHORT_DESCRIPTION怎么显示不出来呀

beingyourself 2008-03-06 09:40:45
源程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ActionTest {
public static void main(String[] args)
{
ActionFrame frame=new ActionFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}


class ActionFrame extends JFrame
{
public ActionFrame()
{
setTitle("action test!");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ActionPanel panel=new ActionPanel();
add(panel);
}
private static final int DEFAULT_WIDTH=1300;
private static final int DEFAULT_HEIGHT=800;
}


class ActionPanel extends JPanel
{
public ActionPanel()
{
Action yellowAction=new ColorAction("Yellow",new ImageIcon("e:/57.gif"),Color.yellow);
Action blueAction=new ColorAction("Blue",new ImageIcon("e:/22.gif"),Color.blue);
Action redAction=new ColorAction("Red",new ImageIcon("e:/23.gif"),Color.red);


add(new JButton(yellowAction));
add(new JButton(blueAction));
add(new JButton(redAction));


InputMap imap=getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke("ctrl Y"),"panel.yellow");
imap.put(KeyStroke.getKeyStroke("ctrl B"),"panel.blue");
imap.put(KeyStroke.getKeyStroke("ctrl R"),"panel.red");
ActionMap amap=getActionMap();
amap.put("panel.yellow",yellowAction);
amap.put("panel.blue",blueAction);
amap.put("panel.red",redAction);
}
public class ColorAction extends AbstractAction
{
public ColorAction(String name,Icon icon,Color c)
{
putValue(Action.NAME,name);

putValue("color",c);
putValue(Action.SMALL_ICON,icon);

putValue(Action.SHORT_DESCRIPTION,"set panel color to "+name.toLowerCase());
}


public void actionPerformed(ActionEvent event)
{
Color c=(Color)getValue("color");
setBackground(c);
}
}

}
和书上都对过了没有错误啊,可是怎么不显示set panel color to .....这个工具提示啊!高手给个解释。
...全文
88 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
beingyourself 2008-03-07
  • 打赏
  • 举报
回复
怎么还是没有“set panel color to ...."这句话呀!
haisenmai 2008-03-06
  • 打赏
  • 举报
回复
报错:Do not use ActionFrame.add() use ActionFrame.getContentPane().add() instead


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ActionTest {
public static void main(String[] args)
{
ActionFrame frame=new ActionFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}


class ActionFrame extends JFrame
{
public ActionFrame()
{
setTitle("action test!");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ActionPanel panel=new ActionPanel();
this.getContentPane().add(panel); ///here@@@@
}
private static final int DEFAULT_WIDTH=1300;
private static final int DEFAULT_HEIGHT=800;
}


class ActionPanel extends JPanel
{
public ActionPanel()
{
Action yellowAction=new ColorAction("Yellow",new ImageIcon("e:/57.gif"),Color.yellow);
Action blueAction=new ColorAction("Blue",new ImageIcon("e:/22.gif"),Color.blue);
Action redAction=new ColorAction("Red",new ImageIcon("e:/23.gif"),Color.red);


add(new JButton(yellowAction));
add(new JButton(blueAction));
add(new JButton(redAction));


InputMap imap=getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
imap.put(KeyStroke.getKeyStroke("ctrl Y"),"panel.yellow");
imap.put(KeyStroke.getKeyStroke("ctrl B"),"panel.blue");
imap.put(KeyStroke.getKeyStroke("ctrl R"),"panel.red");
ActionMap amap=getActionMap();
amap.put("panel.yellow",yellowAction);
amap.put("panel.blue",blueAction);
amap.put("panel.red",redAction);
}
public class ColorAction extends AbstractAction
{
public ColorAction(String name,Icon icon,Color c)
{
putValue(Action.NAME,name);

putValue("color",c);
putValue(Action.SMALL_ICON,icon);

putValue(Action.SHORT_DESCRIPTION,"set panel color to "+name.toLowerCase());
}


public void actionPerformed(ActionEvent event)
{
Color c=(Color)getValue("color");
setBackground(c);
}
}

}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧