< buttons.length; i++) toolbar.add(buttons[i]); buttons[0].setToolTipText("复制"); buttons[0].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.copy(); } }); buttons[1].setToolTipText("剪切"); buttons[1].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.cut(); } }); buttons[2].setToolTipText("粘贴"); buttons[2].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { text.paste(); } }); this.getContentPane().add(toolbar, BorderLayout.NORTH); } public static void main(String args[]) {。
2. 如何用java写一个简单的记事本 a.awt.event.*;
import javax.swing.JOptionPane;
/**
*功能:简单记事本
*2011-12-25
*/
public class SimpleNotepad implements ActionListener{
【怎么用Java写记事本】private Frame frame;
private FileDialog fd_load;
private FileDialog fd_save;
private TextArea ta;
private String file = "";
private MenuItem save;
private RandomAccessFile raf;
private FileChannel fci;
private FileLock flock;
3. 如何用JAVA编写简单的记事本程序 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.awt.datatransfer.*; public class NewEdit { public static void main(String args[]) { MyFrame EditFrame=new MyFrame(); EditFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); EditFrame.setVisible(true); } } class MyFrame extends JFrame { public MyFrame() { Dimension screenSize=toolKit.getScreenSize(); int screenHeight=screenSize.height; int screenWidth=screenSize.width; setSize(screenWidth/2,screenHeight/2); setLocation(screenWidth/4,screenHeight/4); Image img=toolKit.getImage("icon.gif"); setIconImage(img); setTitle("MyEdit"); JMenuBar menuBar=new JMenuBar(); setJMenuBar(menuBar); JMenu fileMenu=new JMenu("文件"); JMenu editMenu=new JMenu("编辑"); JMenu helpMenu=new JMenu("帮助"); menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(helpMenu); JMenuItem newItem=new JMenuItem("新建"); newItem.addActionListener(new NewListener()); JMenuItem openItem=new JMenuItem("打开"); openItem.addActionListener(new OpenListener()); JMenuItem saveItem=new JMenuItem("保存"); saveItem.addActionListener(new SaveListener()); JMenuItem escItem=new JMenuItem("退出"); escItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent myE) { System.exit(0); } }); fileMenu.add(newItem); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.add(escItem); JMenuItem allItem=new JMenuItem("全选"); //JMenuItem copyItem=new JMenuItem("复制"); //CopyAction copyAction=new CopyAction(); //copyItem.addActionListener(copyAction); JMenuItem cutItem=new JMenuItem("剪切"); //JMenuItem pasteItem=new JMenuItem("粘贴"); //Action pasteAction=new PasteAction(); //pasteItem.addActionListener(pasteAction); editMenu.add(allItem); allItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent myE) { textArea.selectAll(); } }); CopyAction copyAction=new CopyAction("剪切"); editMenu.add(copyAction); CutAction cutAction=new CutAction("复制"); editMenu.add(cutAction); Action pasteAction=new PasteAction("粘贴"); editMenu.add(pasteAction); popup=new JPopupMenu(); popup.add(copyAction); popup.add(pasteAction); popup.add(cutAction); textArea = new JTextArea(); textArea.add(popup); textArea.addMouseListener(new MouseListener(){ 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) { popup.show(textArea, e.getX(), e.getY()); } } public void mouseReleased(MouseEvent e) {} });//可尝试用MouseAdapter,代码可能更简单 JScrollPane scroller = new JScrollPane(textArea); add(scroller); //OR--this.getContentPane().add(scroller) //scroller.setComponentPopupMenu(popup); //textArea.setInheritsPopupMenu(true); //scroller.addMouseListener(new MouseAdapter(){}); } public void writeFile(String fileName) { try { File file = new File(fileName); FileWriter write = new FileWriter(file); write.write(textArea.getText()); write.close(); } catch(Exception e){ System.out.println("Error closing file!"); } } public void openFile(String fileName) { try { File file = new File(fileName); FileReader readIn = new FileReader(file); int size = (int)file.length(); int charsRead = 0; char[] content = new char[size]; while(readIn.ready()) charsRead += readIn.read(content,charsRead,size-charsRead); readIn.close(); textArea.setText(new String(content,0,charsRead)); } catch(IOException e) { System.out.println("Error opening file!"); } } private class NewListener implements ActionListener { public void actionPerformed(ActionEvent myE) { textArea.setText(""); } } private class OpenListener implements ActionListener { public void actionPerformed(ActionEvent myE) { openFileDialog.setVisible(true); fileName = openFileDialog.getDirectory()+openFileDialog.getFile(); if(fileName != null) { openFile(fileName); } } } private class SaveListener implements ActionListener { public void actionPerformed(ActionEvent myE) { saveFileDialog.setVisible(true); fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile(); if(fileName !=null) { writeFile(fileName); } } } private class CutAction extends AbstractAction { public CutAction(String name) { super(name); } public void actionPerformed(ActionEvent event) { String text = textArea.getSelectedText();。