JTextArea中的滚动条

3

我想在文本区域中创建一个滚动条,但如果我将JPanel布局设置为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
@Kiheru 我说过我尝试过那样做,但是没有成功。 - John Black
它确实可以工作,但是由于您正在尝试手动创建布局(是的,我尝试过),因此需要正确设置滚动窗格的边界。 - kiheru
你不应该设置滚动面板的边界。你不应该使用空布局。空布局会导致这样的问题。你应该使用布局管理器。Swing是为了与布局管理器一起使用而设计的。阅读JTextArea API并跟随链接到Swing教程,以获取展示如何正确使用Swing的工作示例。 - camickr
可能是JTextArea没有滚动条的重复问题。 - PhoneixS
4个回答

3

我已经纠正了所有问题,下面是可工作的代码。请查看注释以了解更改。

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 教程。并开始使用适当的布局管理器...希望这能帮到您。


4
“null”布局是一个糟糕的建议;请参考Initial Threads - trashgod

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

2

您需要将JTextArea传递给JScrollPane构造函数,然后将JScrollPane对象添加到Container中,而不仅仅是JTextArea。因此,它应该类似于这样:

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

1
+1,@JohnBlack,你发布的代码完全错误。你不应该使用null布局和setBounds()。布局管理器将确定组件的大小/位置。你正在将文本区域添加到面板而不是添加到滚动窗格的视口中。Josh给你的代码是正确的。唯一的其他更改是你不应该使用null布局创建面板。我不知道你从哪里复制了那段代码,但你应该忽略那个网站的示例。请阅读Swing教程。 - camickr

2

如果有人能帮我,请在下面留言!

  • 为什么请你要用头撞墙呢?JScrollPane 是为动态、可调整大小的 LayoutManager 设计的,使用 AbsoluteLayout 会破坏其基本属性。

  • 从顶部开始:

    1. public class app extends JFrame {

      • public class App { ---> Java 命名规范
      • 不继承任何东西,将 JFrame 创建为局部变量
    2. new app(); ---> 参见 Oracle 教程 Initial Thread(初始线程)

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

    4. 你是否覆盖了某些东西?tf1.setBounds(5,5,this.getWidth()-120,20);

    5. 不使用 InsetsNullLayout 就无法正常工作

    6. 将内置的 FlowLayout 更改为 JPanel painel = new JPanel(null); 中的 BorderLayout,在其中放置带有 JTextAreaJScrollPaneCENTER area

    7. 你可以直接将带有 JTextAreaJScrollPane 放置到 JFrames CENTER area 中,将另一个带有 JTextFieldJButtonJPanel 放置到 SOUTHNORTH 中,JFrame 在 API 中实现了 BorderLayout

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接