【问题标题】:JScrollPane not working on my JTextArea?JScrollPane 不能在我的 JTextArea 上工作?
【发布时间】:2013-11-15 13:10:16
【问题描述】:

我对 java GUI 和布局不是很熟悉,但我得承认,我没想到会这么棘手!我不太确定我在这里做什么。

我只想在彼此之上添加 2 个文本区域,并为每个区域添加一个 jscrollpane。但我无法让 JScrollPanes 工作。这是我目前得到它的方式。

public class Class extends JFrame {

     public Class() {
    super("Title");

    getContentPane().setLayout(
            new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setResizable(false);

    JTextArea window1 = new JTextArea("text");
    window1.setEditable(false);
    window1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    window1.setPreferredSize(new Dimension(200, 250));
    window1.setLineWrap(true);

    add(window1);

    JScrollPane scroll = new JScrollPane(window1);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    add(scroll);

    JTextArea window2 = new JTextArea();
    window2.setEditable(true);
    window2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    window2.setPreferredSize(new Dimension(100, 50));
    window2.setLineWrap(true);
    add(window2);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}
}

看起来不错,有一个带有“文本”的框,右边是滚动窗格。在它们下方是第二个 JTextArea(还没有滚动窗格)。问题是,当我在窗口 1 中写入文本并且文本位于 jtextarea 之外的下方时,我希望 JScrollPane 实际上是可滚动的,因此我可以向下滚动以查看底部的文本,但是当我按下它时没有任何反应(并且它的大小或任何东西都不会改变)。我错过了什么值得注意的事情吗?

【问题讨论】:

    标签: java swing jscrollpane jtextarea


    【解决方案1】:

    您将JTextArea 添加到JScrollPane,您无需将其添加到您的JFrame。还有setPreferedSize() 用于JScrollPane,而不是直接用于JTextArea,因为您的JTextArea 将不可滚动。我已经修改了你的代码,试试看:

    public class Class  extends JFrame {
        public Class () {
            super("Title");
    
            getContentPane().setLayout(
                    new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
            setResizable(false);
    
            JTextArea window1 = new JTextArea("text");
            window1.setEditable(false);
            window1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            window1.setLineWrap(true);
    
    
            JScrollPane scroll1 = new JScrollPane(window1);
            scroll1.setPreferredSize(new Dimension(200, 250));
            scroll1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            add(scroll1);
    
            JTextArea window2 = new JTextArea();
            window2.setEditable(true);
            window2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            window2.setLineWrap(true);
            add(window2);
    
            JScrollPane scroll2 = new JScrollPane(window2);
            scroll2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            scroll2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            scroll2.setPreferredSize(new Dimension(100, 50));
            add(scroll2);
    
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    
        public static void main(String... s){
            new Class();
        }
    }
    

    【讨论】:

    • 是的,谢谢,在 JScrollPane 上使用 setPreferredSize 而不是 JTextArea,现在一切顺利!
    【解决方案2】:

    问题是当我在窗口 1 中写入文本并且文本出现时 下面,在 jtextarea 之外,我希望 JScrollPane 实际上是可滚动的


    JTextArea 已包含在JScrollPane 中。你不应该添加window1

    add(window1);// Remove this.
    
    JScrollPane scroll = new JScrollPane(window1);//here you added window1
    add(scroll);// This statement also added the JTextArea 
                //and do the operaton of above add.
    

    【讨论】:

    • 但确实不知道这个......不幸的是它没有解决问题(这就是为什么其他答案被接受为正确答案的原因; setPreferredSize() 应该用于JScrollPane 而不是 JTextArea 来解决问题)!
    • @optional,为什么我会提到支持问题已经提到的东西?
    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多