Java JTextArea 中的滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18292659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:09:15  来源:igfitidea点击:

ScrollBar in JTextArea

javaswingjpaneljscrollpanejtextarea

提问by John Black

I want to create a scroll bar in the textarea but If I set the JPanel Layout to null, the scrollbar won't show!

我想在 textarea 中创建一个滚动条,但是如果我将 JPanel Layout 设置为 null,滚动条将不会显示!

I tried

我试过

JScrollPane scrollbar1 = 
  new JScrollPane(
    ta1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

But didn't work because of the null layout.

但由于空布局而不起作用。

Here is my current code:

这是我当前的代码:

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);
    }
}

I want to make it work correctly. If someone can help me, leave a comment below please!

我想让它正常工作。如果有人可以帮助我,请在下面发表评论!

采纳答案by Jabir

I have corrected all the problems following is the working code. Please read comments for the changes.

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

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);
    }
}

EDIT. Please read tutorial from oracle on Java. And start using appropriate layout manager... Hope it helps

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

回答by Josh M

You have to pass in your JTextAreato your JScrollPaneconstructor, and then add your JScrollPaneobject to your Containeras opposed to just the JTextArea. So it would look something like this:

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

JScrollPane scr = new JScrollPane(ta1);
panel.add(scr);

回答by mKorbel

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

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

  • why please you are you going to smash with your head wall, JScrollPaneis designated for Dynamic, Resizable LayoutManager, AbsoluteLayoutcan broken this its basic properties

  • starting from top

    1. public class app extends JFrame {

      • public class App {---> Java Naming Conventions
      • and not extends anything, create JFrameas local variable
    2. new app();---> se Oracle tutorial Initial Thread

    3. create another JPanel, put there JTextFieldand JButton

    4. did you overlay something tf1.setBounds(5,5,this.getWidth()-120,20);

    5. NullLayoutdoesn't works correctly without using Insets

    6. change built_in FlowLayoutfor JPanel painel = new JPanel(null);to BorderLayout, there put JScrollPanewith JTextAreato CENTER area

    7. you can to put JScrollPanewith JTextAreato JFrames CENTER areadirectly and another JPanelwith JTextFieldand JButtonto SOUTHor NORTH, JFramehas BorderLayoutimplemented in API

    8. JScrollPaneshowing JScrollbarsonly in the case that its Dimensionis smaller than JComponentplaced there

    9. use JFrame.pack()instead of setSize, this line should be before setVisible

  • 为什么你要砸你的头墙,JScrollPane被指定为动态,可调整大小LayoutManagerAbsoluteLayout可以打破它的基本属性

  • 从顶部开始

    1. public class app extends JFrame {

      • public class App {---> Java 命名约定
      • 并且不扩展任何东西,创建JFrame为局部变量
    2. new app();---> se Oracle 教程初始线程

    3. 创建另一个JPanel,放在那里,JTextField然后JButton

    4. 你有没有覆盖一些东西 tf1.setBounds(5,5,this.getWidth()-120,20);

    5. NullLayout不使用就不能正常工作 Insets

    6. 将 built_in 更改FlowLayoutJPanel painel = new JPanel(null);to BorderLayout,然后JScrollPane使用JTextAreatoCENTER area

    7. 您可以直接JScrollPane使用JTextAreatoJFrames CENTER area和另一个JPanelwithJTextFieldJButtonto SOUTHor NORTHJFrameBorderLayout在 API 中实现

    8. JScrollPaneJScrollbars仅在其Dimension小于JComponent放置在那里的情况下显示

    9. 使用JFrame.pack()而不是setSize,这一行应该在之前setVisible

回答by trashgod

Here's a basic example of many of @mKorbels points. Note how the default layout of JPanel(), FlowLayout(), uses the preferred size of its components. The call to f.setSize()is optional to force the scrollbar to appear.

这是许多@mKorbels的基本示例。注怎样的默认布局JPanel()FlowLayout(),使用其组件的首选尺寸。f.setSize()对强制滚动条显示的调用是可选的。

image

图片

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.*;

public class App {

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

    public App() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame("Application");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JPanel panel = new JPanel();
                JTextField tf1 = new JTextField("Some random text", 15);
                tf1.setColumns(10);
                tf1.setText("Omg");
                panel.add(tf1);
                JButton button1 = new JButton("Send");
                panel.add(button1);
                JTextArea ta = new JTextArea(15, 20);
                JScrollPane scr = new JScrollPane(ta);
                scr.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                ta.setLineWrap(true);
                ta.setWrapStyleWord(true);
                f.add(panel, BorderLayout.NORTH);
                f.add(scr, BorderLayout.CENTER);
                f.pack();
                Dimension d = scr.getPreferredSize();
                f.setSize(d.width, d.height);
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}