主题:[讨论]一个关于Swing菜单中显示图标的问题
这个是我的代码:
代码可能有点长,但是思路简单。关键就是菜单中无法显示出图标来,无法显示1.gif这个图象来。
可能这里不怎么好看,我在别的地方写了好了用JAVA代码编辑器写好了,地址:
[url=http://www.chinajavaworld.com/thread.jspa?threadID=747071&tstart=0]http://www.chinajavaworld.com/thread.jspa?threadID=747071&tstart=0[/url]
package jmenu;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MenuMain{
public static void main(String[]args){
MenuFrame frame=new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MenuFrame extends JFrame{
private Action saveaction,saveasaction;
private JCheckBoxMenuItem readonly;
private JPopupMenu pop;
public MenuFrame(){
this.setTitle("Jmenu Test");
this.setSize(400,400);
JMenu filemenu=new JMenu("File");
JMenuItem newitem=filemenu.add(new TestAction("New"));
JMenuItem openitem=filemenu.add(new TestAction("Open"));
openitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
filemenu.addSeparator();
saveaction=new TestAction("Save");
JMenuItem saveitem=filemenu.add(saveaction);
saveitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_DOWN_MASK));
saveasaction=new TestAction("Save as");
filemenu.add(saveasaction);
filemenu.addSeparator();
filemenu.add(new
AbstractAction("Exit"){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);
readonly=new JCheckBoxMenuItem("Read-only");
readonly.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent e){
boolean saveok=! readonly.isSelected();
saveaction.setEnabled(saveok);
saveasaction.setEnabled(saveok);
}
}
);
ButtonGroup buttongroup=new ButtonGroup();
JRadioButtonMenuItem inserttiem=new JRadioButtonMenuItem("Insert",true);
JRadioButtonMenuItem overtypeitem=new JRadioButtonMenuItem("overtype",false);
buttongroup.add(inserttiem);
buttongroup.add(overtypeitem);
Action cutaction=new TestAction("Cut");
cutaction.putValue(Action.SMALL_ICON,new ImageIcon("1.gif"));//本来这个1.gif就要显示在菜单中啊。
Action copyaction=new TestAction("Copy");
copyaction.putValue(Action.SMALL_ICON,new ImageIcon("1.gif"));
JMenu editmenu=new JMenu("Edit");
editmenu.add(cutaction);
editmenu.add(copyaction);
editmenu.addSeparator();
JMenu optionmenu=new JMenu("Option");
optionmenu.add(readonly);
optionmenu.add(inserttiem);
optionmenu.add(overtypeitem);
filemenu.add(optionmenu);
JMenuBar menubar=new JMenuBar();
menubar.add(filemenu);
this.setJMenuBar(menubar);
//popup menu
pop=new JPopupMenu();
pop.add(cutaction);
pop.add(copyaction);
this.getContentPane().addMouseListener(new
MouseAdapter()
{
public void mousePressed(MouseEvent e){
//if(e.isPopupTrigger()){
int i=e.getButton();
if(i==3){
pop.show(e.getComponent(),e.getX(),e.getY());
}
//}
}
public void mouseReleaseed(MouseEvent e){
//if(e.isPopupTrigger())
//pop.show(e.getComponent(),e.getX(),e.getY());
}
});
}
}
class TestAction extends AbstractAction{
public TestAction(String title){
super(title);
}
public void actionPerformed(ActionEvent e){
System.out.println(this.getValue(Action.NAME)+"is selected");
}
}
还有个问题就是怎么控制弹出式的菜单,大家看到了用了个MouseEvent.getButton()
得到他是哪个鼠标按键来显示,但是按SDK中的方法,和其他的资料上有个
public void mouseReleaseed(MouseEvent e){
if(e.isPopupTrigger()) pop.show(e.getComponent(),e.getX(),e.getY());
}
大家看到我的源程序注释了这个语句。
用上面的e.getButton()去判断,,来实现,,
请问有更好的方法么,参考<<JAVA核心技术I>>资料上说的用e.isPopupTriger()方法来做,?
请指教
代码可能有点长,但是思路简单。关键就是菜单中无法显示出图标来,无法显示1.gif这个图象来。
可能这里不怎么好看,我在别的地方写了好了用JAVA代码编辑器写好了,地址:
[url=http://www.chinajavaworld.com/thread.jspa?threadID=747071&tstart=0]http://www.chinajavaworld.com/thread.jspa?threadID=747071&tstart=0[/url]
package jmenu;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MenuMain{
public static void main(String[]args){
MenuFrame frame=new MenuFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MenuFrame extends JFrame{
private Action saveaction,saveasaction;
private JCheckBoxMenuItem readonly;
private JPopupMenu pop;
public MenuFrame(){
this.setTitle("Jmenu Test");
this.setSize(400,400);
JMenu filemenu=new JMenu("File");
JMenuItem newitem=filemenu.add(new TestAction("New"));
JMenuItem openitem=filemenu.add(new TestAction("Open"));
openitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
filemenu.addSeparator();
saveaction=new TestAction("Save");
JMenuItem saveitem=filemenu.add(saveaction);
saveitem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_DOWN_MASK));
saveasaction=new TestAction("Save as");
filemenu.add(saveasaction);
filemenu.addSeparator();
filemenu.add(new
AbstractAction("Exit"){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
);
readonly=new JCheckBoxMenuItem("Read-only");
readonly.addActionListener(new
ActionListener(){
public void actionPerformed(ActionEvent e){
boolean saveok=! readonly.isSelected();
saveaction.setEnabled(saveok);
saveasaction.setEnabled(saveok);
}
}
);
ButtonGroup buttongroup=new ButtonGroup();
JRadioButtonMenuItem inserttiem=new JRadioButtonMenuItem("Insert",true);
JRadioButtonMenuItem overtypeitem=new JRadioButtonMenuItem("overtype",false);
buttongroup.add(inserttiem);
buttongroup.add(overtypeitem);
Action cutaction=new TestAction("Cut");
cutaction.putValue(Action.SMALL_ICON,new ImageIcon("1.gif"));//本来这个1.gif就要显示在菜单中啊。
Action copyaction=new TestAction("Copy");
copyaction.putValue(Action.SMALL_ICON,new ImageIcon("1.gif"));
JMenu editmenu=new JMenu("Edit");
editmenu.add(cutaction);
editmenu.add(copyaction);
editmenu.addSeparator();
JMenu optionmenu=new JMenu("Option");
optionmenu.add(readonly);
optionmenu.add(inserttiem);
optionmenu.add(overtypeitem);
filemenu.add(optionmenu);
JMenuBar menubar=new JMenuBar();
menubar.add(filemenu);
this.setJMenuBar(menubar);
//popup menu
pop=new JPopupMenu();
pop.add(cutaction);
pop.add(copyaction);
this.getContentPane().addMouseListener(new
MouseAdapter()
{
public void mousePressed(MouseEvent e){
//if(e.isPopupTrigger()){
int i=e.getButton();
if(i==3){
pop.show(e.getComponent(),e.getX(),e.getY());
}
//}
}
public void mouseReleaseed(MouseEvent e){
//if(e.isPopupTrigger())
//pop.show(e.getComponent(),e.getX(),e.getY());
}
});
}
}
class TestAction extends AbstractAction{
public TestAction(String title){
super(title);
}
public void actionPerformed(ActionEvent e){
System.out.println(this.getValue(Action.NAME)+"is selected");
}
}
还有个问题就是怎么控制弹出式的菜单,大家看到了用了个MouseEvent.getButton()
得到他是哪个鼠标按键来显示,但是按SDK中的方法,和其他的资料上有个
public void mouseReleaseed(MouseEvent e){
if(e.isPopupTrigger()) pop.show(e.getComponent(),e.getX(),e.getY());
}
大家看到我的源程序注释了这个语句。
用上面的e.getButton()去判断,,来实现,,
请问有更好的方法么,参考<<JAVA核心技术I>>资料上说的用e.isPopupTriger()方法来做,?
请指教