首页 > 编程语言 >JavaSwing FileInput

JavaSwing FileInput

时间:2024-12-21 19:32:22浏览次数:6  
标签:jFrame fileChooser 窗体 设置 FileInput new JavaSwing JFileChooser

try
        {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
            //UIManager.put("RootPane.setupButtonVisible", false);
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            
        }
        catch(Exception e)
        {
            //TODO exception
        }
        
        // 创建窗体对象
        JFrame jFrame =new JFrame();
        // 设置窗体大小
        jFrame.setSize(800, 500);
        // 设置窗体全屏展示
        //jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        // 设置窗体显示位置
        //jFrame.setLocation(100,200);
        // 设置窗体显示正中间
        jFrame.setLocationRelativeTo(null);
        // 设置窗体标题
        jFrame.setTitle("窗体标题");
        // 设置窗体不可全屏显示
        //jFrame.setResizable(false);
        // 设置窗体关闭后退出程序
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置此窗口是否应该始终位于其他窗口上方
        jFrame.setAlwaysOnTop(true);
        // 设置窗体图标
        jFrame.setIconImage(new ImageIcon(HelloWorld.class.getResource("/images/book.png")).getImage());
        

        
        JPanel panel =new JPanel(null);
        
        
        JButton btn01 =new JButton("打开");
        btn01.setLocation(10,10);
        btn01.setSize(100, 40);
        
        JTextField text =new JTextField();
        text.setLocation(120,10);
        text.setSize(300, 40);
        
        
        btn01.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // 创建文件选择器
                JFileChooser fileChooser =new JFileChooser();
                
                // 创建文件选择器 (指定直接进入到磁盘目录)
                JFileChooser fileChooser1 =new JFileChooser("D:/");
                
                
                // 设置选择模式 FILES_ONLY 只选择文件(默认)
                // 设置选择模式 DIRECTORIES_ONLY 只选择文件夹
                // 设置选择模式 FILES_AND_DIRECTORIES 文件夹和文件均可选择
                fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                // 设置一次可选择多个文件
                fileChooser.setMultiSelectionEnabled(true);
                
                // 弹出文件选择器,返回值是用户点击的确定,还是取消按钮
                // APPROVE_OPTION表示点击的是确定按钮
                int option=fileChooser.showOpenDialog(jFrame);
                
                
                // 选择单个文件
                if(option ==JFileChooser.APPROVE_OPTION) {
                    File file =fileChooser.getSelectedFile();
                    text.setText(file.getAbsolutePath());
                }else {
                    text.setText("");
                }
                
                // 选择多个文件
                if(option ==JFileChooser.APPROVE_OPTION) {
                    File[] files =fileChooser.getSelectedFiles();
                    String filename="";
                    for(int i=0;i<files.length;i++) {
                        filename=files[i].getAbsolutePath()+";";
                    }
                    text.setText(filename);
                }else {
                    text.setText("");
                }
                
            }
        });
        
        
        panel.add(btn01);
        panel.add(text);
        
        jFrame.add(panel);
        // 设置窗体可见
        jFrame.setVisible(true);
            
        

 

标签:jFrame,fileChooser,窗体,设置,FileInput,new,JavaSwing,JFileChooser
From: https://www.cnblogs.com/liangqingyun/p/18621067

相关文章

  • java中FileInputStream和FileOutputStream类
    一、介绍FileInputStream类与FileOutputStream类都用来操作磁盘文件,提供了基本的文件写入能力。二、FileInputStream类常用的构造方法1、FileInputStream(Stringname)2、FileInputStream(Filefile)第一个构造方法使用给定的文件名name创建一个FileInputStream对象,第二个......
  • JavaSwing Dialog
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JTree
    packagecom.java.view;importjava.awt.Color;importjava.awt.Cursor;importjava.awt.Dimension;importjava.awt.FlowLayout;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.ItemEvent;......
  • JavaSwing JCombox
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JList
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JSlider
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JProcessBar
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JRadioButton
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JCheckBox
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......
  • JavaSwing JTextArea
    try{BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;//UIManager.put("RootPane.setupButtonVisible",false);org.jb2011.lnf.beautyeye.BeautyEyeLNFHelpe......