求JAVA文本编辑器源程序 急!!!
可以设置文本的字体、大小、颜色等基本参数,可以读取计算机中TXT文件,可以生成一个新的TXT文件。也要有复制、粘贴功能。先谢谢各位啦!!!...
可以设置文本的字体、大小、颜色等基本参数,可以读取计算机中TXT文件,可以生成一个新的TXT文件。也要有复制、粘贴功能。
先谢谢各位啦!!! 展开
先谢谢各位啦!!! 展开
展开全部
import java.awt.Color;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
private JFrame jf = new JFrame("Notepad"); //面板
private JTextArea jta = new JTextArea(20, 30); //文本区
JScrollPane jsp = new JScrollPane(jta); //滚动条
public Test() {
jta.setLineWrap(true);
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);
}
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);
}
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.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
jf.removeAll();
jf.dispose();
}
});
}
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);
chooser.showOpenDialog(this);
String path=null;
// if(i==JFileChooser.APPROVE_OPTION){
path=chooser.getSelectedFile().getPath();
System.out.println(JFileChooser.OPEN_DIALOG);
//}
return path;
}
public String savePath(){
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setVisible(true);
chooser.showSaveDialog(this);
String path=null;
// if(i==JFileChooser.APPROVE_OPTION){
path=chooser.getSelectedFile().getPath();
System.out.println(JFileChooser.OPEN_DIALOG);
//}
return path;
}
public static Color setColor(){
Color selectedColor = JColorChooser.showDialog(null,"select",Color.BLACK) ;
return selectedColor;
}
public boolean writer(String paths){
//String paths=savePath();
FileWriter fw;
try {
fw = new FileWriter(paths,true);
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("新建...")) {
jta.setText("");
}
if (comm.equals("保存")) {
String saveStr=this.savePath();
this.writer(saveStr);
}
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.selectAll();
}
if (comm.equals("颜色")) {
jta.setForeground(setColor());
}
}
public static void main(String[] args) {
new Test();
}
}
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
private JFrame jf = new JFrame("Notepad"); //面板
private JTextArea jta = new JTextArea(20, 30); //文本区
JScrollPane jsp = new JScrollPane(jta); //滚动条
public Test() {
jta.setLineWrap(true);
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);
}
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);
}
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.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
jf.removeAll();
jf.dispose();
}
});
}
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);
chooser.showOpenDialog(this);
String path=null;
// if(i==JFileChooser.APPROVE_OPTION){
path=chooser.getSelectedFile().getPath();
System.out.println(JFileChooser.OPEN_DIALOG);
//}
return path;
}
public String savePath(){
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setVisible(true);
chooser.showSaveDialog(this);
String path=null;
// if(i==JFileChooser.APPROVE_OPTION){
path=chooser.getSelectedFile().getPath();
System.out.println(JFileChooser.OPEN_DIALOG);
//}
return path;
}
public static Color setColor(){
Color selectedColor = JColorChooser.showDialog(null,"select",Color.BLACK) ;
return selectedColor;
}
public boolean writer(String paths){
//String paths=savePath();
FileWriter fw;
try {
fw = new FileWriter(paths,true);
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("新建...")) {
jta.setText("");
}
if (comm.equals("保存")) {
String saveStr=this.savePath();
this.writer(saveStr);
}
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.selectAll();
}
if (comm.equals("颜色")) {
jta.setForeground(setColor());
}
}
public static void main(String[] args) {
new Test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询