1

我想知道为什么 JTabbedPane 只添加 1 个选项卡。当我使用方法 addTab 然后重用它来创建一个新选项卡时,它会覆盖创建的第一个选项卡。这是代码:顺便说一句,可能与问题相关的大部分代码都在 actionlistener 中。

package com.james.client;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.Element;

public class Main extends JFrame {

private static final long serialVersionUID = 1L;

public static void main(String [] args)
{
    //JTextArea
    final JTextArea code = new JTextArea();
    final JTextArea lines = new JTextArea("1");
    final JScrollPane scroll = new JScrollPane(code);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    lines.setBackground(Color.LIGHT_GRAY);
    lines.setEditable(false);

    code.getDocument().addDocumentListener(new DocumentListener(){
        public String getText(){
            int caretPosition = code.getDocument().getLength();
            Element root = code.getDocument().getDefaultRootElement();
            String text = "1" + System.getProperty("line.separator");
            for(int i = 2; i < root.getElementIndex( caretPosition ) + 2; i++){
                text += i + System.getProperty("line.separator");
            }
            return text;
        }
        @Override
        public void changedUpdate(DocumentEvent de) {
            lines.setText(getText());
        }

        @Override
        public void insertUpdate(DocumentEvent de) {
            lines.setText(getText());
        }

        @Override
        public void removeUpdate(DocumentEvent de) {
            lines.setText(getText());
        }

    });

    scroll.getViewport().add(code);
    scroll.setRowHeaderView(lines);

    //JFrame
    JFrame window = new JFrame("MinecraftProgrammer++");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(1000, 700);
    window.setResizable(false);
    window.setLocationRelativeTo(null);

    //JMenuBar
    JMenuBar menu = new JMenuBar();
    JMenu file = new JMenu("File");
    JMenu newfile = new JMenu("New");

    //JTabbedPane
    final JTabbedPane tabs = new JTabbedPane();
    tabs.setBackground(Color.gray);

    //JMenu items
    JMenuItem classfile = new JMenuItem("Class");
    JMenuItem packagefolder = new JMenuItem("Package");
    JMenuItem other = new JMenuItem("Other");
    JMenuItem open = new JMenuItem("Open");
    open.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent arg0) {
          try {
            JFileChooser chooseFile = new JFileChooser();
            chooseFile.setAcceptAllFileFilterUsed(false);
            FileFilter filter1 = new ExtensionFileFilter("Java Class", new String[] {"JAVA"});
            FileFilter filter2 = new ExtensionFileFilter("Text File", new String[] {"TXT"});
            chooseFile.setFileFilter(filter2);
            chooseFile.setFileFilter(filter1);
            chooseFile.showOpenDialog(chooseFile);
            String filePath = chooseFile.getSelectedFile().getAbsolutePath();
            FileReader readFile = new FileReader(filePath);
            String fileName = chooseFile.getSelectedFile().getName();
            tabs.addTab(fileName, scroll);
            @SuppressWarnings("resource")
            Scanner fileReaderScan = new Scanner(readFile);
            String storeAllString = "";
             while(fileReaderScan.hasNextLine())
             {
                 String temp = fileReaderScan.nextLine() + "\n";
                 storeAllString = storeAllString + temp;
             }

             code.setText(storeAllString);
             code.setLineWrap(true);
             code.setWrapStyleWord(true);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println(e);
        }

        }   
    });
    JMenuItem save = new JMenuItem("Save");
    JMenuItem saveas = new JMenuItem("Save As...");

    //Compile menu bar
    file.add(newfile);
    file.add(open);
    file.add(save);
    file.add(saveas);
    newfile.add(classfile);
    newfile.add(packagefolder);
    newfile.add(other);
    menu.add(file);


    window.add(tabs);
    window.setJMenuBar(menu);
    window.setVisible(true);
}

}
4

1 回答 1

3

您正在将 JScrollPane“滚动”添加到 JTabbedPane。你构建这个组件多少次?回答——只有一次。因此,您每次只需添加和重新添加相同的组件。

final JScrollPane scroll = new JScrollPane(code); // here you create scroll

// .....

open.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
      try {
        JFileChooser chooseFile = new JFileChooser();

        // ....

        // you add the same component here
        tabs.addTab(fileName, scroll);

        // ....

    }

由于您不能多次将组件添加到容器并在两个容器中都看到它,因此解决方案是每次需要向 JTabbedPane 添加内容时,在 ActionListener 的方法内创建新选项卡所需的任何组件。actionPerformed(...)

于 2013-05-07T21:07:04.220 回答