我还没有学习JDBC(是这个拼法么?)
我还没有学习JDBC(是这个拼法么?)
这是我以前写过的记事本(功能虽不全,但应该有你想要的东西)
package io;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextBook extends JFrame implements ActionListener {
static JTextArea theArea = null; //文本输入的地方
JMenu themenu = null; //菜单
boolean colorchanged = true; // 控制自动换行的,因为不会像windows的那样打个勾
BufferedReader bf = null; //读
BufferedWriter bw = null; //写
String str = null; //好像没什么用
static FileDialog openfile = null, savefile = null; //调用打开,保存对话框
public TextBook() {
super("记事本----しΟν∈→鱈制作");
theArea = new JTextArea();
theArea.setEditable(true);
Container cp = getContentPane();
cp.add(new JScrollPane(theArea));
// cp.setLayout(new BorderLayout());
JMenuBar MBar = new JMenuBar();
MBar.setOpaque(true);
JMenu mfile = buildFileMenu();
mfile.setOpaque(true);
MBar.add(mfile);
JMenu medite = buildEditeMenu();
medite.setOpaque(true);
MBar.add(medite);
JMenu mformat = buildFormatMenu();
mformat.setOpaque(true);
MBar.add(mformat);
setJMenuBar(MBar); //都是菜单
openfile = new FileDialog(this, "打开对话框", FileDialog.LOAD);
openfile.setVisible(false);
savefile = new FileDialog(this,"保存对话框",FileDialog.SAVE);
savefile.setVisible(false);
}
public JMenu buildFileMenu() {
themenu = new JMenu("文件");
JMenuItem newf = new JMenuItem("新建");
newf.addActionListener(this);
JMenuItem open = new JMenuItem("打开");
open.addActionListener(this);
JMenuItem save = new JMenuItem("保存");
save.addActionListener(this);
JMenuItem close = new JMenuItem("关闭");
close.addActionListener(this);
JMenuItem quit = new JMenuItem("退出");
quit.addActionListener(this);
themenu.add(newf);
themenu.add(open);
themenu.add(save);
themenu.add(close);
themenu.addSeparator();
themenu.add(quit);
return themenu;
}
public JMenu buildEditeMenu() {
themenu = new JMenu("编辑");
JMenuItem copy = new JMenuItem("复制");
JMenuItem cut = new JMenuItem("剪切");
JMenuItem paste = new JMenuItem("粘帖");
themenu.add(copy);
themenu.add(cut);
themenu.add(paste);
return themenu;
}
//上面的不要解释吧,就是搞菜单的
public JMenu buildFormatMenu() {
themenu = new JMenu("格式");
JMenuItem format = new JMenuItem("自动换行");
format.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// theArea.setLineWrap(true);
// theArea.setWrapStyleWord(true);
// boolean colorchanged = true;
if (colorchanged) {
((JMenuItem) e.getSource()).setForeground(Color.red);
theArea.setLineWrap(true);
theArea.setWrapStyleWord(true);
} else {
((JMenuItem) e.getSource()).setForeground(Color.black);
theArea.setLineWrap(false);
theArea.setWrapStyleWord(false);
}
colorchanged = !colorchanged;
}
});
JMenuItem fontSize = new JMenuItem("字体");
fontSize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String size = JOptionPane.showInputDialog(new JFrame(),
"输入你要的字体大小", "字体选择", JOptionPane.YES_NO_OPTION); //得到你输入的字体大小
theArea.setFont(new Font(null, Font.BOLD,
Integer.valueOf(size)));
}
});
themenu.add(format);
themenu.add(fontSize);
return themenu;
}
public void actionPerformed(ActionEvent e) {
//if(e.getSource.equals("new")) //可以这个来判断你按的那个菜单,所以说str没用
//新建事件
str = e.getActionCommand();
if(str == "新建") {
if( !theArea.getText().equals("") ) {
int result = JOptionPane.showConfirmDialog(new JFrame(), "Text" +
"的文字已改变,要保存吗?", "记事本", JOptionPane.YES_NO_CANCEL_OPTION);
// 显示一个对话框,包括确定,否和取消三个按钮,result记录你点的是那个键,根据你的选择判断下一步
if( result == JOptionPane.YES_OPTION ) //{
saveMethod(); //调用保存事件方法
theArea.setText("");
/*}
else if( result == JOptionPane.NO_OPTION )
theArea.setText(""); //消空笔记本*/
}
}
//打开事件
else if(str == "打开") {
openfile.setVisible(true);
try {
File readfile = new File(openfile.getDirectory(), openfile.getFile());
setTitle(readfile.getName() + "----记事本");
theArea.setText("");
bf = new BufferedReader(new FileReader(readfile));
while(bf.readLine() != null) {
theArea.append(bf.readLine() + "\n");
}
bf.close();
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
else if(str == "保存")
saveMethod(); //调用保存事件方法
else if(str == "关闭" ||str == "退出")
System.exit(0);
}
//保存事件方法
public void saveMethod() {
savefile.setVisible(true);
try {
File writefile = new File(savefile.getDirectory(), savefile.getFile());
bw = new BufferedWriter( new FileWriter(writefile));
bw.write(theArea.getText(), 0, (theArea.getText().length()));
bw.flush();
bw.close();
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(this,"警告","未找到该文件或文件发生异常",JOptionPane.ERROR_MESSAGE);
} catch (IOException e1) {
e1.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
JFrame jf = new TextBook();
jf.setSize(new Dimension(800, 600));
jf.addWindowListener(new WindowAdapter() { //关闭事件
public void windowClosing(WindowEvent e) {
// System.exit(0);
if (!theArea.getText().equals("")) {
int result = JOptionPane.showConfirmDialog(new JFrame(),
"Text" + "的文字已改变,要保存吗?", "记事本",
JOptionPane.YES_NO_CANCEL_OPTION); //一个选择对话框,和前面的一样
if( result == JOptionPane.YES_OPTION )
new TextBook().saveMethod(); //调用保存事件方法
}
System.exit(0);
}
});
jf.setVisible(true);
}
}
下面是人家给我的一段代码:
新建的话用setDocument(new PlainDocument())就可以了!
保存的话用这个代码
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(Notebook.this, "保存");
if (returnVal == JFileChooser.APPROVE_OPTION) {
String savefile = fc.getSelectedFile().toString();
try {
BufferedWriter br = new BufferedWriter(new FileWriter(
savefile));
br.write(jta.getText());
//Print;
br.flush();
br.close();
} catch (Exception ex) {
}