JTextArea中的ScrollBar

时间:2013-08-17 19:42:04

标签: java swing jpanel jscrollpane jtextarea

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

我试过

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

但由于空布局而无效。

这是我目前的代码:

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

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

4 个答案:

答案 0 :(得分:2)

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

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

  • 从顶部开始

    1. public class app extends JFrame {

      • public class App { ---> Java命名约定
      • 并没有扩展任何内容,创建JFrame作为本地变量
    2. new app(); ---> se Oracle教程初始线程

    3. 创建另一个JPanel,放置JTextFieldJButton

    4. 您是否覆盖了tf1.setBounds(5,5,this.getWidth()-120,20);

    5. 如果不使用NullLayout

    6. Insets无效

    7. FlowLayout的built_in JPanel painel = new JPanel(null);更改为BorderLayoutJScrollPaneJTextArea更改为CENTER area

      < / LI>
    8. 您可以将JScrollPaneJTextArea直接放在JFrames CENTER area,将另一个JPanelJTextFieldJButton放到{{} 1}}或SOUTHNORTH已在API中实施JFrame

    9. BorderLayout仅在JScrollPane小于JScrollbars的情况下显示Dimension

    10. 使用JComponent代替JFrame.pack(),此行应在setSize之前

答案 1 :(得分:2)

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

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。请阅读Java上的oracle教程。并开始使用适当的布局管理器... 希望它有所帮助

答案 2 :(得分:2)

以下是许多@mKorbels points的基本示例。请注意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);
            }
        });
    }
}

答案 3 :(得分:1)

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

JScrollPane scr = new JScrollPane(ta1);
panel.add(scr);
相关问题