📄 createnew.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package stageexperiment;import java.io.IOException; import javax.swing.*;import java.awt.event.*; import java.io.File;import java.io.FileOutputStream;import java.io.PrintWriter;import java.util.Date;import java.util.logging.Level;import java.util.logging.Logger;/** * * @author lina */public class CreateNew extends JFrame{ String t; String w; String f; String p; String k; String r; Date date=new Date(System.currentTimeMillis()); JLabel topic; JLabel writer; JLabel filename; JLabel path; JLabel keyword; JLabel remark; JTextField jTextFieldtopic; JTextField jTextFieldwriter; JTextField jTextFieldfilename; JTextField jTextFieldpath; JTextField jTextFieldkeyword; JTextField jTextFieldremark; JButton save; JButton exit; JTextArea text; Notes n=new Notes(); String tt; public CreateNew(final AllNotes an,final Main m) throws IOException { this.setLayout(null);//设置布局管理器为null File file=new File("笔记文件"); if(!file.exists()) file.mkdir(); //初始化各个控件 topic=new JLabel("题目"); writer=new JLabel("作者"); filename=new JLabel("笔记文件名"); path=new JLabel("路径"); keyword=new JLabel("关键词"); remark=new JLabel("备注"); jTextFieldtopic=new JTextField(); jTextFieldwriter=new JTextField(); jTextFieldfilename=new JTextField(); jTextFieldpath=new JTextField(file.getPath()); jTextFieldkeyword=new JTextField(); jTextFieldremark=new JTextField(); text=new JTextArea(); save= new JButton("保存"); exit=new JButton("取消"); text.setMaximumSize(text.getPreferredSize()); text.setLineWrap(true); //设置控件位置 this.topic.setBounds(10,10,80,20); this.filename.setBounds(10,50,80,20); this.keyword.setBounds(10,130,80,20); this.path.setBounds(10,170,80,20); this.remark.setBounds(10,210,80,20); this.writer.setBounds(10,90,80,20); this.jTextFieldtopic.setBounds(100,10,100,20); this.jTextFieldfilename.setBounds(100,50,100,20); this.jTextFieldkeyword.setBounds(100,130,100,20); this.jTextFieldpath.setBounds(100,170,100,20); this.jTextFieldremark.setBounds(100,210,100,20); this.jTextFieldwriter.setBounds(100,90,100,20); this.save.setBounds(30, 250, 100, 30); this.exit.setBounds(150, 250, 100, 30); text.setBounds(260,10,330,300); //将控件添加到面板上 this.add(topic); this.add(filename); this.add(keyword); this.add(path); this.add(remark); this.add(writer); this.add(jTextFieldtopic); this.add(jTextFieldfilename); this.add(jTextFieldkeyword); this.add(jTextFieldpath); this.add(jTextFieldremark); this.add(jTextFieldwriter); this.add(save); this.add(exit); this.add(text); this.setBounds(330,300,610,350); this.setTitle("创建新笔记"); this.setVisible(true); save.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { int i; t=jTextFieldtopic.getText().trim();if(t.equals("")) t=null; w=jTextFieldwriter.getText().trim();if(w.equals("")) w=null; f=jTextFieldfilename.getText().trim();if(f.equals("")) f=null; p=jTextFieldpath.getText().trim();if(p.equals("")) p=null; k=jTextFieldkeyword.getText().trim();if(k.equals("")) k=null; r=jTextFieldremark.getText().trim();if(r.equals("")) r=null; tt=text.getText(); // System.out.println(tt); if(f==null) { JOptionPane.showMessageDialog(null,"文件名不能为空","错误",JOptionPane.INFORMATION_MESSAGE);i=1;} else { if(p==null) { JOptionPane.showMessageDialog(null,"路径不能为空","错误",JOptionPane.INFORMATION_MESSAGE);i=1;} else { i=0; for(int j=0;j<an.getLength();j++) { String s1,s2; File ff1=new File(an.all[j].path,an.all[j].filename); File ff2=new File(p,f); s1=ff1.getAbsolutePath();s2=ff2.getAbsolutePath(); if(f.equals(an.all[j].filename)&&s1.equals(s2)) {i=1; JOptionPane.showMessageDialog(null,"笔记文件重名,请重新输入笔记文件名或路径","文件已存",JOptionPane.INFORMATION_MESSAGE); jTextFieldfilename.setText("");t=""; } } } } if(i==0) { n.topic=t; n.filename=f; n.ftime=date.toString(); n.keyword=k; n.path=p; n.remark=r; n.writer=w; JOptionPane.showMessageDialog(null,"新建成功","新建完毕",JOptionPane.INFORMATION_MESSAGE); File p=new File(n.path); if(!p.isDirectory()){ JOptionPane.showMessageDialog(null ,"路径非法","错误",JOptionPane.INFORMATION_MESSAGE);} else { try { if (!p.exists()) { p.mkdir(); } File file = new File(p, n.filename); file.createNewFile(); PrintWriter putfile=new PrintWriter(new FileOutputStream(file)); putfile.print(tt); putfile.close(); m.allnotes.add(n); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } setVisible(false); } } }); exit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { setVisible(false); } }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -