首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ScrollBar in JTextArea

ScrollBar in JTextArea
EN

Stack Overflow用户
提问于 2013-08-17 19:42:04
回答 4查看 13K关注 0票数 3

我想在文本区域中创建一个滚动条,但是如果我将JPanel布局设置为null,滚动条将不会显示!

我试过

代码语言:javascript
复制
JScrollPane scrollbar1 = 
  new JScrollPane(
    ta1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

但是没有工作,因为空布局.

以下是我的当前代码:

代码语言:javascript
复制
import javax.swing.*;

import java.awt.*;
public class app extends JFrame {

    public static void main(String[] args)
    {
        new app();
    }

    public app()
    {
        this.setSize(400,400);
        this.setLocation(0,0);
        this.setResizable(false);
        this.setTitle("Application");           
        JPanel painel = new JPanel(null);           
        // Creating the Input
        JTextField tf1 = new JTextField("Some random text", 15);            
        tf1.setBounds(5,5,this.getWidth()-120,20);
        tf1.setColumns(10);
        tf1.setText("Omg");         
        painel.add(tf1);            
        // Creating the button          
        JButton button1 = new JButton("Send");          
        button1.setBounds(290, 5, 100, 19);         
        painel.add(button1);            
        // Creating the TextArea            
        JTextArea ta1 = new JTextArea(15, 20);
        JScrollPane scr = new JScrollPane();
        ta1.setBounds(5, 35, 385, 330);
        ta1.setLineWrap(true);
        ta1.setWrapStyleWord(true);         
        painel.add(ta1);
        this.add(painel);
        this.setVisible(true);
    }
}

我想让它正常工作。如果有人能帮我,请在下面留言!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-08-17 20:15:23

我已经纠正了所有的问题,以下是工作守则。请阅读有关更改的评论。

代码语言:javascript
复制
import javax.swing.*;

import java.awt.*;

public class app extends JFrame {

    public static void main(String[] args) {
        new app();
    }

    public app() {
        this.setSize(400, 400);
        this.setLocation(0, 0);
        this.setResizable(false);
        this.setTitle("Application");
        JPanel painel = new JPanel(null);
        // Creating the Input
        JTextField tf1 = new JTextField("Some random text", 15);
        tf1.setBounds(5, 5, this.getWidth() - 120, 20);
        tf1.setColumns(10);
        tf1.setText("Omg");

        // resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        painel.add(tf1);
        // Creating the button
        JButton button1 = new JButton("Send");
        button1.setBounds(290, 5, 100, 19);
        painel.add(button1);
        // Creating the TextArea
        JTextArea ta1 = new JTextArea(15, 20);
        JScrollPane scr = new JScrollPane(ta1,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane 
        ta1.setBounds(5, 35, 385, 330);
        ta1.setLineWrap(true);
        ta1.setWrapStyleWord(true);
        scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout
        painel.add(scr);// Add you scroll pane to container
        this.add(painel);
        this.setVisible(true);
    }
}

编辑。请阅读oracle关于Java的教程。开始使用适当的布局管理器..。希望它能帮上忙

票数 3
EN

Stack Overflow用户

发布于 2013-08-17 19:46:34

您必须将您的JTextArea传递给您的JScrollPane构造函数,然后将JScrollPane对象添加到您的Container中,而不是只添加JTextArea。所以看起来是这样的:

代码语言:javascript
复制
JScrollPane scr = new JScrollPane(ta1);
panel.add(scr);
票数 2
EN

Stack Overflow用户

发布于 2013-08-17 20:11:17

If someone can help me, leave a comment below please!

  • 为什么你要用你的头墙砸碎,JScrollPane被指定为动态的,可调整大小的LayoutManagerAbsoluteLayout可以打破它的基本特性
  • 从顶上开始

代码语言:javascript
复制
1. `public class app extends JFrame {`
代码语言:javascript
复制
    - `public class App {` ---> Java Naming Conventions
    - and not extends anything, create `JFrame` as local variable

代码语言:javascript
复制
1. `new app();` ---> se Oracle tutorial Initial Thread
2. create another `JPanel`, put there `JTextField` and `JButton` 
3. did you overlay something `tf1.setBounds(5,5,this.getWidth()-120,20);`
4. `NullLayout` doesn't works correctly without using `Insets`
5. change built\_in `FlowLayout` for `JPanel painel = new JPanel(null);` to `BorderLayout`, there put `JScrollPane` with `JTextArea` to `CENTER area`
6. you can to put `JScrollPane` with `JTextArea` to `JFrames CENTER area` directly and another `JPanel` with `JTextField` and `JButton` to `SOUTH` or `NORTH`, `JFrame` has `BorderLayout` implemented in API
7. `JScrollPane` showing `JScrollbars` only in the case that its `Dimension` is smaller than `JComponent` placed there
8. use `JFrame.pack()` instead of `setSize`, this line should be before `setVisible`

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18292659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档