1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | import java.io.File ; import java.io.FileInputStream ; import java.io.FileOutputStream ; import java.io.PrintStream ; import java.util.Scanner ; import java.awt.BorderLayout ; import java.awt.event.WindowAdapter ; import java.awt.event.WindowEvent ; import java.awt.event.ActionEvent ; import java.awt.event.ActionListener ; import javax.swing.JFrame ; import javax.swing.JTextArea ; import javax.swing.JLabel ; import javax.swing.JButton ; import javax.swing.JPanel ; import javax.swing.JFileChooser ; import javax.swing.JScrollPane ; class Note implements ActionListener{ private JTextArea area = new JTextArea( 8 , 10 ) ; // 定义文本区 private JFrame frame = new JFrame( "Welcome To MLDN" ) ; private JButton open = new JButton( "打开文件" ) ; private JButton save = new JButton( "保存文件" ) ; private JLabel label = new JLabel( "现在没有打开的文件" ) ; private JPanel butPan = new JPanel() ; public Note(){ this .butPan.add(open) ; // 在面板中加入按钮 this .butPan.add(save) ; // 在面板中加入按钮 this .frame.setLayout( new BorderLayout( 3 , 3 )) ; this .frame.add( this .label,BorderLayout.NORTH) ; this .frame.add( this .butPan,BorderLayout.SOUTH) ; this .frame.add( new JScrollPane( this .area),BorderLayout.CENTER) ; this .frame.setSize( 330 , 180 ) ; this .frame.setVisible( true ) ; this .frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit( 1 ) ; } } ) ; this .open.addActionListener( this ) ; this .save.addActionListener( this ) ; } public void actionPerformed(ActionEvent e){ File file = null ; // 接收文件 int result = 0 ; // 接收操作状态 JFileChooser fileChooser = new JFileChooser() ; // 文件选择框 if (e.getSource()== this .open){ // 表示执行的是打开操作 this .area.setText( "" ) ; // 打开将文字区域的内容清空 fileChooser.setApproveButtonText( "确定" ) ; fileChooser.setDialogTitle( "打开文件" ) ; result = fileChooser.showOpenDialog( this .frame) ; if (result==JFileChooser.APPROVE_OPTION){ // 选择的是确定按钮 file = fileChooser.getSelectedFile() ; // 得到选择的文件 this .label.setText( "打开的文件名称为:" + file.getName()) ; } else if (result==JFileChooser.CANCEL_OPTION){ this .label.setText( "没有选择任何文件" ) ; } else { this .label.setText( "操作出现错误" ) ; } if (file!= null ){ try { Scanner scan = new Scanner( new FileInputStream(file)) ; scan.useDelimiter( "\n" ) ; while (scan.hasNext()){ this .area.append(scan.next()) ; this .area.append( "\n" ) ; } scan.close() ; } catch (Exception e1){} } } if (e.getSource()== this .save){ // 判断是否是保存操作 result = fileChooser.showSaveDialog( this .frame) ; // 显示保存框 if (result==JFileChooser.APPROVE_OPTION){ // 选择的是确定按钮 file = fileChooser.getSelectedFile() ; // 得到选择的文件 this .label.setText( "选择的存储文件名称为:" + file.getName()) ; } else if (result==JFileChooser.CANCEL_OPTION){ this .label.setText( "没有选择任何文件" ) ; } else { this .label.setText( "操作出现错误" ) ; } if (file!= null ){ try { PrintStream out = new PrintStream( new FileOutputStream(file)) ; out.print( this .area.getText()) ; out.close() ; } catch (Exception e1){} } } } } public class JFileChooserDemo{ public static void main(String args[]){ new Note() ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通