Java程序设计 5
1、编写一个记事本程序要求:用图形用户界面实现。能实现编辑、保存、另存为、查找替换等功能。提示:使用文件输入输出流。...
1、编写一个记事本程序
要求:
用图形用户界面实现。
能实现编辑、保存、另存为、查找替换等功能。
提示:使用文件输入输出流。 展开
要求:
用图形用户界面实现。
能实现编辑、保存、另存为、查找替换等功能。
提示:使用文件输入输出流。 展开
3个回答
展开全部
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.*;
public class WordProcessSystem extends JFrame{
private JFileChooser chooser;
private JTextArea textArea;
private File file;
private String string = "";
private Font font;
public WordProcessSystem(){
super();
setTitle("日记记事本");
setBounds(100, 100,600, 500);
getContentPane().setLayout(new BorderLayout());
getContentPane().setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("文件");
JMenuItem menuItem = new JMenuItem("新建");
menuItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String str1 = JOptionPane.showInputDialog("请输入新建文件名:");
try {
file = new File("E:\\", str1 + ".txt");
if(file.exists()){
JOptionPane.showMessageDialog(null, "文件名已存在!");
}
else{
file.createNewFile();
}
} catch (Exception e1){
}
}
});
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
JMenuItem menuItem1 = new JMenuItem("打开");
menuItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
String str = chooser.getSelectedFile().toString();
try {
file = new File(str);
readFromFile(file);
textArea.setText(string);
} catch (Exception e1) {
}
}
}
});
JMenuItem menuItem2 = new JMenuItem("退出");
menuItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
JMenuItem menuItem3 = new JMenuItem("保存");
menuItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String string = textArea.getText();
try {
writeToFile(file, string);
} catch (Exception e1) {
}
}
});
menuItem3.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == KeyEvent.VK_C){
String string = textArea.getText();
try {
writeToFile(file, string);
} catch (Exception e1) {
}
}
}
});
JMenuItem menuItem4 = new JMenuItem("另存为");
menuItem4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String s1 = JOptionPane.showInputDialog("请输入保存文件路径和文件名:");
file = new File(s1);
System.out.println(file.getPath());
try {
writeToFile(file, string);
System.out.println("aaaa");
} catch (Exception e1) {
}
}
});
menu.add(menuItem);
menu.add(menuItem1);
menu.add(menuItem2);
menu.add(menuItem3);
menu.add(menuItem4);
JMenu menu2 = new JMenu("格式");
final JMenu menu3 = new JMenu("字体");
String []fontString = {"宋体","楷体","隶书"};
final JMenuItem []menu3_1 = new JMenuItem[fontString.length];
for(int i = 0;i < fontString.length;i++){
String changeString = "";
menu3_1[i] = new JMenuItem(fontString[i]);
menu3_1[i].setActionCommand(changeString + i);
menu3_1[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand().charAt(0)) {
case '0':
font = new Font("宋体",Font.PLAIN,20);
textArea.setFont(font);
break;
case '1':
font = new Font("楷体",Font.PLAIN,18);
textArea.setFont(font);
break;
case '2':
font = new Font("隶书",Font.BOLD,18);
textArea.setFont(font);
break;
default:
break;
}
}});
menu3.add(menu3_1[i]);
}
JMenu menu4 = new JMenu("字号");
String []fontBig = {"10","15","20","25","30","35","40"};
final JMenuItem []menu4_1 = new JMenuItem[fontBig.length];
for(int i = 0;i < fontBig.length;i++){
String changeString = "";
menu4_1[i] = new JMenuItem(fontBig[i]);
menu4_1[i].setActionCommand(changeString + i);
menu4_1[i].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand().charAt(0)) {
case '0':
font = new Font(menu3.getText(),Font.PLAIN,10);
textArea.setFont(font);
break;
case '1':
font = new Font(menu3.getText(),Font.PLAIN,15);
textArea.setFont(font);
break;
case '2':
font = new Font(menu3.getText(),Font.PLAIN,20);
textArea.setFont(font);
break;
case '3':
font = new Font(menu3.getText(),Font.PLAIN,25);
textArea.setFont(font);
break;
case '4':
font = new Font(menu3.getText(),Font.PLAIN,30);
textArea.setFont(font);
break;
case '5':
font = new Font(menu3.getText(),Font.PLAIN,35);
textArea.setFont(font);
break;
case '6':
font = new Font(menu3.getText(),Font.PLAIN,40);
textArea.setFont(font);
break;
default:
break;
}
}});
menu4.add(menu4_1[i]);
}
menu2.add(menu3);
menu2.add(menu4);
menuBar.add(menu);
menuBar.add(menu2);
setJMenuBar(menuBar);
textArea = new JTextArea();
textArea.setVisible(true);
textArea.setLineWrap(true);
// textArea.setFont(new Font("宋体",Font.BOLD,20));
add(textArea);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setSize(600, 410);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scrollPane);
}
public void readFromFile(File f) throws IOException{
FileReader fReader = new FileReader(file);
BufferedReader bReader = new BufferedReader(fReader);
char []data = new char[10000];
int length = 0;
while((length = fReader.read(data)) > 0){
string += new String(data,0,length);
}
// string = new String("");
// while(true){
// String s1 = bReader.readLine();
// if(s1 == null){
// break;
// }
// string += (s1 + "\n");
bReader.close();
fReader.close();
}
public void writeToFile(File file,String string) throws IOException{
if(!file.exists()){
file.createNewFile();
}
FileWriter fWriter = new FileWriter(file);
fWriter.write(string);
fWriter.close();
}
public static void main(String[] args) {
WordProcessSystem wordProcessSystem = new WordProcessSystem();
wordProcessSystem.setVisible(true);
}
}
不好意思,这个没有实现查找和替换功能,你自己可以加上去。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.*;
public class WordProcessSystem extends JFrame{
private JFileChooser chooser;
private JTextArea textArea;
private File file;
private String string = "";
private Font font;
public WordProcessSystem(){
super();
setTitle("日记记事本");
setBounds(100, 100,600, 500);
getContentPane().setLayout(new BorderLayout());
getContentPane().setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("文件");
JMenuItem menuItem = new JMenuItem("新建");
menuItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String str1 = JOptionPane.showInputDialog("请输入新建文件名:");
try {
file = new File("E:\\", str1 + ".txt");
if(file.exists()){
JOptionPane.showMessageDialog(null, "文件名已存在!");
}
else{
file.createNewFile();
}
} catch (Exception e1){
}
}
});
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
JMenuItem menuItem1 = new JMenuItem("打开");
menuItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
String str = chooser.getSelectedFile().toString();
try {
file = new File(str);
readFromFile(file);
textArea.setText(string);
} catch (Exception e1) {
}
}
}
});
JMenuItem menuItem2 = new JMenuItem("退出");
menuItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
JMenuItem menuItem3 = new JMenuItem("保存");
menuItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String string = textArea.getText();
try {
writeToFile(file, string);
} catch (Exception e1) {
}
}
});
menuItem3.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == KeyEvent.VK_C){
String string = textArea.getText();
try {
writeToFile(file, string);
} catch (Exception e1) {
}
}
}
});
JMenuItem menuItem4 = new JMenuItem("另存为");
menuItem4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String s1 = JOptionPane.showInputDialog("请输入保存文件路径和文件名:");
file = new File(s1);
System.out.println(file.getPath());
try {
writeToFile(file, string);
System.out.println("aaaa");
} catch (Exception e1) {
}
}
});
menu.add(menuItem);
menu.add(menuItem1);
menu.add(menuItem2);
menu.add(menuItem3);
menu.add(menuItem4);
JMenu menu2 = new JMenu("格式");
final JMenu menu3 = new JMenu("字体");
String []fontString = {"宋体","楷体","隶书"};
final JMenuItem []menu3_1 = new JMenuItem[fontString.length];
for(int i = 0;i < fontString.length;i++){
String changeString = "";
menu3_1[i] = new JMenuItem(fontString[i]);
menu3_1[i].setActionCommand(changeString + i);
menu3_1[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand().charAt(0)) {
case '0':
font = new Font("宋体",Font.PLAIN,20);
textArea.setFont(font);
break;
case '1':
font = new Font("楷体",Font.PLAIN,18);
textArea.setFont(font);
break;
case '2':
font = new Font("隶书",Font.BOLD,18);
textArea.setFont(font);
break;
default:
break;
}
}});
menu3.add(menu3_1[i]);
}
JMenu menu4 = new JMenu("字号");
String []fontBig = {"10","15","20","25","30","35","40"};
final JMenuItem []menu4_1 = new JMenuItem[fontBig.length];
for(int i = 0;i < fontBig.length;i++){
String changeString = "";
menu4_1[i] = new JMenuItem(fontBig[i]);
menu4_1[i].setActionCommand(changeString + i);
menu4_1[i].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand().charAt(0)) {
case '0':
font = new Font(menu3.getText(),Font.PLAIN,10);
textArea.setFont(font);
break;
case '1':
font = new Font(menu3.getText(),Font.PLAIN,15);
textArea.setFont(font);
break;
case '2':
font = new Font(menu3.getText(),Font.PLAIN,20);
textArea.setFont(font);
break;
case '3':
font = new Font(menu3.getText(),Font.PLAIN,25);
textArea.setFont(font);
break;
case '4':
font = new Font(menu3.getText(),Font.PLAIN,30);
textArea.setFont(font);
break;
case '5':
font = new Font(menu3.getText(),Font.PLAIN,35);
textArea.setFont(font);
break;
case '6':
font = new Font(menu3.getText(),Font.PLAIN,40);
textArea.setFont(font);
break;
default:
break;
}
}});
menu4.add(menu4_1[i]);
}
menu2.add(menu3);
menu2.add(menu4);
menuBar.add(menu);
menuBar.add(menu2);
setJMenuBar(menuBar);
textArea = new JTextArea();
textArea.setVisible(true);
textArea.setLineWrap(true);
// textArea.setFont(new Font("宋体",Font.BOLD,20));
add(textArea);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setSize(600, 410);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scrollPane);
}
public void readFromFile(File f) throws IOException{
FileReader fReader = new FileReader(file);
BufferedReader bReader = new BufferedReader(fReader);
char []data = new char[10000];
int length = 0;
while((length = fReader.read(data)) > 0){
string += new String(data,0,length);
}
// string = new String("");
// while(true){
// String s1 = bReader.readLine();
// if(s1 == null){
// break;
// }
// string += (s1 + "\n");
bReader.close();
fReader.close();
}
public void writeToFile(File file,String string) throws IOException{
if(!file.exists()){
file.createNewFile();
}
FileWriter fWriter = new FileWriter(file);
fWriter.write(string);
fWriter.close();
}
public static void main(String[] args) {
WordProcessSystem wordProcessSystem = new WordProcessSystem();
wordProcessSystem.setVisible(true);
}
}
不好意思,这个没有实现查找和替换功能,你自己可以加上去。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询