• 制作一个Frame用于保存文件和打开文件


    import java.awt.BorderLayout;
    
    
    public class FrameMenu extends JFrame {
    
    	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    	/**
    	 * Launch the application.
    	 */
    	private JFileChooser chooser;
    	private JTextArea textArea;
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					FrameMenu frame = new FrameMenu();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public FrameMenu() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 612, 447);
    		getContentPane().setLayout(null);
    		getContentPane().setLayout(new BorderLayout(0, 0));
    		
    		JScrollPane scrollPane = new JScrollPane();
    		scrollPane.setBounds(131, 69, 2, 2);
    		getContentPane().add(scrollPane);
    		
    		textArea = new JTextArea();
    		scrollPane.setViewportView(textArea);
    		
    		JMenuBar menuBar = new JMenuBar();
    		setJMenuBar(menuBar);
    		
    		JMenu mnNewMenu = new JMenu("文件");
    		menuBar.add(mnNewMenu);
    		
    		JMenuItem mntmNewMenuItem = new JMenuItem("打开");
    		mntmNewMenuItem.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				try {
    					showdig();
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    			}
    		});
    		mnNewMenu.add(mntmNewMenuItem);
    		
    		JMenuItem mntmNewMenuItem_1 = new JMenuItem("保存");
    		mntmNewMenuItem_1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				try {
    					savedig();
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    			}
    		});
    		mnNewMenu.add(mntmNewMenuItem_1);
    	}
    
    	public void showdig() throws IOException {
    		chooser = new JFileChooser();
    		int boo = chooser.showOpenDialog(this);
    		if(boo == JFileChooser.CANCEL_OPTION)
    		{
    			System.out.println("哥们你没有选择文件");
    			return ;
    		}
    		File file = chooser.getSelectedFile();
    		@SuppressWarnings("resource")
    		BufferedReader bfr = new BufferedReader(new FileReader(file));
    		
    		textArea.setText("");;
    		String line = null;
    		while((line = bfr.readLine()) != null)
    		{
    			textArea.append(line + LINE_SEPARATOR);
    		}
    		
    	}
    
    	public void savedig() throws IOException {
    		chooser = new JFileChooser();
    		int chr = chooser.showSaveDialog(this);
    		if(chr == JFileChooser.CANCEL_OPTION)
    		{
    			System.out.println("没有指定文件");
    			return ;
    		}
    		File file = chooser.getSelectedFile();
    		
    		BufferedWriter bfw = new BufferedWriter(new FileWriter(file));
    		String str = textArea.getText();
    		bfw.write(str);
    		bfw.close();
    	}
    }
    

      

  • 相关阅读:
    要学习编程?这10件事情你知道了吗?
    JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
    这8个免费的网上课程可以有助你的技术成长
    给游戏开发初学者的10条建议
    21个国外受欢迎的学习编程的网站:总有一个理由会让你爱上它们
    hibernate 知识梳理
    struts2 知识梳理
    maven 相关
    c#配置log4net步骤
    arcobject 相关
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11862836.html
Copyright © 2020-2023  润新知