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();
    	}
    }
    

      

  • 相关阅读:
    .NET Core 玩一玩 Ocelot API网关
    VUE.js 中取得后台原生HTML字符串 原样显示问题
    简单了解 iTextSharp实现HTML to PDF
    ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下
    C# AutoMapper 了解一下
    玩一玩基于Token的 自定义身份认证+权限管理
    ASP.NET MVC5 实现基于Quartz.NET任务调度
    ASP.NET MVC5 使用NPOI导出ExceL 返回浏览器下载
    [python][openpyxl]读取excel中公式的结果值
    Python实例001:实现识别图片中的文字
  • 原文地址:https://www.cnblogs.com/WINDZLY/p/11862836.html
Copyright © 2011-2022 走看看