用JAVA窗口写个记事本 用JAVA窗口写个简单的记事本(新建,保存,另存为,打开)就行,用IO求高手救我 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先学学swing咯把界面弄出来,然后在写后台部分 主要使用 JTextArea,DefaultEditorKit,Action。 JDK自带一个记事本程序,是个不错的参考C:\Program Files\Java\jdk1.6.0_26\demo\jfc\Stylepad\Stylepad.jar package zizi;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Dialog;import java.awt.Frame;import java.awt.Label;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import javax.swing.*;import javax.swing.event.MenuEvent;import javax.swing.event.MenuListener;import javax.swing.text.BadLocationException;import javax.swing.text.DefaultHighlighter;public class Text extends JFrame implements ActionListener,MenuListener{ private File file; private JMenuBar menuBar; private JMenu menuFile,menuEdit,menuSearch; private JMenuItem itemNew,itemOpen,itemsave,itemSaveAs,itemExit,itemCopy,itemCut,itemPaste,itemDelete,itemAll; public JTextArea jta; private JScrollPane jsp; private JFileChooser jfc; private MyFindDialog findDlg=new MyFindDialog(this,jta); public Text(){ super("记事本"); this.setSize(550, 400); this.setLocation(200, 200); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.file=null; this.jfc=new JFileChooser(new File(".","")); menuBar=new JMenuBar(); this.setJMenuBar(menuBar); menuFile=new JMenu("文件"); menuEdit=new JMenu("编辑"); menuSearch=new JMenu("查找"); menuSearch.addMenuListener(this); menuBar.add(menuFile); menuBar.add(menuEdit); menuBar.add(menuSearch); itemNew=new JMenuItem("新建"); itemNew.addActionListener(this); itemOpen=new JMenuItem("打开"); itemOpen.addActionListener(this); itemsave=new JMenuItem("保存"); itemsave.addActionListener(this); itemSaveAs=new JMenuItem("另存为"); itemSaveAs.addActionListener(this); itemExit=new JMenuItem("退出"); itemExit.addActionListener(this); menuFile.add(itemNew); menuFile.add(itemOpen); menuFile.add(itemsave); menuFile.add(itemSaveAs); menuFile.addSeparator(); menuFile.add(itemExit); itemCopy=new JMenuItem("复制"); itemCopy.addActionListener(this); itemCut=new JMenuItem("剪切"); itemCut.addActionListener(this); itemPaste=new JMenuItem("粘贴"); itemPaste.addActionListener(this); itemDelete=new JMenuItem("删除"); itemDelete.addActionListener(this) ; itemAll=new JMenuItem("全选"); itemAll.addActionListener(this); menuEdit.add(itemCopy); menuEdit.add(itemCut); menuEdit.add(itemPaste); menuEdit.add(itemDelete); menuEdit.addSeparator(); menuEdit.add(itemAll); jta=new JTextArea(); jsp=new JScrollPane(jta); this.add(jsp,BorderLayout.CENTER); } public static void main(String[] args) { Text t=new Text(); t.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if (e.getSource()==itemNew) { this.file=null; jta.setText(""); this.setTitle("未命名"); } if((e.getSource()==itemOpen&&jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) ){ this.file=jfc.getSelectedFile(); this.setTitle(this.file.getName()); this.jta.setText(this.readFromFile()); } if(e.getSource()==itemsave&&this.file!=null) this.writeToFile(this.jta.getText()); if (((e.getSource()==itemSaveAs && this.file==null)&& jfc.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)) { this.file = jfc.getSelectedFile(); if (!file.getName().endsWith(".txt")) this.file= new File(file.getAbsolutePath()+".txt"); this.writeToFile(this.jta.getText()); this.setTitle(this.file.getName()); } if(e.getSource()==itemExit){ System.exit(0); } if(e.getSource()==itemCopy){ jta.copy(); } if(e.getSource()==itemCut){ jta.cut(); } if(e.getSource()==itemPaste){ jta.paste(); } if(e.getSource()==itemDelete){ jta.replaceSelection(null); }; if(e.getSource()==itemAll){ jta.selectAll(); } } private void writeToFile(String lines) { try { FileWriter fout = new FileWriter(this.file); fout.write(lines+"\r\n"); fout.close(); } catch (IOException ioex) { JOptionPane.showMessageDialog(this, "IO错误,写入\""+this.file.getName()+"\"文件不成功"); } } private String readFromFile() { char lines[]=null; try { FileReader fin = new FileReader(this.file); lines=new char[(int)this.file.length()]; fin.read(lines); fin.close(); } catch (FileNotFoundException fe) { JOptionPane.showMessageDialog(this, "\""+this.file.getName()+"\"文件不存在"); } catch (IOException ioex) { JOptionPane.showMessageDialog(this, "IO错误,读取\""+this.file.getName()+"\"文件不成功"); } return new String(lines); } @Override public void menuCanceled(MenuEvent arg0) { // TODO Auto-generated method stub } @Override public void menuDeselected(MenuEvent arg0) { // TODO Auto-generated method stub } @Override public void menuSelected(MenuEvent arg0) { // TODO Auto-generated method stub findDlg.showReplace(); } class MyFindDialog extends Dialog implements ActionListener{ private Label lFind=new Label("查找字符串"); private Label lReplace=new Label("替换字符串"); private TextField tFind=new TextField(10); private TextField tReplace=new TextField(10); private Button bFind=new Button("查找"); private Button bReplace=new Button("替换下一个"); public MyFindDialog(Frame owner,JTextArea jta){ super(owner,"查找",false); Text.this.jta=jta; setLayout(null); lFind.setBounds(10,30,80,20); lReplace.setBounds(10,70,80,20); tFind.setBounds(90, 30, 90, 20); tReplace.setBounds(90,70,90,20); bFind.setBounds(190,30,80,20); bReplace.setBounds(190,70,80,20); add(lFind); add(tFind); add(bFind); add(lReplace); add(tReplace); add(bReplace); setResizable(false); bFind.addActionListener(this); bReplace.addActionListener(this); }//构造函数结束 public void showReplace(){ setTitle("查找替换"); setSize(280,110); setVisible(true); } private void find(){ String text=jta.getText(); String context=tFind.getText(); int index = text.indexOf(context); if (index != -1) { try { while (index != -1) { jta.getHighlighter().addHighlight(index, index + context.length(), DefaultHighlighter.DefaultPainter); index = text.indexOf(context, index + context.length()); } } catch (BadLocationException ex) { ex.printStackTrace(); } } else { JOptionPane.showMessageDialog(Text.this, "没有找到:" + context); } } private void replace(){ String str=tReplace.getText(); String text=jta.getText(); String context=tFind.getText(); int index = text.indexOf(context); if (index != -1) { { jta.replaceRange(str,index, index + context.length()); } } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==bFind) { find();} if(e.getSource()==bReplace) { replace(); } } }} http://hi.baidu.com/freish/blog/item/21a604f7c0271b28730eec61.html 学完Swing和IO你自然就会了。学习时间一天足够。 HTML 转 PDF时 输入框 多选框,单选框怎么显示在PDF格式中,用的是IText struts部署错误 搭建框架问题 请问有没有这样一个完全的分布式系统 本人第一次接触JVAV开发,谁能给一点好的建议 客户端调用web service 初学struts,求助,送分! Hibernate中日期类型的插入问题 如何向指定QQ发送QQ消息、达到客户端效果 FRAME中执行.do的问题 servlet中init方法可以执行,但是doGet方法不能执行 Spring Security3 循环重定向
C:\Program Files\Java\jdk1.6.0_26\demo\jfc\Stylepad\Stylepad.jar
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
public class Text extends JFrame implements ActionListener,MenuListener{
private File file;
private JMenuBar menuBar;
private JMenu menuFile,menuEdit,menuSearch;
private JMenuItem itemNew,itemOpen,itemsave,itemSaveAs,itemExit,itemCopy,itemCut,itemPaste,itemDelete,itemAll;
public JTextArea jta;
private JScrollPane jsp;
private JFileChooser jfc;
private MyFindDialog findDlg=new MyFindDialog(this,jta);
public Text(){
super("记事本");
this.setSize(550, 400);
this.setLocation(200, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.file=null;
this.jfc=new JFileChooser(new File(".",""));
menuBar=new JMenuBar();
this.setJMenuBar(menuBar);
menuFile=new JMenu("文件");
menuEdit=new JMenu("编辑");
menuSearch=new JMenu("查找");
menuSearch.addMenuListener(this);
menuBar.add(menuFile);
menuBar.add(menuEdit);
menuBar.add(menuSearch);
itemNew=new JMenuItem("新建");
itemNew.addActionListener(this);
itemOpen=new JMenuItem("打开");
itemOpen.addActionListener(this);
itemsave=new JMenuItem("保存");
itemsave.addActionListener(this);
itemSaveAs=new JMenuItem("另存为");
itemSaveAs.addActionListener(this);
itemExit=new JMenuItem("退出");
itemExit.addActionListener(this);
menuFile.add(itemNew);
menuFile.add(itemOpen);
menuFile.add(itemsave);
menuFile.add(itemSaveAs);
menuFile.addSeparator();
menuFile.add(itemExit);
itemCopy=new JMenuItem("复制");
itemCopy.addActionListener(this);
itemCut=new JMenuItem("剪切");
itemCut.addActionListener(this);
itemPaste=new JMenuItem("粘贴");
itemPaste.addActionListener(this);
itemDelete=new JMenuItem("删除");
itemDelete.addActionListener(this) ;
itemAll=new JMenuItem("全选");
itemAll.addActionListener(this);
menuEdit.add(itemCopy);
menuEdit.add(itemCut);
menuEdit.add(itemPaste);
menuEdit.add(itemDelete);
menuEdit.addSeparator();
menuEdit.add(itemAll);
jta=new JTextArea();
jsp=new JScrollPane(jta);
this.add(jsp,BorderLayout.CENTER);
}
public static void main(String[] args) {
Text t=new Text();
t.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource()==itemNew) {
this.file=null;
jta.setText("");
this.setTitle("未命名");
}
if((e.getSource()==itemOpen&&jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
){
this.file=jfc.getSelectedFile();
this.setTitle(this.file.getName());
this.jta.setText(this.readFromFile());
}
if(e.getSource()==itemsave&&this.file!=null)
this.writeToFile(this.jta.getText());
if (((e.getSource()==itemSaveAs && this.file==null)&& jfc.showSaveDialog(this)==JFileChooser.APPROVE_OPTION))
{
this.file = jfc.getSelectedFile();
if (!file.getName().endsWith(".txt"))
this.file= new File(file.getAbsolutePath()+".txt");
this.writeToFile(this.jta.getText());
this.setTitle(this.file.getName());
}
if(e.getSource()==itemExit){
System.exit(0);
}
if(e.getSource()==itemCopy){
jta.copy();
}
if(e.getSource()==itemCut){
jta.cut();
}
if(e.getSource()==itemPaste){
jta.paste();
}
if(e.getSource()==itemDelete){
jta.replaceSelection(null);
};
if(e.getSource()==itemAll){
jta.selectAll();
}
}
private void writeToFile(String lines)
{
try
{
FileWriter fout = new FileWriter(this.file);
fout.write(lines+"\r\n");
fout.close();
}
catch (IOException ioex)
{
JOptionPane.showMessageDialog(this, "IO错误,写入\""+this.file.getName()+"\"文件不成功");
}
} private String readFromFile()
{
char lines[]=null;
try
{
FileReader fin = new FileReader(this.file);
lines=new char[(int)this.file.length()];
fin.read(lines);
fin.close();
}
catch (FileNotFoundException fe)
{
JOptionPane.showMessageDialog(this, "\""+this.file.getName()+"\"文件不存在");
}
catch (IOException ioex)
{
JOptionPane.showMessageDialog(this, "IO错误,读取\""+this.file.getName()+"\"文件不成功");
}
return new String(lines);
}
@Override
public void menuCanceled(MenuEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void menuDeselected(MenuEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void menuSelected(MenuEvent arg0) {
// TODO Auto-generated method stub
findDlg.showReplace();
}
class MyFindDialog extends Dialog implements ActionListener{
private Label lFind=new Label("查找字符串");
private Label lReplace=new Label("替换字符串");
private TextField tFind=new TextField(10);
private TextField tReplace=new TextField(10);
private Button bFind=new Button("查找");
private Button bReplace=new Button("替换下一个");
public MyFindDialog(Frame owner,JTextArea jta){
super(owner,"查找",false);
Text.this.jta=jta;
setLayout(null);
lFind.setBounds(10,30,80,20);
lReplace.setBounds(10,70,80,20);
tFind.setBounds(90, 30, 90, 20);
tReplace.setBounds(90,70,90,20);
bFind.setBounds(190,30,80,20);
bReplace.setBounds(190,70,80,20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
}//构造函数结束
public void showReplace(){
setTitle("查找替换");
setSize(280,110);
setVisible(true);
}
private void find(){
String text=jta.getText();
String context=tFind.getText();
int index = text.indexOf(context);
if (index != -1) {
try {
while (index != -1) {
jta.getHighlighter().addHighlight(index, index + context.length(), DefaultHighlighter.DefaultPainter);
index = text.indexOf(context, index + context.length());
}
} catch (BadLocationException ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(Text.this, "没有找到:" + context);
}
}
private void replace(){
String str=tReplace.getText();
String text=jta.getText();
String context=tFind.getText();
int index = text.indexOf(context);
if (index != -1) {
{
jta.replaceRange(str,index, index + context.length()); }
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==bFind)
{ find();}
if(e.getSource()==bReplace)
{
replace();
}
}
}}