import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import javax.swing.*;
@SuppressWarnings("serial")
public class Notepad extends JFrame {
Notepad notepad;
static int size=12;
int i,j,k,m,n;
JTextArea textarea;
String file = "";
FileDialog fileload = new FileDialog(this, "打开文件", FileDialog.LOAD);
FileDialog filesave = new FileDialog(this, "保存文件", FileDialog.SAVE);
Denglu denglu;
public Notepad(){
super("记事本");
denglu=new Denglu(this,"登陆",true);
denglu.setVisible(true);
setSize(400, 400);
try {
JMenuBar menuBar=new JMenuBar();
setJMenuBar(menuBar);//设置菜单栏
//****设置下拉式菜单
//文件
JMenu menu1=new JMenu("文件");
final JMenuItem[] menuItem1={ new JMenuItem("打开"),new JMenuItem("保存"),
getsavefilenamenew JMenuItem("另存为"),new JMenuItem("退出")
};
for(i=0;i<menuItem1.length;i++){
menu1.add(menuItem1[i]);
menuItem1[i].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JMenuItem mi1=(Source();
ActionCommand().equals("打开")){
openFile();
}
else ActionCommand().equals("保存")){
saveFile();
}
else ActionCommand().equals("另存为")){
saveAs();
}
else ActionCommand().equals("退出")){
}
}
});
}
//编辑
JMenu menu2=new JMenu("编辑");
final JMenuItem[] menuItem2={new JMenuItem("复制"),new JMenuItem("剪切"),new JMenuItem("粘贴")
};
for(j=0;j<menuItem2.length;j++){
menu2.add(menuItem2[j]);
menuItem2[j].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JMenuItem mi2=(Source();
ActionCommand().equals("复制")){
}
else ActionCommand().equals("剪切")){
textarea.cut();
}
else ActionCommand().equals("粘贴")){
textarea.paste();
}
}
});
}
//格式
JMenu menu3=new JMenu("格式");
JMenu menu3_1=new JMenu("自动换行");
final JMenuItem[] menuItem3_1={new JMenuItem("是"),new JMenuItem("否")
};
for(k=0;k<menuItem3_1.length;k++){
menu3_1.add(menuItem3_1[k]);
menuItem3_1[k].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JMenuItem mi3_1=(Source();
if(ActionCommand().equals("是")){
textarea.setLineWrap(true);
}
else if(ActionCommand().equals("否")){
textarea.setLineWrap(false);
}
}
});
}
/
/字号
JMenu menu3_2=new JMenu("字号");
final JMenuItem[] menuItem3_2={new JMenuItem("10号"),new JMenuItem("15号"),new JMenuItem("20号"),
new JMenuItem("25号"),new JMenuItem("30号"),new JMenuItem("40号"),
new JMenuItem("放大"),new JMenuItem("缩小"),new JMenuItem("重设")
};
for(m=0;m<menuItem3_2.length;m++){
menu3_2.add(menuItem3_2[m]);
menuItem3_2[m].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JMenuItem mi3_2=(Source();
if(ActionCommand().equals("10号")){
size=10;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
else if(ActionCommand().equals("15号")){
size=15;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
else if(ActionCommand().equals("20号")){
size=20;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
else if(ActionCommand().equals("25号")){
size=25;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
else if(ActionCommand().equals("30号")){
size=30;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
else if(ActionCommand().equals("40号")){
size=40;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
else if(ActionCommand().equals("放大")){
size=size+1;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
else if(ActionCommand().equals("缩小")){
size=size-1;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
else if(ActionCommand().equals("重设")){
size=12;
textarea.setFont(new Font("Default",Font.PLAIN,size));
}
}
});
}
//颜
JMenu menu3_3=new JMenu("颜");
final JMenuItem[] menuItem3_3={new JMenuItem("red"), new JMenuItem("black"),new JMenuItem("blue"),
new JMenuItem("orange"),new JMenuItem("cyan"), new JMenuItem("green")
};
for(n=0;n<menuItem3_3.length;n++){
menu3_3.add(menuItem3_3[n]);
menuItem3_3[n].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JMenuItem mi3_3=(Source();
if(ActionCommand().equals("red")){
textarea.d);
}
else if(ActionCommand().equals("black")){
textarea.setForeground(Color.black);
}
else if(ActionCommand().equals("blue")){
textarea.setForeground(Color.blue);
}
else if(ActionCommand().equals("orange")){
textarea.ange);
}
else if(ActionCommand().equals("cyan")){
textarea.an);
}
else if(ActionCommand().equals("green")){
textarea.);
}
}
});
}
menu3.add(menu3_1);
menu3.add(menu3_2);
menu3.add(menu3_3);
//帮助
JMenu menu4=new JMenu("帮助");
final JMenuItem menuItem4 =new JMenuItem("关于");
menuItem4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JDialog dialog = new JDialog(notepad, "关于该记事本", true);
dialog.setLayout(new GridLayout(2,1));
dialog.setSize(300, 200);
dialog.setLocation(40+getX(), 50+getY());
dialog.setTitle("关于jdk");
ImageIcon imageIcon=new ImageIcon("ma.gif");
JLabel label=new JLabel("jdk1.6.0_21",imageIcon,JLabel.CENTER);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setToolTipText("作者");
dialog.add(label);
dialog.setVisible(true);
dialog.setResizable(false);
}
});
menu4.add(menuItem4);
//****创建文本域
textarea = new JTextArea();
JScrollPane scrollPane=new JScrollPane(textarea);
this.add(scrollPane);
//添加组件
menuBar.add(menu1);
menuBar.add(menu2);
menuBar.add(menu3);
menuBar.add(menu4);
this.setVisible(true);
} catch (Exception e1) {
e1.printStackTrace();
}
}
public void openFile(){//打开
fileload.setVisible(true);
String direct = Directory();
String filename = File();
if ((direct != null) && (filename != null)) {
file = direct + filename;
loadFile();
}
}
public void loadFile() {//打开
textarea.setText("");
super.setTitle(file + "-记事本");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String reader = br.readLine();
while (reader != null) {
textarea.append(reader + "\n");
reader = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
System.out.println("目标文件:" + file + "不到!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveFile() {//保存
String dangqianwenjian = Text();
try {
if (file.isEmpty()) {
filesave.setVisible(true);
String direct = Directory();
String filename = File();
if ((direct != null) && (filename != null)) {
file = direct + filename;
PrintWriter pw = new PrintWriter(new FileWriter(file));
pw.println(dangqianwenjian);
pw.flush();
pw.close();
super.setTitle(file + "-记事本");
}
} else {
PrintWriter pw = new PrintWriter(new FileWriter(file));
pw.println(dangqianwenjian);
pw.flush();
pw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveAs() {//另存为
filesave.setVisible(true);
St
ring direct = Directory();
String filename = File();
if ((direct != null) && (filename != null)) {
file = direct + filename;
saveFile();
super.setTitle(file + "-记事本");
}
}
public static void main(String[] args) {
Notepad notepad=new Notepad();
notepad.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
notepad.setResizable(true);
}
}
class Denglu extends Dialog implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
Label Lname ,Lpassword; //定义用户名和密码的标签
TextField Tname; //定义用户名的文本框
JPasswordField Tpassword; //定义密码的文本框。注:使用JPasswordField的好处就是输入的密码是以****显示给用户的
Panel panel1=new Panel(); //定义面板
Button Load,login; //定义登陆和注册的按钮
boolean b=true;
File file1=new File("用户名");
File file2=new File("密码"); //定义记录用户注册的用户名和密码的文件夹
Denglu(Frame f,String s,boolean b){
super(f,s,b);
Lname=new Label ("用户名");Lname.setBackground(Color.lightGray);//设置标签的背景颜
Lpassword=new Label ("密码");Lpassword.setBackground(Color.lightGray);//设置标签的背景颜
Tname=new TextField (10);
Tpassword=new JPasswordField (10);
Load=new Button("登陆");Load.setBackground(Color.lightGray);
login=new Button("注册");login.setBackground(Color.lightGray);
add(panel1,BorderLayout.CENTER);
setLayout(new CardLayout(3,2));
panel1.add(Lname);
panel1.add(Tname);
panel1.add(Lpassword);
panel1.add(Tpassword);
panel1.add(Load);Load.addActionListener(this);
panel1.add(login);login.addActionListener(this);
setSize(200,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
}
});
}
public void actionPerformed(ActionEvent e){
// 用户登录事件处理
if (e.getSource() == Load) {
String name1 = Text();
String password1 = new Password());
boolean flag = false;
String s1[]=new String[30];String s2[]=new String[30];int j=0; int index=0;
try{RandomAccessFile in3=new RandomAccessFile(file1,"rw");
String s=null;int x=0;in3.seek(0);
while((adLine())!=null){s1[x]=s;x=x+1;}
j=x;
in3.close();
RandomAccessFile in4=new RandomAccessFile(file2,"rw");
int y=0;
in4.seek(0);
while((adLine())!=null){s2[y]=s;y=y+1;}
in4.close();
}catch(Exception a){System.out.println("error2");}
for(int i=0;i<j+1;i++){if(name1.equals(s1[i]))
{flag=true;index=i;break;}
}
if(flag) {if(password1.equals(s2[index])){
JOptionPane.showMessageDialog(null, "恭喜您登陆成功!", "消息", JOptionPane.INFORMATION_MESSAGE);
setVisible(false);
}
else {
JOptionPane.showMessageDialog(null, "对不起您的用户名或密码错误!", "错误",JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(null, "对不起您的用户名或密码错误!", "错误",JOptionPane.ERROR_MESSAGE);
}
}
// 用户注册模块的事件处理
if (e.getSource() == login) {String s1[]=new String[30]; int j=0; boolean flag = false;
String NCmp = Text();
String PCmp = new Password());
try{RandomAccessFile in=new RandomAccessFile(file1,"rw");
String s=null;
while((adLine())!=null){s1[j]=s;j=j+1;}
in.close();
}catch(Exception a){System.out.println("error");}
for(int i=0;i<j+1;i++){if(NCmp.equals(s1[i]))
{flag=true;break;}
}
if (flag) {
JOptionPane.showMessageDialog(null, "对不起您的用户名已经注册!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "注册成功!", "消息", JOptionPane.INFORMATION_MESSAGE);
try{RandomAccessFile in1=new RandomAccessFile(file1,"rwd");
ists()){long length=file1.length();in1.seek(length);}
in1.writeBytes(NCmp+"\r\n");;in1.close();
RandomAccessFile in2=new RandomAccessFile(file2,"rwd");
ists()){long length2=file2.length();in2.seek(length2);}
in2.writeBytes(PCmp+"\r\n");
in2.close();
}catch(Exception b){System.out.println("error1");}
}
}
}}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论