Java JDialog上带有滚动条的Swing文本区

Java JDialog上带有滚动条的Swing文本区,java,swing,jscrollpane,jtextarea,jdialog,Java,Swing,Jscrollpane,Jtextarea,Jdialog,我有这样一个场景,当用户单击按钮时,弹出窗口将打开一个文本区域。当需要时,文本区域将有一些带有滚动条的内容。为了实现这一点,我使用了JDialog并在JDialog中添加了一个文本区域。在我的例子中,我能够在单击按钮时显示对话框,并在带有内容的对话框上显示文本区域。但我无法获取文本区域的滚动条。我也在文本区使用了JScrollPane public class DialogPanel { public void createDialog() { final JFrame

我有这样一个场景,当用户单击按钮时,弹出窗口将打开一个文本区域。当需要时,文本区域将有一些带有滚动条的内容。为了实现这一点,我使用了JDialog并在JDialog中添加了一个文本区域。在我的例子中,我能够在单击按钮时显示对话框,并在带有内容的对话框上显示文本区域。但我无法获取文本区域的滚动条。我也在文本区使用了JScrollPane

public class DialogPanel {

    public void createDialog() {
        final JFrame mainFrame = new JFrame();
        mainFrame.setVisible(true);
        mainFrame.setSize(500, 600);
        mainFrame.setLayout(new BorderLayout());
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton btn = new JButton("Open Dialog");
        mainFrame.add(btn, BorderLayout.SOUTH);
        JTextField txtField = new JTextField();
        mainFrame.add(txtField, BorderLayout.NORTH);
        btn.setPreferredSize(new Dimension(100, 100));
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                JDialog dialog = new JDialog(mainFrame);
                dialog.setLocationByPlatform(true);
                JTextArea txtArea = new JTextArea();
                txtArea.setAutoscrolls(true);
                txtArea.setPreferredSize(new Dimension(900, 500));
                txtArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                txtArea.setFont(new Font("courier new", Font.PLAIN, 12));
                txtArea.setLineWrap(true);
                JScrollPane txtAreaScroll = new JScrollPane();
                txtAreaScroll.setViewportView(txtArea);
                txtAreaScroll.setAutoscrolls(true);

                File file;
                String line = null;
                StringBuilder fileContents = new StringBuilder();
                try {
                    file = new File(
                            "D:\\Softwares\\Apache\\apache-tomcat-7.0.47\\RUNNING.txt");
                    BufferedReader reader = new BufferedReader(new FileReader(
                            file));
                    while ((line = reader.readLine()) != null) {
                        fileContents.append(line + "\n");
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                txtArea.setText(fileContents.toString());

                dialog.add(txtAreaScroll);
                dialog.pack();
                dialog.setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        DialogPanel dialogPanel = new DialogPanel();
        dialogPanel.createDialog();
    }
}

本质上,
txtArea.setPreferredSize(新维度(900500))
正在删除
JTextArea
用于确定显示所有文本所需的空间量的自动计算。你实际上是在说,只需要500像素的高度

您“可以”设置滚动窗格的首选大小,但实际上并不建议这样做。相反,您希望更改
JTextArea
中的
getPreferredScrollableViewportSize
返回的值

这将告诉滚动窗格使可视区域变大的大小。。。如果可以

JTextArea txtArea = new JTextArea() {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(900, 500);
    }

};
请查看以了解更多详细信息

已更新

正如AndrewThompson所指出的,更好的(也是更可取的)方法是简单地指定
JTextArea
的行和列,并让它根据平台渲染功能来确定这意味着什么

JTextArea txtArea = new JTextArea(40, 100);

是的,为了简单…

本质上是,
txtArea.setPreferredSize(新维度(900500))
正在删除
JTextArea
用于确定显示所有文本所需的空间量的自动计算。你实际上是在说,只需要500像素的高度

您“可以”设置滚动窗格的首选大小,但实际上并不建议这样做。相反,您希望更改
JTextArea
中的
getPreferredScrollableViewportSize
返回的值

这将告诉滚动窗格使可视区域变大的大小。。。如果可以

JTextArea txtArea = new JTextArea() {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(900, 500);
    }

};
请查看以了解更多详细信息

已更新

正如AndrewThompson所指出的,更好的(也是更可取的)方法是简单地指定
JTextArea
的行和列,并让它根据平台渲染功能来确定这意味着什么

JTextArea txtArea = new JTextArea(40, 100);

是的,为了简单起见…

您使用的是
dialog.pack()
查看并定义对话框的自身大小

您使用的是
dialog.pack()
查看并定义对话框的自身大小

,这样您就可以使用带有滚动条的文本区域:

                JTextArea txtArea = new JTextArea(40,100);
                txtArea.setAutoscrolls(true);
                txtArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                txtArea.setFont(new Font("courier new", Font.PLAIN, 12));
                txtArea.setLineWrap(true);
                txtArea.setText(j);
                JScrollPane txtAreaScroll = new JScrollPane (txtArea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                txtAreaScroll.setViewportView(txtArea);
                txtAreaScroll.setAutoscrolls(true);
                // now add scroll pane in frame

这样您就可以使用带有滚动条的文本区域:

                JTextArea txtArea = new JTextArea(40,100);
                txtArea.setAutoscrolls(true);
                txtArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                txtArea.setFont(new Font("courier new", Font.PLAIN, 12));
                txtArea.setLineWrap(true);
                txtArea.setText(j);
                JScrollPane txtAreaScroll = new JScrollPane (txtArea, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
                txtAreaScroll.setViewportView(txtArea);
                txtAreaScroll.setAutoscrolls(true);
                // now add scroll pane in frame

这很有效。感谢您提供的解决方案和解释。:)记住,荡秋千很有趣;)这种方法是否比设置文本区域的列数和行数更合适?@AndrewThompson有效。感谢您提供的解决方案和解释。:)记住,荡秋千很有趣;)这种方法是否有过多设置文本区域的列数和行数的情况?@AndrewThompson,OP在
dialog.add(txtAreaScroll)之后使用它,并且在
对话框之前。setVisible(true)
对话框之后OP正在使用的。添加(txtAreaScroll),并且在
对话框之前。setVisible(true)