學了JAVA一個月.就寫了個NotePad.由於時間關係.要實訓了,很多功能沒加上去,只實現了簡單的介面和最基本上的功能.
以後有時間再完善吧..
=================================================================================
/*貓貓..第一個Java程式
*
*
*copyright 貓貓
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
public class Notepad extends JFrame
{
String openFilePath;
String openFileName;
String title="ERROR MESSAGE";
int type=JOptionPane.ERROR_MESSAGE;
public Notepad()
{
super("記事本");
final JTextArea text = new JTextArea();
text.setToolTipText("請鍵入內容");
//介面
//退出事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//簡單的布局
final JPanel panel=new JPanel();
panel.setLayout(new GridLayout(1,1));
panel.add(new JScrollPane(text));
this.getContentPane().add(panel);
//功能表項目
JMenuBar Mbar = new JMenuBar();
this.setJMenuBar(Mbar);
JMenu file = new JMenu("檔案");
JMenu edit = new JMenu("編輯");
JMenu help = new JMenu("協助");
Mbar.add(file);
Mbar.add(edit);
Mbar.add(help);
JMenuItem newFile = new JMenuItem("建立");
newFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.setText(" ");
}
});
//布局結束
//建立檔案
newFile.setMnemonic('N');
newFile.setAccelerator( KeyStroke.getKeyStroke('N',java.awt.Event.CTRL_MASK,true));
//開啟檔案
JMenuItem open = new JMenuItem("開啟");
open.setMnemonic('O');
open.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,true));
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser openfile = new JFileChooser();
openfile.setDialogTitle("開啟檔案");
openfile.setApproveButtonText("開啟");
openfile.showOpenDialog(panel);
File filename = openfile.getSelectedFile();
StringBuffer strBF = new StringBuffer();
String error_message = "Error";
FileInputStream inputfile = null;
try{
char buffer[] = new char[1024];
inputfile = new FileInputStream(filename);
int len = 0;
FileReader in = new FileReader(filename.getAbsoluteFile());
while((len = in.read(buffer)) != -1)
{
strBF.append(buffer , 0 , len);
}
inputfile.close();
text.setText(strBF.toString());
String openfilename = filename.getName();
setTitle(openfilename);
}
catch(IOException ioEX)
{
JOptionPane.showMessageDialog(panel,error_message,title,type);
}
}});
//儲存檔案
JMenuItem save = new JMenuItem("儲存");
save.setMnemonic('S');
save.setAccelerator(KeyStroke.getKeyStroke('S',java.awt.Event.CTRL_MASK,true));
save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JFileChooser savefile=new JFileChooser();
savefile.setApproveButtonText("儲存");
savefile.setDialogTitle("儲存檔案");
savefile.showSaveDialog(panel);
File filesa=savefile.getSelectedFile();
String file_notfound_message="找不到檔案";
FileOutputStream outputfile=null;
//處理異常開始
try
{
outputfile = new FileOutputStream(filesa);
}
catch(FileNotFoundException fe)
{
JOptionPane.showMessageDialog(panel,file_notfound_message,title,type);
}
String filecontent=text.getText();
String write_error_message="寫檔案錯誤";
try
{
outputfile.write(filecontent.getBytes());
}
catch(IOException ioEx)
{
JOptionPane.showMessageDialog(panel,write_error_message,title,type);
}
String cmessage="關閉錯誤";
try
{
outputfile.close();
}
catch(IOException ioEx)
{
JOptionPane.showMessageDialog(panel,cmessage,title,type);
}
}
}
);
//退出
JMenuItem exit = new JMenuItem("退出");
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
exit.setMnemonic('Q');
exit.setAccelerator(KeyStroke.getKeyStroke('Q',java.awt.Event.CTRL_MASK,true));
//尋找
JMenuItem find = new JMenuItem("尋找");
find.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//因為課程太緊.所以尋找功能沒時間加上去了.^_^
}
});
find.setMnemonic('F');
find.setAccelerator(KeyStroke.getKeyStroke('F',java.awt.Event.CTRL_MASK,true));
//剪下
JMenuItem cut = new JMenuItem("剪下");
cut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.cut();
}
});
cut.setMnemonic('C');
cut.setAccelerator(KeyStroke.getKeyStroke('C',java.awt.Event.CTRL_MASK,true));
//複製
JMenuItem copy = new JMenuItem("複製");
copy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
text.copy();
}
});
copy.setMnemonic('o');
copy.setAccelerator(KeyStroke.getKeyStroke('O',java.awt.Event.CTRL_MASK,true));
//粘貼
JMenuItem paste = new JMenuItem("粘貼");
paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
text.paste();
}});
paste.setMnemonic('P');
paste.setAccelerator(KeyStroke.getKeyStroke('P',java.awt.Event.CTRL_MASK,true));
JMenuItem about = new JMenuItem("關於");
about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int type=JOptionPane.INFORMATION_MESSAGE;
String title="關於";
String message="Make by cat lee";
JOptionPane.showMessageDialog(panel,message,title,type);
}});
file.add(newFile);
file.add(open);
file.add(save);
file.addSeparator();
file.add(exit);
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
help.add(about);
}
public static void main(String[] args) {
Notepad notepad = new Notepad();
notepad.setSize(640, 480);
notepad.setVisible(true);
notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}