简体   繁体   English

JScrollPane不在JTextArea中滚动

[英]JScrollPane Not Scrolling In JTextArea

There's something that I don't understand. 我有些不懂 My code does not like JScrollBar apparently. 我的代码显然不喜欢JScrollBar I add it and I cannot scroll horizontally nor vertically. 我添加了它,因此无法水平或垂直滚动​​。

Here's what it looks like: 看起来是这样的:

Keep in mind that I'm new and I'm still working on it, so I'm sorry if it was something really obvious and easily avoidable. 请记住,我是新手,我仍在努力之中,所以对于它确实很明显并且很容易避免的事情,我感到抱歉。

public ChangeLog() {

    //Init.
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JTextArea textarea = new JTextArea();
    JScrollPane scrollpane = new JScrollPane(textarea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    //Text Stuff
    textarea.setFont(textarea.getFont().deriveFont(16f));
    textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
            + "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
    textarea.setForeground(Color.BLACK);
    Dimension d = new Dimension(250, 275);
    textarea.setPreferredSize(d);

    //Other Stuff
    scrollpane.setViewportView(textarea);
    scrollpane.getPreferredSize();

    //Layout
    panel.setLayout(null);
    scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
    textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));

    //Frame Stuff
    frame.setAlwaysOnTop(true);
    frame.setSize(300, 350);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);

    //Panel Stuff
    frame.add(panel);
    panel.setSize(frame.getSize());
    panel.setBackground(Color.BLUE);
    panel.add(textarea);
    panel.add(scrollpane);
} }
Dimension d = new Dimension(250, 275);
textarea.setPreferredSize(d);

Don't hardcode a size for the text area. 不要硬编码文本区域的大小。 The size of the text area will change dynamically as text is added/removed and scrollbars will appear/disappear as required. 文本区域的大小将随着添加/删除文本而动态变化,并且滚动条将根据需要显示/消失。

JTextArea textarea = new JTextArea();

Don't create the text area with no parameters. 不要创建没有参数的文本区域。 Instead, when you create the text area use something like: 相反,当您创建文本区域时,请使用类似以下内容的内容:

JTextArea textarea = new JTextArea(5, 20);

to suggest a default size of the text area. 建议文本区域的默认大小。 Then when you have more than 5 lines of text the scrollbar will appear. 然后,当您有多于5行文本时,将显示滚动条。

So I'm a relatively new Java developer 所以我是一个相对较新的Java开发人员

Start by reading the Swing Tutorial for Swing basics. 首先阅读有关Swing基础知识的Swing教程 There is a section on How to Use Text Areas to get you started. 有一节介绍How to Use Text Areas来帮助您入门。

panel.setLayout(null);
scrollpane.setBounds(...)

Don't a null layout. 不要使用空布局。 Don't use setBounds(). 不要使用setBounds()。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。 See the above tutorial for working examples. 有关工作示例,请参见上面的教程。

I have created a working solution. 我创建了一个可行的解决方案。 Made some changes also. 也做了一些更改。

public TestClass() {

        //Init.
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new BorderLayout());
        JTextArea textarea = new JTextArea();
        JScrollPane scrollpane = new JScrollPane(textarea);
        panel.add(scrollpane, BorderLayout.CENTER);



        //Text Stuff
        textarea.setFont(textarea.getFont().deriveFont(16f));
        textarea.setText("Change Log: \n V1.0(A): Original encoder \n V1.0(B): Original decoder \n V1.1: Combination of both encoder and decoder \n V1.2: Added a heavier encoding & decoding system \n V1.3: Added an icon \n V1.4: Created an 'Info' page \n V1.5: Added a 'Change Log' page to the 'Info' page \n "
                + "V1.6: Removed the 'Change Log' \n V1.7: Added a 'Change Log' but was not implemented \n V1.8: Added a the 'Change Log' button \n V1.9: Added horizontal and vertical scroll bars to the 'Change Log'");
        textarea.setForeground(Color.BLACK);
        //Dimension d = new Dimension(250, 275);
        //textarea.setPreferredSize(d);


        //Other Stuff
        scrollpane.setViewportView(textarea);
        scrollpane.getPreferredSize();




        //Layout
        //scrollpane.setBounds(new Rectangle(new Point(20, 20), scrollpane.getPreferredSize()));
        //textarea.setBounds(new Rectangle(new Point(20, 23), textarea.getPreferredSize()));

        //Listeners



        //Frame Stuff
        frame.setAlwaysOnTop(true);
        frame.setSize(300, 350);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
        frame.setResizable(false);


        //Panel Stuff
        frame.add(panel);
        panel.setSize(frame.getSize());
        panel.setBackground(Color.BLUE);
        panel.add(scrollpane);
    }

Also when swing better works with the layout managers and null layout will leads to inconsistent look on different screen types. 同样,当摆动更好地与布局管理器配合使用时,空布局将导致不同屏幕类型上的外观不一致。

Let me know if anything more required. 让我知道是否还有其他要求。 And yes everybody starts from scratch. 是的,每个人都从头开始。 I am still learning. 我仍在学习。 You will too get many things. 您会得到很多东西。 Just keep the hunger of learning. 只是保持学习的渴望。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM