java做记事本,怎么实现复制粘贴功能南,
1个回答
展开全部
public boolean saveDialog() //保存
{
FileDialog filedialog=new FileDialog(this,"SaveAs",FileDialog.SAVE);
filedialog.setFile(this.file.getName());
filedialog.setVisible(true);
if((filedialog.getDirectory()!=null)&&(filedialog.getFile()!=null))
{
this.file=new File(filedialog.getDirectory(),filedialog.getFile()+".txt");
return true;
}
else
return false;
}
if(e.getActionCommand()=="chazhao")//查找
{
try
{
final JDialog jd=new JDialog(this,"",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridwidth=1;
gbc.gridheight=1;
jd.getContentPane().setLayout(gbl);
jd.setSize(380,100);
jd.setResizable(false);
final JTextField jtf=new JTextField(15);
JLabel jlFind=new JLabel("neinong:");
jd.getContentPane().add(jlFind);
JButton jbFind=new JButton("chazhao");
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=textarea.getText();
String strB=jtf.getText();
if(a>=0)
{
a=strA.indexOf(strB,StartFindPos);
b=strB.length();
StartFindPos=a+b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "weichaozhaodao", "",1);
a=0;
StartFindPos=0;
}
textarea.select(a,StartFindPos);
}
}
}
);
JButton jbCancel=new JButton("cancel");
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.getContentPane().add(jtf);
jd.getContentPane().add(jbFind);
jd.getContentPane().add(jbCancel);
jd.setLocation(240,200);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jd.setVisible(true);
}
catch(Exception efind)
{
}
}
if(e.getActionCommand()=="fuzhi")//复制
textarea.copy();
if(e.getActionCommand()=="qianqie")//剪切
textarea.cut();
if(e.getActionCommand()=="zhantie")//粘贴
textarea.paste();
{
FileDialog filedialog=new FileDialog(this,"SaveAs",FileDialog.SAVE);
filedialog.setFile(this.file.getName());
filedialog.setVisible(true);
if((filedialog.getDirectory()!=null)&&(filedialog.getFile()!=null))
{
this.file=new File(filedialog.getDirectory(),filedialog.getFile()+".txt");
return true;
}
else
return false;
}
if(e.getActionCommand()=="chazhao")//查找
{
try
{
final JDialog jd=new JDialog(this,"",true);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
gbc.weightx=0.5;
gbc.weighty=0.5;
gbc.gridwidth=1;
gbc.gridheight=1;
jd.getContentPane().setLayout(gbl);
jd.setSize(380,100);
jd.setResizable(false);
final JTextField jtf=new JTextField(15);
JLabel jlFind=new JLabel("neinong:");
jd.getContentPane().add(jlFind);
JButton jbFind=new JButton("chazhao");
jbFind.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent efind)
{
String strA=textarea.getText();
String strB=jtf.getText();
if(a>=0)
{
a=strA.indexOf(strB,StartFindPos);
b=strB.length();
StartFindPos=a+b;
if(a==-1)
{
JOptionPane.showMessageDialog(null, "weichaozhaodao", "",1);
a=0;
StartFindPos=0;
}
textarea.select(a,StartFindPos);
}
}
}
);
JButton jbCancel=new JButton("cancel");
jbCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ejb)
{
jd.dispose();
}
}
);
jd.getContentPane().add(jtf);
jd.getContentPane().add(jbFind);
jd.getContentPane().add(jbCancel);
jd.setLocation(240,200);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jd.setVisible(true);
}
catch(Exception efind)
{
}
}
if(e.getActionCommand()=="fuzhi")//复制
textarea.copy();
if(e.getActionCommand()=="qianqie")//剪切
textarea.cut();
if(e.getActionCommand()=="zhantie")//粘贴
textarea.paste();
更多追问追答
追问
这么多啊
追答
你可以只要复制和粘贴的功能,其他代码省略,根据需求筛选
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询