在java程序中加个右击弹出菜单(剪切 复制 删除) 求大神帮忙 急用!!!!!!!
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.io.*;publiccla...
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;
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("未修改"); //添加标签
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);
//监听器接口
New.addActionListener(new actionListener());
Open.addActionListener(new actionListener());
Save.addActionListener(new actionListener());
SaveAs.addActionListener(new actionListener());
Exit.addActionListener(new actionListener());
Cut.addActionListener(new actionListener());
Copy.addActionListener(new actionListener());
Paste.addActionListener(new actionListener());
Delete.addActionListener(new actionListener());
SelectAll.addActionListener(new actionListener());
About.addActionListener(new actionListener());
text.addKeyListener
(new KeyAdapter()
{public void keyTyped(KeyEvent e)
{processTextArea();}
});
}
void processTextArea(){
stateBar.setText("已修改");}
//将文件读到编辑区
接下
else if(mi==SaveAs||mi==Save)
{JFileChooser jf=new JFileChooser();
if(file!=null)
jf.setSelectedFile(file);
int returnVal=jf.showSaveDialog(jishiben.this);
if(returnVal==JFileChooser.APPROVE_OPTION)
{file=jf.getSelectedFile();
save();}
}
}
}
public static void main(String args[])
{
new jishiben();
}
} 展开
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class jishiben extends JFrame
{File file=null;
private JLabel stateBar;
private JTextArea text;
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("未修改"); //添加标签
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);
//监听器接口
New.addActionListener(new actionListener());
Open.addActionListener(new actionListener());
Save.addActionListener(new actionListener());
SaveAs.addActionListener(new actionListener());
Exit.addActionListener(new actionListener());
Cut.addActionListener(new actionListener());
Copy.addActionListener(new actionListener());
Paste.addActionListener(new actionListener());
Delete.addActionListener(new actionListener());
SelectAll.addActionListener(new actionListener());
About.addActionListener(new actionListener());
text.addKeyListener
(new KeyAdapter()
{public void keyTyped(KeyEvent e)
{processTextArea();}
});
}
void processTextArea(){
stateBar.setText("已修改");}
//将文件读到编辑区
接下
else if(mi==SaveAs||mi==Save)
{JFileChooser jf=new JFileChooser();
if(file!=null)
jf.setSelectedFile(file);
int returnVal=jf.showSaveDialog(jishiben.this);
if(returnVal==JFileChooser.APPROVE_OPTION)
{file=jf.getSelectedFile();
save();}
}
}
}
public static void main(String args[])
{
new jishiben();
}
} 展开
1个回答
展开全部
根据你的要求,改完的程序如下:(改动的地方见注释,未写入的程序不需要改动)
主要是把JTextArea text;改成JTextArea text=new JTextArea();
加了右击弹出菜单一段。
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("未修改");
//右击弹出菜单 //这里加一段
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();
}
}
主要是把JTextArea text;改成JTextArea text=new JTextArea();
加了右击弹出菜单一段。
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("未修改");
//右击弹出菜单 //这里加一段
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询