有厉害解决嘛,解决一下

  • 4、编写一个程序获取一个已知文件的扩展名,并通过GUI输入文件名和输出得到的扩展名。

完整代码如下

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setSize(200, 200);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(new BorderLayout());

        JTextField jTextField = new JTextField();
        JButton jButton = new JButton("获取文件扩展名");
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String filename = jTextField.getText();
                int index = filename.lastIndexOf(".");
                JOptionPane.showMessageDialog(jFrame, "您输入的文件扩展名是:" + filename.substring(index+1));
            }
        });

        jFrame.add(jTextField, BorderLayout.CENTER);
        jFrame.add(jButton, BorderLayout.SOUTH);
        jFrame.setVisible(true);
    }

运行结果如下

img

img

如有帮助,请采纳,十分感谢!

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class Main {
    public static void main(String[] args) {
        App app = new App();
        app.setVisible(true);
    }
}

class App extends JFrame {
    public App() {
        setSize(500, 500);
        setTitle("文件管理器");
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initUI();
    }

    private void initUI() {
        JPanel panel = new JPanel(new GridLayout(3, 100));
        JTextArea textArea = new JTextArea(1, 0);
        JLabel label = new JLabel();
        JButton button = new JButton("获取文件类型");

        panel.add(textArea);
        panel.add(label);
        panel.add(button);

        textArea.setWrapStyleWord(true);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String file_path = textArea.getText();
                int index = file_path.lastIndexOf(".");
                label.setText(file_path.substring(index + 1));
            }
        });

        add(panel);

        JMenuBar menuBar = new JMenuBar();
        JMenu menuFile = new JMenu("文件");
        JMenuItem menuFileOpen = new JMenuItem("打开");
        menuBar.add(menuFile);

        menuFile.add(menuFileOpen);

        menuFileOpen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                if ((fileChooser.showDialog(null, "选择文件")) != JFileChooser.APPROVE_OPTION) {
                    return;
                }
                File file = fileChooser.getSelectedFile();
                String file_path = file.getAbsolutePath();
                int index = file_path.lastIndexOf(".");
                textArea.setText(file_path);
                label.setText(file_path.substring(index + 1));
            }
        });

        this.setJMenuBar(menuBar);
    }
}