java 鼠标右键实现弹出菜单
//为组件定制鼠标右键弹出式菜单classFontPopupextendsJPopupMenuimplementsActionListener{String[]item;...
//为组件定制鼠标右键弹出式菜单
class FontPopup extends JPopupMenu implements ActionListener {
String[] item;
Font[] font;
JMenuItem mt;
JTextArea t;
FontPopup(JTextArea t) {
this.t = t;
// 下面语句给菜单设置立体边框
this.setBorder(new BevelBorder(BevelBorder.RAISED));
item = new String[] { "标准仿宋", "粗放行楷", "规矩新魏", "精致宋体", "灵感隶书" };
font = new Font[] { new Font("仿宋", 0, 18), new Font("华文行楷", 0, 30), new Font("华文新魏", 0, 22),
new Font("宋体", 0, 14), new Font("隶书", 0, 20) };
for (int i = 0; i < item.length; i++) {
mt = new JMenuItem(item[i]);
mt.setFont(new Font("仿宋", 0, 14));
this.add(mt);
mt.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
int i;
for (i = 0; i < item.length; i++)
if (s.equals(item[i]))
break;
t.setFont(font[i]);
}
}
class JTextAreaWithMenu extends JTextArea implements MouseListener {
FontPopup fp;
JTextAreaWithMenu() {
fp = new FontPopup(this);
this.addMouseListener(this);
this.add(fp);
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == e.BUTTON3)
fp.show(this, e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
代码有什么问题啊,运行点击鼠标右键没反应 展开
class FontPopup extends JPopupMenu implements ActionListener {
String[] item;
Font[] font;
JMenuItem mt;
JTextArea t;
FontPopup(JTextArea t) {
this.t = t;
// 下面语句给菜单设置立体边框
this.setBorder(new BevelBorder(BevelBorder.RAISED));
item = new String[] { "标准仿宋", "粗放行楷", "规矩新魏", "精致宋体", "灵感隶书" };
font = new Font[] { new Font("仿宋", 0, 18), new Font("华文行楷", 0, 30), new Font("华文新魏", 0, 22),
new Font("宋体", 0, 14), new Font("隶书", 0, 20) };
for (int i = 0; i < item.length; i++) {
mt = new JMenuItem(item[i]);
mt.setFont(new Font("仿宋", 0, 14));
this.add(mt);
mt.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
int i;
for (i = 0; i < item.length; i++)
if (s.equals(item[i]))
break;
t.setFont(font[i]);
}
}
class JTextAreaWithMenu extends JTextArea implements MouseListener {
FontPopup fp;
JTextAreaWithMenu() {
fp = new FontPopup(this);
this.addMouseListener(this);
this.add(fp);
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == e.BUTTON3)
fp.show(this, e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
代码有什么问题啊,运行点击鼠标右键没反应 展开
2016-06-20
展开全部
JPopupMenu 不需要继承,,,,,,,把问题复杂化了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询