Java 拆分窗格中的JTextArea在加载文件时不显示文本

Java 拆分窗格中的JTextArea在加载文件时不显示文本,java,swing,Java,Swing,我写的作业代码有问题 当我在GUI中加载txt文档时,我需要它执行的功能很简单,我希望GUI中的左面板显示文档中的文本 我尝试了一些调整JTextField和使用JTextArea的方法,但是每当我加载示例文件时,我总是会得到一个小的空框,在那里我可以输入东西 如果有人对此有解决方案,并能指出我做错了什么,我们将不胜感激 这是我的密码 import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.ev

我写的作业代码有问题

当我在GUI中加载txt文档时,我需要它执行的功能很简单,我希望GUI中的左面板显示文档中的文本

我尝试了一些调整JTextField和使用JTextArea的方法,但是每当我加载示例文件时,我总是会得到一个小的空框,在那里我可以输入东西

如果有人对此有解决方案,并能指出我做错了什么,我们将不胜感激

这是我的密码

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.*;

public class JavaAssingnment {


public static void main(String[] args) throws FileNotFoundException {

    window window = new window();
    String reader;



}

public static class window extends JFrame {



    public window() throws FileNotFoundException {


            JMenuBar menuBar = new JMenuBar(); // menubar
            JMenu menu1 = new JMenu("File"); //menu 
            menuBar.add(menu1); // add menu to gui
            JMenu menu2 = new JMenu("Help");
            menuBar.add(menu2);
            JMenuItem menuItem1 = new JMenuItem("Load File", KeyEvent.VK_1); // create drop down menu
            JMenuItem menuItem2 = new JMenuItem("Save File", KeyEvent.VK_1);
            JMenuItem menuItem3 = new JMenuItem("Exit", KeyEvent.VK_1);     
            JMenuItem menuItem4 = new JMenuItem("About", KeyEvent.VK_1);

            menu1.add(menuItem1); // adds drop down menus to gui
            menu1.add(menuItem2);
            menu1.add(menuItem3);
            menu2.add(menuItem4);

            JPanel leftScrollPane = new JPanel();
            JPanel rightPane = new JPanel();
            JSplitPane splitPane;
            JTextArea input = new JTextArea();
            input.setVisible(true);
            input.setSize(300,300);
            leftScrollPane.add(input);


            this.setVisible(true);
            this.setSize(400, 400);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            splitPane = new JSplitPane();
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setDividerSize(10);
            splitPane.setDividerLocation(100);
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            splitPane.setOneTouchExpandable(true);
            splitPane.setDividerLocation(600);

            Dimension minimumSize = new Dimension(100, 50);

            leftScrollPane.setSize(400, 400);

            this.setJMenuBar(menuBar);
            splitPane.setPreferredSize(new Dimension(400, 200));
            splitPane.setLeftComponent(leftScrollPane);
            splitPane.setRightComponent(rightPane);
            this.add(splitPane);
            this.setSize(1280, 720);

            // execute code when selected
            menuItem1.addActionListener(new ActionListener() {


                @Override
                public void actionPerformed(ActionEvent e) {

                    final JFileChooser fc = new JFileChooser();

                    // you can set the directory with the setCurrentDirectory method.
                    int returnVal = fc.showOpenDialog(null);

                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        // User has selected to open the file.

                        File file = fc.getSelectedFile();

                        try {
                            // Open the selected file
                            BufferedReader reader = new BufferedReader(new FileReader(file));

                            // Output the contents to the console.
                            String nextLine = reader.readLine();

                            while ( nextLine != null ) {

                                nextLine = reader.readLine();
                                input.setText(nextLine);

                            }

                            reader.close();

                        } catch (IOException e1) {

                            System.err.println("Error while reading the file");
                        } 


                    }
                }});

    }
}
}
这不表示尺寸,而

        JTextArea input = new JTextArea(20,40);
建议以列和行为单位的大小

input.setSize(300,300);
这一点都没有帮助,因为布局会忽略大小而选择首选大小。但是也不要弄乱它,因为数字只是一个猜测,而设置行和列可以让运行时确定它应该有多大

进一步:

JPanel leftScrollPane = new JPanel();
将以默认的
FlowLayout
结束,这意味着组件将以其首选大小显示,并且不会拉伸到面板的大小

这将“修复”以下问题:

JPanel leftScrollPane = new JPanel(new GridLayout()); // will stretch content to fill
这不表示尺寸,而

        JTextArea input = new JTextArea(20,40);
建议以列和行为单位的大小

input.setSize(300,300);
这一点都没有帮助,因为布局会忽略大小而选择首选大小。但是也不要弄乱它,因为数字只是一个猜测,而设置行和列可以让运行时确定它应该有多大

进一步:

JPanel leftScrollPane = new JPanel();
将以默认的
FlowLayout
结束,这意味着组件将以其首选大小显示,并且不会拉伸到面板的大小

这将“修复”以下问题:

JPanel leftScrollPane = new JPanel(new GridLayout()); // will stretch content to fill
除了上面所说的,while循环中还有一个错误

String nextLine = reader.readLine();
你看这里的第一行

                        while ( nextLine != null ) {
如果第一行不是空的

                            nextLine = reader.readLine();
你读第二行并把它放到屏幕上

                            input.setText(nextLine);
                        }
在while循环的控制语句中不检查read行

String nextLine = reader.readLine();
而是像这样尝试:

                        String nextLine;
                        String temp ="";

                        while ( ( nextLine = reader.readLine()) != null ) {
                            temp += nextLine;
                            input.setText(temp);
如果不将之前读取的每一行添加到新读取的行中,则只会以文本文件的最后一行结束。

除上述内容外,while循环中还有一个错误

String nextLine = reader.readLine();
splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(600);

Dimension minimumSize = new Dimension(100, 50);

leftScrollPane.setSize(400, 400);

this.setJMenuBar(menuBar);
splitPane.setPreferredSize(new Dimension(400, 200));
splitPane.setLeftComponent(leftScrollPane); // ???
splitPane.setRightComponent(rightPane); // ??
你看这里的第一行

                        while ( nextLine != null ) {
如果第一行不是空的

                            nextLine = reader.readLine();
你读第二行并把它放到屏幕上

                            input.setText(nextLine);
                        }
在while循环的控制语句中不检查read行

String nextLine = reader.readLine();
而是像这样尝试:

                        String nextLine;
                        String temp ="";

                        while ( ( nextLine = reader.readLine()) != null ) {
                            temp += nextLine;
                            input.setText(temp);
如果不将之前读取的每一行添加到新读取的行中,则只会以文本文件的最后一行结束

splitPane.setLeftComponent(leftScrollPane);
splitPane.setRightComponent(rightPane);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(600);

Dimension minimumSize = new Dimension(100, 50);

leftScrollPane.setSize(400, 400);

this.setJMenuBar(menuBar);
splitPane.setPreferredSize(new Dimension(400, 200));
splitPane.setLeftComponent(leftScrollPane); // ???
splitPane.setRightComponent(rightPane); // ??
为什么要将组件添加到拆分窗格两次

JPanel leftScrollPane = new JPanel();
JPanel rightPane = new JPanel();
JSplitPane splitPane;
JTextArea input = new JTextArea();
input.setVisible(true);
input.setSize(300,300);
leftScrollPane.add(input);
为什么要将文本区域添加到称为“滚动窗格”的JPanel中。通常,您会将文本区域添加到实际的JScrollPane中,以便在必要时使用滚动条。没有必要安装面板。此外:

  • Swing组件在默认情况下是可见的,因此不需要setVisible(true)
  • 布局管理器(或者在本例中是JSplitPane)将确定每个组件的大小,因此setSize(…)不起任何作用。您不应该尝试设置组件的大小
  • 因此,更好的代码应该是:

    JTextArea textArea = new JTextArea(5,20);
    JScrollPane scrollPane = new JScrollPane( textArea );
    splitPane.setLeftComponent( scrollPane );
    
    每当我加载示例文件时

    不要在循环中读取文件。使用JTextArea的read(…)方法:

    textArea.read(reader, null);
    
    为什么要将组件添加到拆分窗格两次

    JPanel leftScrollPane = new JPanel();
    JPanel rightPane = new JPanel();
    JSplitPane splitPane;
    JTextArea input = new JTextArea();
    input.setVisible(true);
    input.setSize(300,300);
    leftScrollPane.add(input);
    
    为什么要将文本区域添加到称为“滚动窗格”的JPanel中。通常,您会将文本区域添加到实际的JScrollPane中,以便在必要时使用滚动条。没有必要安装面板。此外:

  • Swing组件在默认情况下是可见的,因此不需要setVisible(true)
  • 布局管理器(或者在本例中是JSplitPane)将确定每个组件的大小,因此setSize(…)不起任何作用。您不应该尝试设置组件的大小
  • 因此,更好的代码应该是:

    JTextArea textArea = new JTextArea(5,20);
    JScrollPane scrollPane = new JScrollPane( textArea );
    splitPane.setLeftComponent( scrollPane );
    
    每当我加载示例文件时

    不要在循环中读取文件。使用JTextArea的read(…)方法:

    textArea.read(reader, null);
    

    那么,我应该对代码的前两个突出显示的部分进行任何更改吗?我不明白您在编写“代码的前两个突出显示部分”时所指的是什么。但是,这段代码太糟糕了,我会把它扔掉,在读完Swing/layout教程后重新开始。那么,我应该对前两段突出显示的代码进行任何更改吗?我不明白您在编写“前两段突出显示的代码”时指的是什么。但是这段代码太糟糕了,我会把它扔掉,在读完Swing/layout教程后重新开始。