java windows记事本源代码
要求记事本名字是“我自己编写的记事本""文件”里没有“页面设置”跟“打印”其他一样“编辑”里没有“撤销”没有“删除”没有“转到”其他一样“格式”里多一个“字体颜色”选择其...
要求记事本名字是“我自己编写的记事本" "文件”里没有“页面设置”跟“打印” 其他一样
“编辑”里 没有“撤销” 没有“删除” 没有“转到” 其他一样
“格式”里多一个“字体颜色”选择 其他一样
“帮助”里只有“关于记事本” 点了之后无内容
3楼哥们 你的代码编译不了 展开
“编辑”里 没有“撤销” 没有“删除” 没有“转到” 其他一样
“格式”里多一个“字体颜色”选择 其他一样
“帮助”里只有“关于记事本” 点了之后无内容
3楼哥们 你的代码编译不了 展开
6个回答
展开全部
该项目代码包括两个文件:MainFrame.java和SelectTextFile.java。
(SelectTextFile.java的代码这里就不贴了,太长了。你百度HI我吧^_^)
补充说明:我这个里面没有“新建”,但其实新建也是一样调用那个SelectTextFile.java而已,而且,新建的时候更简单,只须按照路径调用File的.createNewFile()就可以了。具体我就不添加了。有疑问的话你可以百度HI我)
1.MainFrame.java代码:
package MyProject;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.datatransfer.*;
import java.io.*;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JMenu jMenu = null;
private JMenu jMenu1 = null;
private JMenu jMenu2 = null;
private JMenuItem jMenuItem = null;
private JMenuItem jMenuItem1 = null;
private JMenuItem jMenuItem2 = null;
private JMenuItem jMenuItem3 = null;
private JLabel jLabel = null;
private JScrollPane jScrollPane = null;
private JTextArea jTextArea = null;
private JMenuItem jMenuItem4 = null;
private JMenuItem jMenuItem5 = null;
private JMenuItem jMenuItem6 = null;
private JMenuItem jMenuItem7 = null;
private static MainFrame myMainFrame=null;
private static String textstr="";///用于记录文本文件的路径
private File myFile=null;
private FileReader myrder=null;
private FileWriter mywr=null;
/**
* This is the default constructor
*/
public MainFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(412, 350);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent e) {
if(!textstr.equals("")){
try{
myFile=new File(textstr);
if(!myFile.exists()){
myFile.createNewFile();
}
myrder=new FileReader(myFile);
char[] mychar=new char[(int)myFile.length()];
myrder.read(mychar);
String tmp=new String(mychar);
jTextArea.setText(tmp);
myrder.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
}
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
myMainFrame=this;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(15, 18, 65, 18));
jLabel.setText("文件内容:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getJMenu());
jJMenuBar.add(getJMenu1());
jJMenuBar.add(getJMenu2());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
jMenu.setText("文件");
jMenu.add(getJMenuItem());
jMenu.add(getJMenuItem1());
jMenu.add(getJMenuItem2());
jMenu.add(getJMenuItem3());
}
return jMenu;
}
/**
* This method initializes jMenu1
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu1() {
if (jMenu1 == null) {
jMenu1 = new JMenu();
jMenu1.setText("编辑");
jMenu1.add(getJMenuItem4());
jMenu1.add(getJMenuItem5());
jMenu1.add(getJMenuItem6());
}
return jMenu1;
}
/**
* This method initializes jMenu2
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu2() {
if (jMenu2 == null) {
jMenu2 = new JMenu();
jMenu2.setText("帮助");
jMenu2.add(getJMenuItem7());
}
return jMenu2;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem() {
if (jMenuItem == null) {
jMenuItem = new JMenuItem();
jMenuItem.setText("打开");
jMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.myMainFrame.setEnabled(false);
SelectTextFile mysl=new SelectTextFile();
mysl.setVisible(true);
}
});
}
return jMenuItem;
}
/**
* This method initializes jMenuItem1
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem1() {
if (jMenuItem1 == null) {
jMenuItem1 = new JMenuItem();
jMenuItem1.setText("关闭");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
myFile=null;
}
catch(Exception ee){
ee.printStackTrace();
}
jTextArea.setText("");
}
});
}
return jMenuItem1;
}
/**
* This method initializes jMenuItem2
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem2() {
if (jMenuItem2 == null) {
jMenuItem2 = new JMenuItem();
jMenuItem2.setText("保存");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
String tmp=jTextArea.getText();
char[] mychar=tmp.toCharArray();
myFile.delete();
myFile.createNewFile();
mywr=new FileWriter(myFile);
mywr.write(mychar);
mywr.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
});
}
return jMenuItem2;
}
/**
* This method initializes jMenuItem3
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem3() {
if (jMenuItem3 == null) {
jMenuItem3 = new JMenuItem();
jMenuItem3.setText("退出");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jMenuItem3;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(15, 46, 371, 225));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
}
return jTextArea;
}
/**
* This method initializes jMenuItem4
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem4() {
if (jMenuItem4 == null) {
jMenuItem4 = new JMenuItem();
jMenuItem4.setText("复制");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());
}
});
}
return jMenuItem4;
}
/**
* This method initializes jMenuItem5
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem5() {
if (jMenuItem5 == null) {
jMenuItem5 = new JMenuItem();
jMenuItem5.setText("剪切");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());
jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));
}
});
}
return jMenuItem5;
}
/**
* This method initializes jMenuItem6
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem6() {
if (jMenuItem6 == null) {
jMenuItem6 = new JMenuItem();
jMenuItem6.setText("黏贴");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));
jTextArea.setText(jTextArea.getText()+(MainFrame.this.getClipboardText(MainFrame.this.getToolkit().getSystemClipboard())));
}
catch(Exception ee){
ee.printStackTrace();
}
}
});
}
return jMenuItem6;
}
/**
* This method initializes jMenuItem7
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem7() {
if (jMenuItem7 == null) {
jMenuItem7 = new JMenuItem();
jMenuItem7.setText("关于记事本");
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
////暂无代码!!
}
});
}
return jMenuItem7;
}
public static void main(String args[]){
new MainFrame();
}
public static MainFrame getMyMainFrame() {
return myMainFrame;
}
public static void setMyMainFrame(MainFrame myMainFrame) {
MainFrame.myMainFrame = myMainFrame;
}
public static String getTextstr() {
return textstr;
}
public static void setTextstr(String textstr) {
MainFrame.textstr = textstr;
}
protected static String getClipboardText(Clipboard clip) throws Exception{
Transferable clipT = clip.getContents(null);// 获取剪切板中的内容
if (clipT != null) {
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) // 检查内容是否是文本类型
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
protected static void setClipboardText(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
} // @jve:decl-index=0:visual-constraint="10,10"
(SelectTextFile.java的代码这里就不贴了,太长了。你百度HI我吧^_^)
补充说明:我这个里面没有“新建”,但其实新建也是一样调用那个SelectTextFile.java而已,而且,新建的时候更简单,只须按照路径调用File的.createNewFile()就可以了。具体我就不添加了。有疑问的话你可以百度HI我)
1.MainFrame.java代码:
package MyProject;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.datatransfer.*;
import java.io.*;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JMenu jMenu = null;
private JMenu jMenu1 = null;
private JMenu jMenu2 = null;
private JMenuItem jMenuItem = null;
private JMenuItem jMenuItem1 = null;
private JMenuItem jMenuItem2 = null;
private JMenuItem jMenuItem3 = null;
private JLabel jLabel = null;
private JScrollPane jScrollPane = null;
private JTextArea jTextArea = null;
private JMenuItem jMenuItem4 = null;
private JMenuItem jMenuItem5 = null;
private JMenuItem jMenuItem6 = null;
private JMenuItem jMenuItem7 = null;
private static MainFrame myMainFrame=null;
private static String textstr="";///用于记录文本文件的路径
private File myFile=null;
private FileReader myrder=null;
private FileWriter mywr=null;
/**
* This is the default constructor
*/
public MainFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(412, 350);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent e) {
if(!textstr.equals("")){
try{
myFile=new File(textstr);
if(!myFile.exists()){
myFile.createNewFile();
}
myrder=new FileReader(myFile);
char[] mychar=new char[(int)myFile.length()];
myrder.read(mychar);
String tmp=new String(mychar);
jTextArea.setText(tmp);
myrder.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
}
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
myMainFrame=this;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(15, 18, 65, 18));
jLabel.setText("文件内容:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getJMenu());
jJMenuBar.add(getJMenu1());
jJMenuBar.add(getJMenu2());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
jMenu.setText("文件");
jMenu.add(getJMenuItem());
jMenu.add(getJMenuItem1());
jMenu.add(getJMenuItem2());
jMenu.add(getJMenuItem3());
}
return jMenu;
}
/**
* This method initializes jMenu1
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu1() {
if (jMenu1 == null) {
jMenu1 = new JMenu();
jMenu1.setText("编辑");
jMenu1.add(getJMenuItem4());
jMenu1.add(getJMenuItem5());
jMenu1.add(getJMenuItem6());
}
return jMenu1;
}
/**
* This method initializes jMenu2
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu2() {
if (jMenu2 == null) {
jMenu2 = new JMenu();
jMenu2.setText("帮助");
jMenu2.add(getJMenuItem7());
}
return jMenu2;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem() {
if (jMenuItem == null) {
jMenuItem = new JMenuItem();
jMenuItem.setText("打开");
jMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.myMainFrame.setEnabled(false);
SelectTextFile mysl=new SelectTextFile();
mysl.setVisible(true);
}
});
}
return jMenuItem;
}
/**
* This method initializes jMenuItem1
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem1() {
if (jMenuItem1 == null) {
jMenuItem1 = new JMenuItem();
jMenuItem1.setText("关闭");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
myFile=null;
}
catch(Exception ee){
ee.printStackTrace();
}
jTextArea.setText("");
}
});
}
return jMenuItem1;
}
/**
* This method initializes jMenuItem2
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem2() {
if (jMenuItem2 == null) {
jMenuItem2 = new JMenuItem();
jMenuItem2.setText("保存");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
String tmp=jTextArea.getText();
char[] mychar=tmp.toCharArray();
myFile.delete();
myFile.createNewFile();
mywr=new FileWriter(myFile);
mywr.write(mychar);
mywr.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
});
}
return jMenuItem2;
}
/**
* This method initializes jMenuItem3
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem3() {
if (jMenuItem3 == null) {
jMenuItem3 = new JMenuItem();
jMenuItem3.setText("退出");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jMenuItem3;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(15, 46, 371, 225));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
}
return jTextArea;
}
/**
* This method initializes jMenuItem4
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem4() {
if (jMenuItem4 == null) {
jMenuItem4 = new JMenuItem();
jMenuItem4.setText("复制");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());
}
});
}
return jMenuItem4;
}
/**
* This method initializes jMenuItem5
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem5() {
if (jMenuItem5 == null) {
jMenuItem5 = new JMenuItem();
jMenuItem5.setText("剪切");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());
jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));
}
});
}
return jMenuItem5;
}
/**
* This method initializes jMenuItem6
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem6() {
if (jMenuItem6 == null) {
jMenuItem6 = new JMenuItem();
jMenuItem6.setText("黏贴");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));
jTextArea.setText(jTextArea.getText()+(MainFrame.this.getClipboardText(MainFrame.this.getToolkit().getSystemClipboard())));
}
catch(Exception ee){
ee.printStackTrace();
}
}
});
}
return jMenuItem6;
}
/**
* This method initializes jMenuItem7
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem7() {
if (jMenuItem7 == null) {
jMenuItem7 = new JMenuItem();
jMenuItem7.setText("关于记事本");
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
////暂无代码!!
}
});
}
return jMenuItem7;
}
public static void main(String args[]){
new MainFrame();
}
public static MainFrame getMyMainFrame() {
return myMainFrame;
}
public static void setMyMainFrame(MainFrame myMainFrame) {
MainFrame.myMainFrame = myMainFrame;
}
public static String getTextstr() {
return textstr;
}
public static void setTextstr(String textstr) {
MainFrame.textstr = textstr;
}
protected static String getClipboardText(Clipboard clip) throws Exception{
Transferable clipT = clip.getContents(null);// 获取剪切板中的内容
if (clipT != null) {
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) // 检查内容是否是文本类型
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
protected static void setClipboardText(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
} // @jve:decl-index=0:visual-constraint="10,10"
展开全部
import java.awt.FlowLayout;
import java.awt.event.*;
import java.io.*;
import java.util.Date;
import javax.swing.*;
import cn.com.bj.*;
public class TestNotepad extends JFrame implements ActionListener, MouseListener{
private int set = 0; //查找位置
private JFrame jf = new JFrame("Notepad"); //面板
private JTextArea jta = new JTextArea(20, 30); //文本区
JScrollPane jsp = new JScrollPane(jta); //滚动条
private JTextField jtf = new JTextField(10); //单行文本区
private JTextField jtft = new JTextField(10); //单行文本区
//接收发送过来的请求,并做相应处理
public void readText(String path){
//list.clear();
FileInputStream fis;
try {
fis = new FileInputStream(path);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
jta.setText("");
String temp=br.readLine();
while(temp!=null){
jta.append(temp+"\n");
temp=br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String readPath(){
//JFrame frm=new JFrame();
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setVisible(true);
int i=chooser.showOpenDialog(this);
String path=null;
if(i==JFileChooser.APPROVE_OPTION){
path=chooser.getSelectedFile().getPath();
return path;
}else
return null;
}
public String savePath(){
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setVisible(true);
int i=chooser.showSaveDialog(this);
String path=null;
if(i==JFileChooser.APPROVE_OPTION){
path=chooser.getSelectedFile().getPath();
return path;
}else
return null;
}
public boolean writer(String paths){
//String paths=savePath();
FileWriter fw;
try {
fw = new FileWriter(paths);
fw.write(jta.getText());
fw.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand(); //
if (comm.equals("新建...")) {
new TestNotepad();
//jta.setText("");
}
if (comm.equals("保存")) {
System.out.println("保存");
String saveStr=this.savePath();
System.out.println(this.writer(saveStr));
}
if (comm.equals("日期")){
jta.append(new Date().toString());
}
if (comm.equals("打开...")){
readText(readPath());
}
if (comm.equals("退出")){
System.exit(0);
}
if (comm.equals("剪切")) {
jta.cut();
}
if (comm.equals("复制")) {
jta.copy();
}
if (comm.equals("粘贴")) {
jta.paste();
}
if (comm.equals("删除")) {
jta.cut();
}
if(comm.equals("颜色选择")){
jta.setForeground(Util.setColor());
}
if ("查找...".equals(comm) || "替换...".equals(comm)) {
JDialog jd = new JDialog(jf, comm);
String label = comm.substring(0, 2);
JButton jb = new JButton(label);
jb.addActionListener(this);
jd.setLayout(new FlowLayout());
jd.add(jtf);
if ("替换...".equals(comm)) {
JLabel jlt = new JLabel(">");
jd.add(jlt);
jd.add(jtft);
}
jd.add(jb);
if ("替换...".equals(comm)) {
JButton jball = new JButton("替换全部");
jball.addActionListener(this);
jd.add(jball);
}
jd.pack();
jd.setResizable(false);
jd.setLocation(400, 240);
jd.setVisible(true);
}
if (comm.equals("全选")) {
jta.selectAll();
}
if (comm.equals("关于...")) {
JDialog jd = new JDialog(jf, "关于...");
jd.setLayout(new FlowLayout());
JLabel jl = new JLabel("制作者: xxxxxxxx");
jd.add(jl);
jd.setSize(180, 100);
jd.setResizable(false);
jd.setLocation(400, 240);
jd.setVisible(true);
}
if (comm.equals("查找")) {
String str = jtf.getText();
String strall = jta.getText();
int n = strall.indexOf(str, set);
if (n != -1) {
set = n + 1;
jta.setSelectionStart(n);
jta.setSelectionEnd(n + str.length());
}
}
if (comm.equals("替换") || comm.equals("替换全部")) {
String str = jtf.getText();
String strall = jta.getText();
if (comm.equals("替换全部")) {
int n1 = 0;
while (true) {
set = strall.indexOf(str, n1);
if (set == -1)
break;
n1 = set + 1;
jta.setSelectionStart(set);
jta.setSelectionEnd(set + str.length());
jta.replaceSelection(jtft.getText());
}
set = 0;
}
int n = strall.indexOf(str, set);
if (n == -1) {
set = 0;
} else {
set = n + 1;
jta.setSelectionStart(n);
jta.setSelectionEnd(n + str.length());
jta.replaceSelection(jtft.getText());
}
}
}
public TestNotepad() {
jta.setLineWrap(true);
jta.addMouseListener(this);
jf.add(jsp);
JMenuBar jmb = new JMenuBar();
JMenu jm1 = new JMenu("文件");
String[] label1 = { "新建...", "打开...", "保存", "退出" };
JMenuItem[] jmi1 = new JMenuItem[label1.length];
for (int i = 0; i < jmi1.length; i++) {
jmi1[i] = new JMenuItem(label1[i]);
jm1.add(jmi1[i]);
jmi1[i].addActionListener(this);
}
jm1.insertSeparator(1);
jm1.insertSeparator(3);
jm1.insertSeparator(6);
JMenu jm2 = new JMenu("编辑");
String[] label2 = { "剪切", "复制", "粘贴", "删除", "查找...", "替换...",
"全选","颜色选择","日期" };
JMenuItem[] jmi2 = new JMenuItem[label2.length];
for (int i = 0; i < jmi2.length; i++) {
jmi2[i] = new JMenuItem(label2[i]);
jm2.add(jmi2[i]);
jmi2[i].addActionListener(this);
}
jm2.insertSeparator(1);
jm2.insertSeparator(5);
jm2.insertSeparator(7);
jm2.insertSeparator(10);
JMenu jm3 = new JMenu("帮助");
JMenuItem jmi3 = new JMenuItem("关于...");
jm3.add(jmi3);
jmi3.addActionListener(this);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jf.setJMenuBar(jmb);
jf.pack();
jf.setLocation(300, 200);
jf.setVisible(true);
//jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
new TestNotepad();
}
//鼠标右键功能
public void mouseClicked(MouseEvent e) {
JPopupMenu pp = new JPopupMenu();
if (e.getButton() == 3) {
JMenuItem jmi1,jmi2,jmi3,jmi4;
pp.add(jmi1 = new JMenuItem("剪切"));
pp.add(jmi2 = new JMenuItem("复制"));
pp.add(jmi3 = new JMenuItem("粘贴"));
pp.addSeparator();
pp.add(jmi4 = new JMenuItem("全选"));
jmi1.addActionListener(this);
jmi2.addActionListener(this);
jmi3.addActionListener(this);
jmi4.addActionListener(this);
pp.pack();
jf.add(pp);
pp.setVisible(true);
pp.show(e.getComponent(), e.getX(), e.getY());
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
关于撤消操作部分可参见%JAVA_HOME%\demo\jfc\Notepad
import java.awt.event.*;
import java.io.*;
import java.util.Date;
import javax.swing.*;
import cn.com.bj.*;
public class TestNotepad extends JFrame implements ActionListener, MouseListener{
private int set = 0; //查找位置
private JFrame jf = new JFrame("Notepad"); //面板
private JTextArea jta = new JTextArea(20, 30); //文本区
JScrollPane jsp = new JScrollPane(jta); //滚动条
private JTextField jtf = new JTextField(10); //单行文本区
private JTextField jtft = new JTextField(10); //单行文本区
//接收发送过来的请求,并做相应处理
public void readText(String path){
//list.clear();
FileInputStream fis;
try {
fis = new FileInputStream(path);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
jta.setText("");
String temp=br.readLine();
while(temp!=null){
jta.append(temp+"\n");
temp=br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String readPath(){
//JFrame frm=new JFrame();
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setVisible(true);
int i=chooser.showOpenDialog(this);
String path=null;
if(i==JFileChooser.APPROVE_OPTION){
path=chooser.getSelectedFile().getPath();
return path;
}else
return null;
}
public String savePath(){
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setVisible(true);
int i=chooser.showSaveDialog(this);
String path=null;
if(i==JFileChooser.APPROVE_OPTION){
path=chooser.getSelectedFile().getPath();
return path;
}else
return null;
}
public boolean writer(String paths){
//String paths=savePath();
FileWriter fw;
try {
fw = new FileWriter(paths);
fw.write(jta.getText());
fw.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand(); //
if (comm.equals("新建...")) {
new TestNotepad();
//jta.setText("");
}
if (comm.equals("保存")) {
System.out.println("保存");
String saveStr=this.savePath();
System.out.println(this.writer(saveStr));
}
if (comm.equals("日期")){
jta.append(new Date().toString());
}
if (comm.equals("打开...")){
readText(readPath());
}
if (comm.equals("退出")){
System.exit(0);
}
if (comm.equals("剪切")) {
jta.cut();
}
if (comm.equals("复制")) {
jta.copy();
}
if (comm.equals("粘贴")) {
jta.paste();
}
if (comm.equals("删除")) {
jta.cut();
}
if(comm.equals("颜色选择")){
jta.setForeground(Util.setColor());
}
if ("查找...".equals(comm) || "替换...".equals(comm)) {
JDialog jd = new JDialog(jf, comm);
String label = comm.substring(0, 2);
JButton jb = new JButton(label);
jb.addActionListener(this);
jd.setLayout(new FlowLayout());
jd.add(jtf);
if ("替换...".equals(comm)) {
JLabel jlt = new JLabel(">");
jd.add(jlt);
jd.add(jtft);
}
jd.add(jb);
if ("替换...".equals(comm)) {
JButton jball = new JButton("替换全部");
jball.addActionListener(this);
jd.add(jball);
}
jd.pack();
jd.setResizable(false);
jd.setLocation(400, 240);
jd.setVisible(true);
}
if (comm.equals("全选")) {
jta.selectAll();
}
if (comm.equals("关于...")) {
JDialog jd = new JDialog(jf, "关于...");
jd.setLayout(new FlowLayout());
JLabel jl = new JLabel("制作者: xxxxxxxx");
jd.add(jl);
jd.setSize(180, 100);
jd.setResizable(false);
jd.setLocation(400, 240);
jd.setVisible(true);
}
if (comm.equals("查找")) {
String str = jtf.getText();
String strall = jta.getText();
int n = strall.indexOf(str, set);
if (n != -1) {
set = n + 1;
jta.setSelectionStart(n);
jta.setSelectionEnd(n + str.length());
}
}
if (comm.equals("替换") || comm.equals("替换全部")) {
String str = jtf.getText();
String strall = jta.getText();
if (comm.equals("替换全部")) {
int n1 = 0;
while (true) {
set = strall.indexOf(str, n1);
if (set == -1)
break;
n1 = set + 1;
jta.setSelectionStart(set);
jta.setSelectionEnd(set + str.length());
jta.replaceSelection(jtft.getText());
}
set = 0;
}
int n = strall.indexOf(str, set);
if (n == -1) {
set = 0;
} else {
set = n + 1;
jta.setSelectionStart(n);
jta.setSelectionEnd(n + str.length());
jta.replaceSelection(jtft.getText());
}
}
}
public TestNotepad() {
jta.setLineWrap(true);
jta.addMouseListener(this);
jf.add(jsp);
JMenuBar jmb = new JMenuBar();
JMenu jm1 = new JMenu("文件");
String[] label1 = { "新建...", "打开...", "保存", "退出" };
JMenuItem[] jmi1 = new JMenuItem[label1.length];
for (int i = 0; i < jmi1.length; i++) {
jmi1[i] = new JMenuItem(label1[i]);
jm1.add(jmi1[i]);
jmi1[i].addActionListener(this);
}
jm1.insertSeparator(1);
jm1.insertSeparator(3);
jm1.insertSeparator(6);
JMenu jm2 = new JMenu("编辑");
String[] label2 = { "剪切", "复制", "粘贴", "删除", "查找...", "替换...",
"全选","颜色选择","日期" };
JMenuItem[] jmi2 = new JMenuItem[label2.length];
for (int i = 0; i < jmi2.length; i++) {
jmi2[i] = new JMenuItem(label2[i]);
jm2.add(jmi2[i]);
jmi2[i].addActionListener(this);
}
jm2.insertSeparator(1);
jm2.insertSeparator(5);
jm2.insertSeparator(7);
jm2.insertSeparator(10);
JMenu jm3 = new JMenu("帮助");
JMenuItem jmi3 = new JMenuItem("关于...");
jm3.add(jmi3);
jmi3.addActionListener(this);
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm3);
jf.setJMenuBar(jmb);
jf.pack();
jf.setLocation(300, 200);
jf.setVisible(true);
//jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
new TestNotepad();
}
//鼠标右键功能
public void mouseClicked(MouseEvent e) {
JPopupMenu pp = new JPopupMenu();
if (e.getButton() == 3) {
JMenuItem jmi1,jmi2,jmi3,jmi4;
pp.add(jmi1 = new JMenuItem("剪切"));
pp.add(jmi2 = new JMenuItem("复制"));
pp.add(jmi3 = new JMenuItem("粘贴"));
pp.addSeparator();
pp.add(jmi4 = new JMenuItem("全选"));
jmi1.addActionListener(this);
jmi2.addActionListener(this);
jmi3.addActionListener(this);
jmi4.addActionListener(this);
pp.pack();
jf.add(pp);
pp.setVisible(true);
pp.show(e.getComponent(), e.getX(), e.getY());
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
关于撤消操作部分可参见%JAVA_HOME%\demo\jfc\Notepad
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果你的机子上面安装的是jdk1.6以上,那么你jdk目录的demo\jfc\Notepad下面就有一个记事本
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一楼的回答很好……
你可以继续向下开发。
你可以继续向下开发。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
风追云影
的回答我个人认为很好。
的回答我个人认为很好。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不错
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询