java - JFrame 标题到文本文件标题

标签 java swing

我想知道如何将框架标题添加为用户选择的任何文本文件的名称。

例如,如果我选择文本文件名 lab3LargeData.txt,则框架标题将为 lab3LargeData.txt。

这是迄今为止我的全部代码。我添加了注释以使其可读。

package GUIdesign;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TextAnalyzer extends Frame implements ActionListener {

    // ------------------------------------------
    // Declare class variables that are accessible from all methods in the class
    // ------------------------------------------
    JFrame frame;
    JTextField textField;
    JButton button, button2;
    JLabel label;
    JButton fileButton;
    JFileChooser fc = new JFileChooser();
    JTextArea textArea = new JTextArea();

    public TextAnalyzer() {
        JFrame frame = new JFrame(); // constructor

        // ------------------------------------------
        // Create a panel (container) to hold our components
        // ------------------------------------------
        JPanel panel = new JPanel();
        panel.setBackground(Color.black);
        panel.setLayout(new BorderLayout());
        frame.getContentPane().add(panel);

        // ------------------------------------------
        // adding buttons
        // ------------------------------------------
        button = new JButton("Pick a file");
        button.setPreferredSize(new Dimension(100, 30));
        button.addActionListener(this);
        panel.add(button, BorderLayout.PAGE_START);

        // ------------------------------------------
        // creating a text area
        // ------------------------------------------
        textArea = new JTextArea();
        textArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(false);
        panel.add(textArea);

        // ------------------------------------------
        // creating scroll
        // ------------------------------------------
        JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        panel.add(scroll, BorderLayout.CENTER);
        JPanel statusPanel = new JPanel();

        // ------------------------------------------
        // creating status bar (fix later with file title and file size)
        // ------------------------------------------
        statusPanel.setBorder((Border) new BevelBorder(BevelBorder.LOWERED));
        frame.add(statusPanel, BorderLayout.SOUTH);
        statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16));
        statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
        JLabel statusLabel = new JLabel("status");
        statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
        statusPanel.add(statusLabel);

        // ------------------------------------------
        // Setting up the frame
        // ------------------------------------------

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ability to
                                                                // close frame
        frame.getContentPane().add(panel);
        frame.setSize(400, 300); // size of the frame
        frame.setVisible(true); // make the frame visible // everything must be
                                // done before this statement
        // frame.setTitle(); // title of the frame
        frame.setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        TextAnalyzer demo = new TextAnalyzer();
        System.out.println(demo);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        // ------------------------------------------
        // getting the text from text field
        // ------------------------------------------

        int returnVal = fc.showOpenDialog(frame);

        // ------------------------------------------
        // If file was really selected, do something with it
        // ------------------------------------------
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            // ------------------------------------------
            // Get the file the user selected
            // ------------------------------------------
            File file = fc.getSelectedFile();
            System.out.println("name of file" + file.getName());
            String name = file.getName();

            // ------------------------------------------
            // String builder
            // ------------------------------------------

            StringBuilder sb = new StringBuilder();
            // ------------------------------------------
            // Create a scanner to the file
            // ------------------------------------------

            Scanner input;
            try {
                input = new Scanner(file);

                // ------------------------------------------
                // read text from file
                // ------------------------------------------
                while (input.hasNext()) {
                    sb.append(input.nextLine());
                    sb.append("\n");
                }
                System.out.println(input);
                input.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            textArea.setText(sb.toString());
            // ------------------------------------------
            // Do something with the file path (display it, use it to open,
            // etc.)
            // ------------------------------------------
            System.out.println(file.getPath());

        } else {
            System.out.println("No file was selected");
        }

    }

}

我尝试过这样做,但我知道这是不正确的。只是为了玩弄代码。

if (returnVal == JFileChooser.APPROVE_OPTION) {
        // ------------------------------------------
        // Get the file the user selected
        // ------------------------------------------
        File file = fc.getSelectedFile();
        System.out.println("name of file" + file.getName());
        String name = file.getName();
        frame.setTitle(name);// error right here

最佳答案

您的变量框架未初始化:您正在构造函数中初始化局部变量。

关于java - JFrame 标题到文本文件标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43912745/

相关文章:

java - 如何删除 JPanel 中由 ArrayList 指定的项目

java - 使用 JLabel 设置框架背景图像的正确方法是什么?

java - Netbeans 中的 JTable : how do display table of many columns

java - 调用方法中的数组越界异常,如何从方法返回整个数组

java - Spring 与 Callable 异步如何工作以及我不明白的地方?

java - Android 中的选项卡布局和按钮

java - android中如何动态添加数据