zoukankan      html  css  js  c++  java
  • 制作一个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();
    	}
    }
    

      

  • 相关阅读:
    ArcGIS.Server.9.3.DotNet的ADF与ASP.NET AJAX
    ArcGIS.Server.9.3和ArcGIS API for JavaScript保存自定义图形(十)
    android开发我的新浪微博客户端载入页面UI篇(1.1)
    ArcGIS.Server.9.3和ArcGIS API for JavaScript实现距离量算和面积量算(九)
    ArcGIS.Server.9.3和ArcGIS API for JavaScript实现点、线、面的buffer分析(十一)
    android开发我的新浪微博客户端载入页面sqlite篇(1.2)
    市净率PB
    转 未来的GDI:WPF技术纵览
    转 LUA语言学习教程
    sl下的两种跨线程访问方式
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11862836.html
Copyright © 2011-2022 走看看