记事本(JAVA)
本次设计实现了记事本部分功能,其实涉及了io流以及swing里面的相关操作,你们的关注就是给我最大的鼓励
package com.zking.swing3;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.IOException;import javax.swing.ButtonGroup;import javax.swing.JCheckBoxMenuItem;import javax.swing.JComponent;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPopupMenu;import javax.swing.JRadioButtonMenuItem;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.KeyStroke;import javax.swing.ScrollPaneConstants;public class MyNotepad extends JFrame{
//菜单栏
private JMenuBar jmb = new JMenuBar();
//菜单
private JMenu jmFile = new JMenu("文件(F)"); private JMenu jmEdit = new JMenu("编辑(E)"); private JMenu jmFormat= new JMenu("格式(O)"); private JMenu jmFind = new JMenu("查看(V)"); private JMenu jmHelp = new JMenu("帮助(H)"); static JTextArea jta = new JTextArea(); // 文本域
private JScrollPane jsp = new JScrollPane(jta); //滚动面板
//菜单项 //加入到 文件项
private JMenuItem jmiNew = new JMenuItem("新建"); private JMenuItem jmiOpen = new JMenuItem("打开");
private JMenuItem jmiSive = new JMenuItem("保存");
private JMenuItem jmiOtherSive = new JMenuItem("另存为");
private JMenuItem jmiexit = new JMenuItem("退出(D)"); //加入到 格式项
private JCheckBoxMenuItem autoLine = new JCheckBoxMenuItem("自动换行"); //复选框
private JMenuItem jmieFon= new JMenuItem("字体"); //加入到 编辑项
private JRadioButtonMenuItem jrMen = new JRadioButtonMenuItem("男"); //单选框
private JRadioButtonMenuItem jrWomen = new JRadioButtonMenuItem("女");
private ButtonGroup bp = new ButtonGroup(); private JFileChooser jfc = new JFileChooser(); //选择文件
//上下文菜单
private JPopupMenu jpm = new JPopupMenu(); private JMenuItem jmiCopy = new JMenuItem("复制");
private JMenuItem jmiPaste = new JMenuItem("粘贴");
public MyNotepad() {
this.setTitle("记事本");
this.setSize(400, 300); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); jmb.add(jmFile);
jmb.add(jmEdit);
jmb.add(jmFormat);
jmb.add(jmFind);
jmb.add(jmHelp); this.add(BorderLayout.NORTH,jmb); //将菜单栏加入面板
//文件项
jmFile.add(jmiNew);
jmFile.add(jmiOpen);
jmFile.add(jmiSive); jmFile.add(jmiOtherSive); jmFile.addSeparator(); //分割线
jmFile.add(jmiexit); //格式项
jmFormat.add(autoLine); jmFormat.add(jmieFon);//
jmieFon.add(); //编辑项 jmEdit.add(jrMen);
jmEdit.add(jrWomen);
bp.add(jrMen);
bp.add(jrWomen);
//文本域中加入上下文菜单
jta.add(jpm);
jpm.add(jmiCopy);
jpm.add(jmiPaste); //设置滚动面板
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED); this.add(BorderLayout.CENTER,jsp); this.setVisible(true); //显示窗口 //新建
jmiNew.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) { new MyNotepad(); MyNotepad.this.dispose();
}
});
//打开
jmiOpen.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) { int choose = jfc.showOpenDialog(MyNotepad.this); switch(choose) {
case JFileChooser.APPROVE_OPTION: //点击打开
JOptionPane.showMessageDialog(null, "文件错误"); break;
case JFileChooser.CANCEL_OPTION: //点击取消
break;
}
}
});
//保存
jmiSive.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) { try {
new NotepadSive(); //文件保存类
} catch (IOException e1) { e1.printStackTrace();
}
}
});
//另存为
jmiOtherSive.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { int choose = jfc.showOpenDialog(MyNotepad.this); switch(choose) {
case JFileChooser.APPROVE_OPTION: //点击保存
JOptionPane.showMessageDialog(null, "保存失败"); //提高可读性
break;
case JFileChooser.CANCEL_OPTION: //点击取消
break;
}
}
});
//退出
jmiexit.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) { System.exit(0); } });
//退出快捷键
jmiexit.registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { System.exit(0);
}
};KeyStroke.getKeyStroke(KeyEvent.VK_D,KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW); //自动换行
autoLine.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) { if(autoLine.isSelected()) {
//isSelected()方法为boolean类型,表示是否选中
jta.setLineWrap(true); //设置文本域可自动换行
}else {
jta.setLineWrap(false); //取消自动换行 }
}
});
jta.addMouseListener(new MouseAdapter() { //上下文菜单
@Override
public void mouseClicked(MouseEvent e) { if(e.getButton() == 3) { //监听鼠标右击
jpm.show(jta, e.getX(), e.getY()); //显示菜单,及其位置
}
}
});
}
public static void main(String[] args) {
new MyNotepad();
}}
保持文件(io):
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class NotepadSive {
public NotepadSive() throws IOException { File fl = new File("D:\\360downloads\\wpcache\\笔记本.text"); //如果没有该文件就会创建一个新的
FileOutputStream fos = new FileOutputStream(fl,true); //字节流中的输入流
BufferedOutputStream bos = new BufferedOutputStream(fos); //输入流对应的缓冲流
byte[] by = MyNotepad.jta.getText().getBytes(); bos.write(by); //写入byte数组
bos.flush();
bos.close();
fos.close(); //关闭流
}}