我給你如下:/*
創(chuàng)新互聯(lián)建站是一家專業(yè)提供絳縣企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、HTML5建站、小程序制作等業(yè)務(wù)。10年已為絳縣眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
* WriteBoard.java
*
* Created on 2006年12月19日, 下午7:26
*/
/**
*
* @author LecH.giF
*/
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.awt.FileDialog;
public class WriteBoard extends java.awt.Frame {
Clipboard clipboard =null;
FileDialog fc = new FileDialog(this);
/** Creates new form WriteBoard */
public WriteBoard() {
clipboard = getToolkit().getSystemClipboard();
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// editor-fold defaultstate="collapsed" desc=" Generated Code "
private void initComponents() {
textArea1 = new java.awt.TextArea();
menuBar1 = new java.awt.MenuBar();
menu1 = new java.awt.Menu();
menuItem1 = new java.awt.MenuItem();
menuItem2 = new java.awt.MenuItem();
menuItem3 = new java.awt.MenuItem();
menuItem4 = new java.awt.MenuItem();
menuItem5 = new java.awt.MenuItem();
menu2 = new java.awt.Menu();
menuItem6 = new java.awt.MenuItem();
menuItem7 = new java.awt.MenuItem();
menuItem8 = new java.awt.MenuItem();
setTitle("WriteBoard");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
add(textArea1, java.awt.BorderLayout.CENTER);
menu1.setLabel("Menu");
menuItem1.setLabel("\u65b0\u5efa");
menuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
newText(evt);
}
});
menu1.add(menuItem1);
menuItem2.setLabel("\u6253\u5f00");
menuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
open(evt);
}
});
menu1.add(menuItem2);
menuItem3.setLabel("\u4fdd\u5b58");
menuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem3ActionPerformed(evt);
}
});
menu1.add(menuItem3);
menuItem4.setLabel("\u53e6\u5b58\u4e3a");
menuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem4ActionPerformed(evt);
}
});
menu1.add(menuItem4);
menuItem5.setLabel("\u9000\u51fa");
menuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exit(evt);
}
});
menu1.add(menuItem5);
menuBar1.add(menu1);
menu2.setLabel("\u7f16\u8f91");
menuItem6.setLabel("\u526a\u5207");
menuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem6ActionPerformed(evt);
}
});
menu2.add(menuItem6);
menuItem7.setLabel("\u590d\u5236");
menuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem7ActionPerformed(evt);
}
});
menu2.add(menuItem7);
menuItem8.setLabel("\u7c98\u8d34");
menuItem8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
menuItem8ActionPerformed(evt);
}
});
menu2.add(menuItem8);
menuBar1.add(menu2);
setMenuBar(menuBar1);
pack();
}// /editor-fold
private void menuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
fc.show();
if(fc.getFile()!=null){
File file = new File(fc.getFile());
try {
PrintWriter pw = new PrintWriter(file);
pw.print(textArea1.getText());
pw.flush();
pw.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
else{
return;
}
}
private void menuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
fc.show();
if(fc.getFile()!=null){
File file = new File(fc.getFile());
try {
PrintWriter pw = new PrintWriter(file);
pw.print(textArea1.getText());
pw.flush();
pw.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
else{
return;
}
}
private void menuItem8ActionPerformed(java.awt.event.ActionEvent evt) {
Transferable contents = clipboard.getContents(this);
DataFlavor flavor = DataFlavor.stringFlavor;
if(contents.isDataFlavorSupported(flavor))
try{
String str;
str=(String)contents.getTransferData(flavor);
textArea1.append(str);
}catch(Exception e){}
}
private void menuItem7ActionPerformed(java.awt.event.ActionEvent evt) {
String temp = this.textArea1.getSelectedText();
StringSelection text = new StringSelection(temp);
clipboard.setContents(text,null);
}
private void menuItem6ActionPerformed(java.awt.event.ActionEvent evt) {
String temp = this.textArea1.getSelectedText();
StringSelection text = new StringSelection(temp);
clipboard.setContents(text,null);
int start = textArea1.getSelectionStart();
int end = textArea1.getSelectionEnd();
textArea1.replaceRange("",start,end);
}
private void open(java.awt.event.ActionEvent evt) {
fc.show();
if(fc.getFile()!=null){
File file = new File(fc.getFile());
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s;
try {
while((s= br.readLine())!=null){
textArea1.append(s+"\n");
}
fr.close();
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
else{
return;
}
}
private void newText(java.awt.event.ActionEvent evt) {
this.textArea1.setText("");
}
private void exit(java.awt.event.ActionEvent evt) {
System.exit(0);
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WriteBoard().setVisible(true);
}
});
}
// Variables declaration - do not modify
private java.awt.Menu menu1;
private java.awt.Menu menu2;
private java.awt.MenuBar menuBar1;
private java.awt.MenuItem menuItem1;
private java.awt.MenuItem menuItem2;
private java.awt.MenuItem menuItem3;
private java.awt.MenuItem menuItem4;
private java.awt.MenuItem menuItem5;
private java.awt.MenuItem menuItem6;
private java.awt.MenuItem menuItem7;
private java.awt.MenuItem menuItem8;
private java.awt.TextArea textArea1;
// End of variables declaration
}
給你個(gè)做好了的Java的源程序的記事本,自己看看就行了的,不怎么難的···
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class MyNotepad implements ActionListener{
private JFrame frame=new JFrame("新記事本");
private JTextArea jta=new JTextArea();
private String result="";
private boolean flag=true;
private File f;
private JButton jb=new JButton("開始");
private JTextField jtf=new JTextField(15);
private JTextField jt=new JTextField(15);
private JButton jbt=new JButton("替換為");
private JButton jba=new JButton("全部替換");
private Icon ic=new ImageIcon("D:\\java課堂筆記\\GUI\\11.gif");
private String value;
private int start=0;
private JFrame jf=new JFrame("查找");
private JFrame jfc=new JFrame("替換");
@Override
public void actionPerformed(ActionEvent e) {
String comm=e.getActionCommand();
if("新建".equals(comm)){
if(!(frame.getTitle().equals("新記事本"))){
if(!flag){
write();
newNew();
}else{
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語(yǔ)句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
}
}
}else if(!(jta.getText().isEmpty())){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語(yǔ)句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
newNew();
}
}else{
newNew();
}
}else if("打開".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
jfc.setDialogType(JFileChooser.OPEN_DIALOG);
int returnVal = jfc.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語(yǔ)句,保證了程序的健壯性
f=jfc.getSelectedFile();
frame.setTitle(f.getName());
result=read();
flag=false;
value=result;
jta.setText(result);
}
}else if("保存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
if(flag){
int returnVal = jfc.showDialog(null,"保存為");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語(yǔ)句,保證了程序的健壯性
f=jfc.getSelectedFile();
flag=false;
write();
}
}else{
write();
}
}else if("另存".equals(comm)){
JFileChooser jfc=new JFileChooser("D:\\java課堂筆記");
int returnVal = jfc.showDialog(null,"另存");
if(returnVal == JFileChooser.APPROVE_OPTION) {//選擇文件后再執(zhí)行下面的語(yǔ)句,保證了程序的健壯性
f=jfc.getSelectedFile();
write();
}
}else if("退出".equals(comm)){
System.exit(0);
}else if("撤銷".equals(comm)){
jta.setText(value);
}else if("剪切".equals(comm)){
value=jta.getText();
jta.cut();
}else if("復(fù)制".equals(comm)){
jta.copy();
}else if("粘貼".equals(comm)){
value=jta.getText();
jta.paste();
}else if("刪除".equals(comm)){
value=jta.getText();
jta.replaceSelection(null);
}else if("全選".equals(comm)){
jta.selectAll();
}else if("查找".equals(comm)){
value=jta.getText();
jf.add(jtf,BorderLayout.CENTER);
jf.add(jb,BorderLayout.SOUTH);
jf.setLocation(300,300);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("替換".equals(comm)){
value=jta.getText();
GridLayout gl=new GridLayout(3,3);
JLabel jl1=new JLabel("查找內(nèi)容:");
JLabel jl2=new JLabel("替換為:");
jfc.setLayout(gl);
jfc.add(jl1);
jfc.add(jtf);
jfc.add(jb);
jfc.add(jl2);
jfc.add(jt);
jfc.add(jbt);
JLabel jl3=new JLabel();
JLabel jl4=new JLabel();
jfc.add(jl3);
jfc.add(jl4);
jfc.add(jba);
jfc.setLocation(300,300);
jfc.pack();
jfc.setVisible(true);
jfc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}else if("版本".equals(comm)){
JDialog jd=new JDialog(frame,"關(guān)于對(duì)話框");
jd.setSize(200,200);
JLabel l=new JLabel("哈哈哈哈哈哈哈哈哈哈呵呵呵呵呵呵呵呵呵呵呵呵呵");
jd.add(l,BorderLayout.CENTER);
jd.setLocation(100,200);
jd.setSize(300,300);
jd.setVisible(true);
// jd.pack();
jd.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
}else if("開始".equals(comm)||"下一個(gè)".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jb.setText("下一個(gè)");
// value=value.substring(s+temp.length());//不能截取字串
}else {
JOptionPane.showMessageDialog(jf, "查找完畢!", "提示", 0, ic);
jf.dispose();
}
}else if("替換為".equals(comm)){
String temp=jtf.getText();
int s=value.indexOf(temp,start);
if(value.indexOf(temp,start)!=-1){
jta.setSelectionStart(s);
jta.setSelectionEnd(s+temp.length());
jta.setSelectedTextColor(Color.GREEN);
start=s+1;
jta.replaceSelection(jt.getText());
}else {
JOptionPane.showMessageDialog(jf, "查找完畢!", "提示", 0, ic);
jf.dispose();
}
}else if("全部替換".equals(comm)){
String temp=jta.getText();
temp=temp.replaceAll(jtf.getText(), jt.getText());
jta.setText(temp);
}
}
public String read(){
String temp="";
try {
FileInputStream fis = new FileInputStream(f.getAbsolutePath());
byte[] b=new byte[1024];
while(true){
int num=fis.read(b);
if(num==-1)break;
temp=temp+new String(b,0,num);
}
fis.close();
} catch (Exception e1) {
e1.printStackTrace();
}
return temp;
}
public void write(){
try {
FileOutputStream fos=new FileOutputStream(f);
fos.write(jta.getText().getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void newNew(){
frame.dispose();
new MyNotepad();
flag=true;
}
public MyNotepad(){
JMenuBar jmb=new JMenuBar();
String[] menuLab={"文件","編輯","幫助"};
String[][] menuItemLab={{"新建","打開","保存","另存","退出"},
{"撤銷","剪切","復(fù)制","粘貼","刪除","全選","查找","替換"},
{"版本"}};
for(int i=0;imenuLab.length;i++){
JMenu menu=new JMenu(menuLab[i]);
jmb.add(menu);
for(int j=0;jmenuItemLab[i].length;j++){
JMenuItem jmi=new JMenuItem(menuItemLab[i][j]);
menu.add(jmi);
jmi.addActionListener(this);
}
}
frame.setJMenuBar(jmb);
jta.setLineWrap(true);//自動(dòng)換行
JScrollPane jsp=new JScrollPane(jta);//滾動(dòng)窗口面板
frame.add(jsp);
jb.addActionListener(this);
jbt.addActionListener(this);
jba.addActionListener(this);
frame.setLocation(200,50);
frame.setSize(620,660);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new MyNotepad();
}
}
用記事本寫完代碼后運(yùn)行方法如下:
1、用瀏覽器打開用記事本編寫的代碼
新建“文本文檔”后,鼠標(biāo)右鍵點(diǎn)擊該文本文檔,在菜單欄的“打開方式”選擇“用記事本打開”,也可以設(shè)置默認(rèn)打開方式為“記事本”;用記事本打開文本文檔后,直接在該文檔內(nèi)根據(jù)自己的需要輸入想要編輯的網(wǎng)頁(yè)代碼。
2、記事本寫java代碼怎么運(yùn)行
首先,需要安裝jdk并配置環(huán)境變量。然后,在命令行中,用javac命令編譯用記事本編寫的代碼。下一步,在命令行中,用java命令執(zhí)行編譯后的結(jié)果。
代碼是什么
代碼是程序員用開發(fā)工具所支持的語(yǔ)言寫出來(lái)的源文件,是一組由字符、符號(hào)或信號(hào)碼元以離散形式表示信息的明確的規(guī)則體系。代碼設(shè)計(jì)的原則包括唯一確定性、標(biāo)準(zhǔn)化和通用性、可擴(kuò)充性與穩(wěn)定性、便于識(shí)別與記憶、力求短小與格式統(tǒng)一以及容易修改等。
計(jì)算機(jī)源代碼最終目的是將人類可讀文本翻譯成為計(jì)算機(jī)可執(zhí)行的二進(jìn)制指令,這種過(guò)程叫編譯,它由通過(guò)編譯器完成。源代碼就是用匯編語(yǔ)言和高級(jí)語(yǔ)言寫出來(lái)的地代碼。目標(biāo)代碼是指源代碼經(jīng)過(guò)編譯程序產(chǎn)生的能被 cpu直接識(shí)別二進(jìn)制代碼。
可執(zhí)行代碼就是將目標(biāo)代碼連接后形成的可執(zhí)行文件,當(dāng)然也是二進(jìn)制的。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileFilter;
public class Demo extends JFrame {
private static final long serialVersionUID = 1L; //Eclipse自動(dòng)生成序列號(hào)
String name = "無(wú)標(biāo)題";
JPanel menuPanel = new JPanel();
JTextArea text = new TextAreaMenu(); //文本編輯區(qū)
JScrollPane jsp = new JScrollPane(text); //可滾動(dòng)編輯區(qū)
JMenuBar mnbMain = new JMenuBar();
JMenu mnServer = new JMenu("文件(F)");
JMenu mnEdit = new JMenu("編輯(E)");
JMenuItem[] mniServers = new JMenuItem[]{
new JMenuItem("新建(N)"),
new JMenuItem("保存(S)"),
new JMenuItem("打開(O)"),
new JMenuItem("退出(X)"),
};
{
menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
mnbMain.add(mnServer);
menuPanel.add(mnbMain);
mnbMain.setBounds(5, 0, 50, 30);
for (int i = 0; i mniServers.length; i++) {
mnServer.add(mniServers[i]);
}
mniServers[0].addActionListener(new ActionListener() { //定義"新建"組件操作
@Override
public void actionPerformed(ActionEvent arg0) {
new Demo(getLocation().x+15,getLocation().y+5);
}
});
mniServers[1].addActionListener(new ActionListener() { //定義"保存"組件操作
@Override
public void actionPerformed(ActionEvent arg0) {
chooseToSave();
}
});
mniServers[2].addActionListener(new ActionListener() { //定義"打開"組件操作
@Override
public void actionPerformed(ActionEvent arg0) {
chooseToOpen();
}
});
mniServers[3].addActionListener(new ActionListener() { //定義"退出"組件操作
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
text.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusGained(FocusEvent e) {
}
});
}
public Demo(int x,int y) {
this.setTitle( name +" - 記事本");
this.setBounds(x, y, 600, 400);
this.setLayout(new BorderLayout());
this.add(menuPanel, BorderLayout.NORTH);
this.add(jsp);
jsp.setBounds(5, 30, getWidth()-10, getHeight()-50);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public Demo() {
this(200,200);
}
protected void chooseToSave() {
File file = chooseFile();
if(null==file)return;
if(file.exists()){
int cho = JOptionPane.showConfirmDialog(this, "文件已存在,是否覆蓋?");
System.out.println(cho);
if(cho==JOptionPane.OK_OPTION)save(file);
else return;
}
else save(file);
}
private void save(File file) {
name = file.getName();
write(text.getText(),file.getPath());
this.setTitle( name +" - 記事本");
}
protected void chooseToOpen() {
File file = chooseFile();
if(null==file||!file.exists())return;
name = file.getName();
Demo.this.setTitle( name +" - 記事本");
read(text,file);
}
/*********************************************MAIN**************************************************/
public static void main(String[] args) {
new Demo();
}
private File chooseFile(){
JFileChooser chooser = new JFileChooser(); //構(gòu)建文件選擇器
chooser.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return "文本文件";
}
@Override
public boolean accept(File f) {
String name = f.getName().toLowerCase();
return f.isDirectory() || name.endsWith(".txt")
||name.endsWith(".c") || name.endsWith(".java")
||name.endsWith(".cpp"); //可識(shí)別文件
}
});
int result = chooser.showDialog(null, "確定");
if (result==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
} else {
System.out.println("未選擇文件");
}
return chooser.getSelectedFile();
}
public static void read(JTextArea text,File file){ //定義讀取文件操作
FileReader fr;
try {
fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String string = null;
while((string = br.readLine()) != null){
text.append(string+"\n");
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write(String txt,String fileName){
FileWriter fw;
try {
fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(txt);
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class TextAreaMenu extends JTextArea implements MouseListener {
private static final long serialVersionUID = -2308615404205560110L;
private JPopupMenu pop = null; // 彈出菜單
private JMenuItem selectAll = null,copy = null, paste = null, cut = null, cancel=null; // 功能菜單
public TextAreaMenu() {
super();
init();
}
private void init() {
this.addMouseListener(this);
this.setSelectedTextColor(Color.red);
pop = new JPopupMenu();
pop.add(selectAll = new JMenuItem("全選"));
pop.add(copy = new JMenuItem("復(fù)制"));
pop.add(paste = new JMenuItem("粘貼"));
pop.add(cut = new JMenuItem("剪切"));
pop.add(cancel = new JMenuItem("撤銷"));
selectAll.setAccelerator(KeyStroke.getKeyStroke('A', InputEvent.CTRL_MASK));
copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
cancel.setAccelerator(KeyStroke.getKeyStroke('Z', InputEvent.CTRL_MASK));
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
this.add(pop);
}
/**
* 菜單動(dòng)作
* @param e
*/
public void action(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals(selectAll.getText())) { // 全選
this.selectAll();
}
else if (str.equals(copy.getText())) { // 復(fù)制
this.copy();
} else if (str.equals(paste.getText())) { // 粘貼
this.paste();
} else if (str.equals(cut.getText())) { // 剪切
this.cut();
}
else if (str.equals(cancel.getText())) { //撤銷
this.cut();
}
}
public JPopupMenu getPop() {
return pop;
}
public void setPop(JPopupMenu pop) {
this.pop = pop;
}
/**
* 剪切板中是否有文本數(shù)據(jù)可供粘貼
*
* @return true為有文本數(shù)據(jù)
*/
public boolean isClipboardString() {
boolean b = false;
Clipboard clipboard = this.getToolkit().getSystemClipboard();
Transferable content = clipboard.getContents(this);
try {
if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {
b = true;
}
} catch (Exception e) {
}
return b;
}
/**
* 文本組件中是否具備復(fù)制的條件
*
* @return true為具備
*/
public boolean isCanCopy() {
boolean b = false;
int start = this.getSelectionStart();
int end = this.getSelectionEnd();
if (start != end)
b = true;
return b;
}
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) {
copy.setEnabled(isCanCopy());
paste.setEnabled(isClipboardString());
cut.setEnabled(isCanCopy());
pop.show(this, e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
}
}