【问题标题】:ScrollBar in JTextAreaJTextArea 中的滚动条
【发布时间】:2013-08-17 19:42:04
【问题描述】:

我想在 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);
    }
}

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

【问题讨论】:

  • 正确的方法是不使用null布局。无论如何,您需要将TextArea 放在滚动窗格中,然后将组合显示在滚动窗格中。
  • @Kiheru 我说过我尝试过这样做,但没有成功。
  • 它确实有效,但是您需要正确设置滚动窗格的边界,因为您正在尝试手动创建布局(是的,我试过了)。
  • 您不应该设置滚动窗格的边界。您不应该使用空布局。空布局会导致这样的问题。您应该使用布局管理器。 Swing 旨在与布局管理器一起使用。阅读 JTextArea API 并点击 Swing 教程的链接,以获取向您展示如何正确使用 Swing 的工作示例。
  • JTextArea no scroll bar的可能重复

标签: java swing jpanel jscrollpane jtextarea


【解决方案1】:

我已经纠正了所有问题,以下是工作代码。请阅读 cmets 了解更改。

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 教程。并开始使用适当的布局管理器...... 希望对你有帮助

【讨论】:

  • 一个null布局不好的建议;另见Initial Threads
  • 谢谢,这正是我想要的。
【解决方案2】:

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

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

【讨论】:

  • +1,@JohnBlack,您发布的代码完全错误。您不应该使用空布局和 setBounds()。布局管理器将确定组件的大小/位置。您正在将文本区域添加到面板中,而不是将其添加到滚动面板的视口中。乔希给你的代码是正确的。唯一的其他变化是您不应该使用空布局创建面板。我不知道您从哪里复制了该代码,但您应该忽略该网站示例。请阅读 Swing 教程。
【解决方案3】:

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(); --->看Oracle教程初始线程

    3. 创建另一个JPanel,把JTextFieldJButton放在那里

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

    5. 如果不使用InsetsNullLayout 将无法正常工作

    6. 将内置的FlowLayout 更改为JPanel painel = new JPanel(null);BorderLayout,将JScrollPaneJTextArea 更改为CENTER area

    7. 您可以将JScrollPaneJTextArea 直接放入JFrames CENTER area 并将另一个JPanelJTextFieldJButton 放入SOUTHNORTH, JFrame3 有@在API中实现

    8. JScrollPane 仅在其Dimension 小于JComponent 的情况下才显示JScrollbars

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

【讨论】:

    【解决方案4】:

    这是许多@mKorbels points 的基本示例。请注意JPanel()FlowLayout() 的默认布局如何使用其组件的首选大小。对f.setSize() 的调用是可选的,以强制显示滚动条。

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

    【讨论】:

      猜你喜欢
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2021-04-25
      • 2011-02-10
      • 2015-03-09
      • 2016-02-22
      • 2017-09-04
      相关资源
      最近更新 更多