📄 save.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package drawfigure;import java.io.IOException; import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.*;import java.awt.event.*; import java.io.File;import java.io.FileOutputStream;import java.io.PrintWriter;public class Save extends JFrame{ JLabel fname; JLabel path; JTextField jTextFieldfname; JTextField jTextFieldpath; JButton save; JButton exit; Save(final DrawFigure draw) { this.setLayout(null);//设置布局管理器为null File file=new File("画图文件"); if(!file.exists()) file.mkdir(); //初始化各个控件 fname=new JLabel("文件名"); path=new JLabel("保存路径"); jTextFieldfname=new JTextField("未命名.txt"); jTextFieldpath=new JTextField(file.getAbsolutePath()); save= new JButton("保存"); exit=new JButton("取消"); //设置控件位置 this.fname.setBounds(10,10,80,20); this.path.setBounds(10,50,80,20); this.jTextFieldfname.setBounds(100,10,200,20); this.jTextFieldpath.setBounds(100,50,200,20); this.save.setBounds(30, 100, 100, 30); this.exit.setBounds(200, 100, 100, 30); //将控件添加到面板上 this.add(fname); this.add(path); this.add(jTextFieldfname); this.add(jTextFieldpath); this.add(save); this.add(exit); this.setBounds(330,300,350,200); this.setTitle("保存"); this.setVisible(true); save.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { String p,f;int ju=0; p=jTextFieldpath.getText(); f=jTextFieldfname.getText(); File pa=new File(p); if(!pa.isDirectory()){ JOptionPane.showMessageDialog(null ,"路径非法","错误",JOptionPane.INFORMATION_MESSAGE);} else { try { if (!pa.exists()) { pa.mkdir(); } File f1 = new File(pa, f); if(f1.exists()) { ju=JOptionPane.showConfirmDialog(null,"该文件已存在,是否覆盖?", "警告",JOptionPane.INFORMATION_MESSAGE); //0-是,1-否,2-取消。 } if(ju==0){ draw.saved=true; f1.createNewFile(); PrintWriter output = new PrintWriter(new FileOutputStream(f1)); for (int i = 0; i < draw.stack.size(); i++) { output.write(String.valueOf(draw.stack.get(i).i)+'\t'); output.write(String.valueOf(draw.stack.get(i).sx)+'\t'); output.write(String.valueOf(draw.stack.get(i).sy)+'\t'); output.write(String.valueOf(draw.stack.get(i).x)+'\t'); output.write(String.valueOf(draw.stack.get(i).y)+'\t'); output.write(String.valueOf(draw.stack.get(i).c.getBlue())+'\t'); output.write(String.valueOf(draw.stack.get(i).c.getGreen())+'\t'); output.write(String.valueOf(draw.stack.get(i).c.getRed())+'\t'); output.write(String.valueOf(draw.stack.get(i).fill)+'\t'+'\r'+'\n'); } output.close();setVisible(false); } if(ju==2) { draw.saved=false; setVisible(false); draw.repaint(); } } catch (IOException ex) { Logger.getLogger(Save.class.getName()).log(Level.SEVERE, null, ex); } } draw.repaint(); } }); exit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) { draw.saved=false; setVisible(false); draw.repaint(); } }); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -