{
if(e.getButton() == MouseEvent.BUTTON3)//点击右键
popUpMenu.show(editMenu, e.getX(), e.getY())
//popUpMenu是一个JPopupMenu editMenu是一个JMenu
//e.getX(), e.getY()是获得鼠标点击的坐标 在点击的地方d出一个 editMenu,editMenu中的 内容自己添加
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1)//点击鼠标左键
popUpMenu.setVisible(false)//d出菜单消失
}
根据你的要求,改完的程序如下:(改动的地方见注释,未写入的程序不需要改动)主要是把JTextArea text改成JTextArea text=new JTextArea()
加了右击d出菜单一段。
import java.awt.*import java.awt.event.*
import javax.swing.*
import java.io.*
public class jishiben extends JFrame{
File file=null
private JLabel stateBar
private JTextArea text=new JTextArea()//这里把JTextArea text改成JTextArea text=new JTextArea()
private JMenuBar mb
private JMenu files, edit, help
private JMenuItem Open,New,Save,SaveAs,Exit,Cut,Copy,Paste,Delete,SelectAll,About
public jishiben()
//构造函数
{
super("我的记事本")
//菜单栏
mb=new JMenuBar()
files=new JMenu("文件")
edit=new JMenu("编辑")
help=new JMenu("帮助")
About=new JMenuItem("关于")
Open=new JMenuItem("打开") //文件菜单的下拉菜单
New=new JMenuItem("新建")
Save=new JMenuItem("保存")
SaveAs=new JMenuItem("另存为")
Exit=new JMenuItem("退出")
Cut=new JMenuItem("剪切") //编辑菜单的下拉菜单
Copy=new JMenuItem("复制")
Paste=new JMenuItem("粘贴")
Delete=new JMenuItem("删除")
SelectAll=new JMenuItem("全选")
stateBar=new JLabel("未修改")
//右击d出菜单 //这里加一段
JPopupMenu menu = new JPopupMenu()
JMenuItem menuItemCut = new JMenuItem("剪切")
JMenuItem menuItemCopy = new JMenuItem("复制")
JMenuItem menuItemDelete = new JMenuItem("删除")
menu.add(menuItemCut)
menu.add(menuItemCopy)
menu.add(menuItemDelete)
text.setComponentPopupMenu(menu)
menuItemCut.addActionListener(new actionListener())
menuItemCopy.addActionListener(new actionListener())
menuItemDelete.addActionListener(new actionListener())
//添加标签
stateBar.setHorizontalAlignment(SwingConstants.LEFT)
stateBar.setBorder(BorderFactory.createEtchedBorder())
mb.add(files)mb.add(edit)mb.add(help)
files.add(New)files.add(Open)files.add(Save)files.add(SaveAs)files.addSeparator() //分割线
files.add(Exit)
edit.add(Cut)edit.add(Copy)edit.add(Paste)edit.add(Delete) //分割线
edit.add(SelectAll)
help.add(About)
setJMenuBar(mb)
//text=new JTextArea() //这里去掉一句移到上面
add(new JScrollPane(text))//滚动面板
setSize(600,500)
setVisible(true)
}
class actionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
public static void main(String[] args) {
new jishiben()
}
}
import javax.swing.*import java.awt.*
import java.awt.event.*
public class ExamplePop {
JFrame frame
JTextAreaMenu textmenu
private JScrollPane pane
public ExamplePop(){
init()
}
private void init(){
frame =new JFrame("测试右键菜单")
textmenu=new JTextAreaMenu()
pane =new JScrollPane(textmenu)
frame.add(pane)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setSize(300, 200)
frame.setVisible(true)
}
public static void main(String[] args){
try { // 使用Windows的界面风格
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
} catch (Exception e) {
e.printStackTrace()
}
new ExamplePop()
}
class JTextAreaMenu extends JTextArea implements MouseListener{
private JPopupMenu pop = null// d出菜单
private JMenuItem copy = null, paste = null, cut = null// 三个功能菜单
public JTextAreaMenu() {
super()
init()
}
private void init() {
this.addMouseListener(this)
pop = new JPopupMenu()
pop.add(copy = new JMenuItem("复制"))
pop.add(paste = new JMenuItem("粘贴"))
pop.add(cut = new JMenuItem("剪切"))
this.add(pop)
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
pop.show(this, e.getX(), e.getY())
}
}
public void mouseReleased(MouseEvent e) {
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)