求java版记事本源代码,急求!!!!!!
【要求】:(1)GUI界面应该包括菜单栏、工具栏及状态栏;其中状态栏能动态显示系统时间(2)实现记事本中的打开、保存功能、自动换行功能;另需添加字体、字号及颜色的设置、帮...
【要求】:
(1)GUI界面应该包括菜单栏、工具栏及状态栏;其中状态栏能动态显示系统时间
(2)实现记事本中的打开、保存功能、自动换行功能;另需添加字体、字号及颜色的设置、帮助窗口的打开等。
(3)实现编辑菜单里面的“查找和替换”功能、撤消功能;文字选中后的复制粘贴功能等。
要求加详细注释!!!!!!!! 展开
(1)GUI界面应该包括菜单栏、工具栏及状态栏;其中状态栏能动态显示系统时间
(2)实现记事本中的打开、保存功能、自动换行功能;另需添加字体、字号及颜色的设置、帮助窗口的打开等。
(3)实现编辑菜单里面的“查找和替换”功能、撤消功能;文字选中后的复制粘贴功能等。
要求加详细注释!!!!!!!! 展开
2个回答
展开全部
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.awt.datatransfer.*;
import javax.swing.*;
public class MiniEdit extends JFrame implements ActionListener {
/**
* Method main
*
*
* @param args
*
*/
MenuBar menuBar = new MenuBar();
Menu file = new Menu("File"),
edit = new Menu("Edit"),
help = new Menu("Help");
MenuItem[] menuItem ={
new MenuItem("New"),
new MenuItem("Open"),
new MenuItem("Save"),
new MenuItem("Exit"),
new MenuItem("Select All"),
new MenuItem("Copy"),
new MenuItem("Cut"),
new MenuItem("Paste"),
new MenuItem("Help")
};
TextArea textArea = new TextArea();
String fileName = "NoName";
Toolkit toolKit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolKit.getSystemClipboard();
//opne and close message dialogs
private FileDialog openFileDialog =
new FileDialog(this,"Open File",FileDialog.LOAD);
private FileDialog saveFileDialog =
new FileDialog(this,"Save File",FileDialog.SAVE);
public static void main(String[] args) {
// TODO: Add your code here
MiniEdit MyEdit = new MiniEdit();
MyEdit.show();
}
/**
* Method MiniEdit
*
*
*/
public MiniEdit() {
// TODO: Add your code here
setTitle("MiniEdit");
setFont(new Font("Times New Roman",Font.PLAIN,15));
setBackground(Color.white);
setSize(500,500);
setMenuBar(menuBar);
menuBar.add(file);
menuBar.add(edit);
menuBar.add(help);
for(int i=0;i<4;i++)
{
file.add(menuItem[i]);
edit.add(menuItem[i+4]);
}
help.add(menuItem[8]);
add(textArea);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
e.getWindow().dispose();
System.exit(0);
}
});
//add actionListener
for(int i=0;i<menuItem.length;i++)
{
menuItem[i].addActionListener(this);
}
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
Object eventSource = e.getSource();
if(eventSource == menuItem[0])//newItem
{
textArea.setText("");
}
else if(eventSource == menuItem[1])//OpenItem
{
openFileDialog.show();
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
{
openFile(fileName);
}
}
else if(eventSource ==menuItem[2])//SaveItem
{
saveFileDialog.show();
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
if(fileName !=null)
{
writeFile(fileName);
}
}
else if(eventSource==menuItem[3])//exitItem
{
System.exit(0);
}
else if(eventSource == menuItem[4])//Select All
{
textArea.selectAll();
}
else if(eventSource == menuItem[5])//copy
{
String text = textArea.getSelectedText();
StringSelection selection= new StringSelection(text);
clipboard.setContents(selection,null);
}
else if(eventSource == menuItem[6])//cut
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
textArea.replaceText("",textArea.getSelectionStart(),
textArea.getSelectionEnd());
}
else if(eventSource == menuItem[7])//Paste
{
Transferable contents = clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception ex){}
textArea.replaceText(text,
textArea.getSelectionStart(),textArea.getSelectionEnd());
}
else if(eventSource == menuItem[8])
{
// JOptionPane.showMessageDialog(null,"This is a MiniEdit.");
}
}
//Read 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(Exception e)
{
System.out.println("Error opening file!");
}
}
//write file
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!");
import java.util.EventListener;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.lang.*;
import java.awt.datatransfer.*;
import javax.swing.*;
public class MiniEdit extends JFrame implements ActionListener {
/**
* Method main
*
*
* @param args
*
*/
MenuBar menuBar = new MenuBar();
Menu file = new Menu("File"),
edit = new Menu("Edit"),
help = new Menu("Help");
MenuItem[] menuItem ={
new MenuItem("New"),
new MenuItem("Open"),
new MenuItem("Save"),
new MenuItem("Exit"),
new MenuItem("Select All"),
new MenuItem("Copy"),
new MenuItem("Cut"),
new MenuItem("Paste"),
new MenuItem("Help")
};
TextArea textArea = new TextArea();
String fileName = "NoName";
Toolkit toolKit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolKit.getSystemClipboard();
//opne and close message dialogs
private FileDialog openFileDialog =
new FileDialog(this,"Open File",FileDialog.LOAD);
private FileDialog saveFileDialog =
new FileDialog(this,"Save File",FileDialog.SAVE);
public static void main(String[] args) {
// TODO: Add your code here
MiniEdit MyEdit = new MiniEdit();
MyEdit.show();
}
/**
* Method MiniEdit
*
*
*/
public MiniEdit() {
// TODO: Add your code here
setTitle("MiniEdit");
setFont(new Font("Times New Roman",Font.PLAIN,15));
setBackground(Color.white);
setSize(500,500);
setMenuBar(menuBar);
menuBar.add(file);
menuBar.add(edit);
menuBar.add(help);
for(int i=0;i<4;i++)
{
file.add(menuItem[i]);
edit.add(menuItem[i+4]);
}
help.add(menuItem[8]);
add(textArea);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
e.getWindow().dispose();
System.exit(0);
}
});
//add actionListener
for(int i=0;i<menuItem.length;i++)
{
menuItem[i].addActionListener(this);
}
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
Object eventSource = e.getSource();
if(eventSource == menuItem[0])//newItem
{
textArea.setText("");
}
else if(eventSource == menuItem[1])//OpenItem
{
openFileDialog.show();
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
{
openFile(fileName);
}
}
else if(eventSource ==menuItem[2])//SaveItem
{
saveFileDialog.show();
fileName = saveFileDialog.getDirectory()+saveFileDialog.getFile();
if(fileName !=null)
{
writeFile(fileName);
}
}
else if(eventSource==menuItem[3])//exitItem
{
System.exit(0);
}
else if(eventSource == menuItem[4])//Select All
{
textArea.selectAll();
}
else if(eventSource == menuItem[5])//copy
{
String text = textArea.getSelectedText();
StringSelection selection= new StringSelection(text);
clipboard.setContents(selection,null);
}
else if(eventSource == menuItem[6])//cut
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
textArea.replaceText("",textArea.getSelectionStart(),
textArea.getSelectionEnd());
}
else if(eventSource == menuItem[7])//Paste
{
Transferable contents = clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try{
text = (String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception ex){}
textArea.replaceText(text,
textArea.getSelectionStart(),textArea.getSelectionEnd());
}
else if(eventSource == menuItem[8])
{
// JOptionPane.showMessageDialog(null,"This is a MiniEdit.");
}
}
//Read 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(Exception e)
{
System.out.println("Error opening file!");
}
}
//write file
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!");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |