Java标签的内容不断变化

时间:2017-12-16 06:23:52

标签: java swing jtextarea jtabbedpane

此代码的目的[打开Java文件,比该Java文件的内容将显示在新标签]我也尝试编译func但我需要先在这里解决这个问题。

我的问题是当我尝试打开新的Java文件时,我打开的Java文件的最后内容会累积到Java文件的新内容。我不知道为什么,有没有办法解决这个问题?

package term_project_GUI;

import javax.swing.*;
import javax.swing.filechooser.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class JavaIDE_UI extends JFrame {
    Container contentPane;

    JTabbedPane tp;
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JTextArea ta = new JTextArea(15, 30);
    JScrollPane sp = new JScrollPane(ta);

    JavaIDE_UI() {
        setTitle("JavaIDE");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createMenu();

        contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        tp = new JTabbedPane();
        contentPane.add(tp, BorderLayout.NORTH);
        contentPane.add(p2, BorderLayout.SOUTH);

        setSize(400, 500);
        setVisible(true);
    }

    void createMenu() {
        JMenuBar mb = new JMenuBar();
        JMenu File = new JMenu("File");
        JMenu Run = new JMenu("Run");
        JMenuItem Open = new JMenuItem("Open");
        JMenuItem Close = new JMenuItem("Close");
        JMenuItem Save = new JMenuItem("Save");
        JMenuItem SaveAs = new JMenuItem("Save As");
        JMenuItem Quit = new JMenuItem("Quit");
        JMenuItem Compile = new JMenuItem("Compile");

    Open.addActionListener(new OpenActionListener_open());

    File.add(Open);
    File.add(Close);
    File.add(Save);
    File.add(SaveAs);
    File.add(Quit);

    Run.add(Compile);

    mb.add(File);
    mb.add(Run);

    setJMenuBar(mb);
}

class OpenActionListener_open implements ActionListener {
    JFileChooser chooser;
    JTextArea ta = new JTextArea(15, 30);

    OpenActionListener_open() {
        chooser = new JFileChooser();
    }

    public void actionPerformed(ActionEvent e) {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(".java files", "java");
        chooser.setFileFilter(filter);

        int ret = chooser.showOpenDialog(null);
        if (ret != JFileChooser.APPROVE_OPTION) {
            JOptionPane.showMessageDialog(null, "File Unchoose.", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }


        String fileName = chooser.getSelectedFile().getName();
        tp.addTab(fileName, new JScrollPane(ta));

        String filePath = chooser.getSelectedFile().getPath();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String data = "";
            while ((data = reader.readLine()) != null) {
                ta.append(data+"\n");
            }
            reader.close();
        } catch (IOException e1) {
        }


    }
}

public static void main(String[] args) {
    new JavaIDE_UI();
}
}

1 个答案:

答案 0 :(得分:1)

您正在创建两个JTextArea实例,一个是JavaIDE_UI的实例字段,另一个是OpenActionListener_open的实例字段。

调用actionPerformed方法后,它会将文本附加到现有JTextAreaOpenActionListener_open),这个过程也会将其从中删除&# 39;当前标签/滚动窗格并将其移至您正在创建的新标签

相反,请为每个标签创建一个新的JTextArea ...

class OpenActionListener_open implements ActionListener {

    JFileChooser chooser;

    OpenActionListener_open() {
        chooser = new JFileChooser();
    }

    public void actionPerformed(ActionEvent e) {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(".java files", "java");
        chooser.setFileFilter(filter);

        int ret = chooser.showOpenDialog(null);
        if (ret != JFileChooser.APPROVE_OPTION) {
            JOptionPane.showMessageDialog(null, "File Unchoose.", "Warning", JOptionPane.WARNING_MESSAGE);
            return;
        }

        String fileName = chooser.getSelectedFile().getName();
        JTextArea ta = new JTextArea();
        JScrollPane sp = new JScrollPane(ta);
        tp.addTab(fileName, sp);

        String filePath = chooser.getSelectedFile().getPath();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String data = "";
            while ((data = reader.readLine()) != null) {
                ta.append(data + "\n");
            }
            reader.close();
        } catch (IOException e1) {
        }
        tp.setSelectedComponent(sp);

    }
}

此示例还将选择选项卡,以便您可以看到新打开的文件。

此外,我会更改为contentPane.add(tp, BorderLayout.NORTH); contentPane.add(tp);,否则您将会遇到一些奇怪的布局问题