java右键菜单问题请教

2024-11-21 21:33:34
推荐回答(2个)
回答1:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;

public class PopupTest {
public PopupTest(){
final JFrame f=new JFrame("弹出菜单测试");
f.setSize(300,400);
f.setLocation(200,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setResizable(false);

final PopupMenu popupmenu1=new PopupMenu("背景");

String[] colorstring={"红色","绿色","蓝色","青色","洋红","黄色","黑色"};

for(int i=0;i<=6;i++){
MenuItem colorItem=new MenuItem(colorstring[i]);
popupmenu1.add(colorItem);
}
f.add(popupmenu1);
f.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getButton()==MouseEvent.BUTTON3){
popupmenu1.show(f,e.getX(), e.getY());
}
}
});//这个地方错了。

}

public static void main(String[] args){
PopupTest p=new PopupTest();

}
}
完美解决。。接分。。。

回答2:

资深JAVA专家 小班授课
企业真实项目驱动
毕业即就业 最低起薪3500
零学费入学 详情搜索“疯狂JAVA”