Java - 放入 JscrollBar 后 JTextArea 未显示

标签 java swing jframe jscrollpane

我是 Jframes 新手,我想设计一个带有文本框和两个按钮的窗口。除了滚动条部分之外,我能够使其正常工作。 我编写了下面的代码来启用文本区域的滚动条。

private JTextArea outputPane;
outputPane = new JTextArea();
outputPane.setColumns(20);
outputPane.setRows(5);
outputPane.setFont(new Font("Monospaced", Font.PLAIN, 18));
outputPane.setBounds(12, 13, 408, 189);
contentPane.add(outputPane);

JScrollPane scrollPane = new JScrollPane(outputPane);
jScrollPane1.setBounds(399, 13, 21, 189);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

现在的问题是我在窗口上看到禁用的滚动条,但我看不到我的文本区域。

请帮我解决这个问题。我什至尝试使用 WindowsBuilder 但我无法弄清楚。

由于我仍处于学习阶段,因此将不胜感激对更正代码的详细解释。

提前致谢。

最佳答案

首先查看 Laying Out Components Within a ContainerHow to Use Scroll PanesHow to Use Text Areas可能不会受伤

Now the problem is I am getting a disabled scrollbar on the window but I cannot see my Text Area.

可能的问题是,您看到的JTextArea,“禁用”滚动条只是因为您使用scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_​​ALWAYS,这即使没有任何内容可滚动,也会始终显示滚动条,因此它可能看起来是空的。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        // Swing is not thread safe, so need to get started in the
        // Event Dispatching Thread before we do anything
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    // I simply hate the default look and feel                      
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                // Always better to create an instance of a window
                // to display you content then to extend from one  
                // directly...
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    // Our main UI, I do it this way so I'm not locked into a single
    // use case and can decide how I want to use the view
    public class TestPane extends JPanel {

        public TestPane() {
            // The default layout is a FlowLayout, so we want to change
            // this will allow the main component to occupy the whole
            // available space
            setLayout(new BorderLayout());
            // Providing "sizing" hints, 10 rows, 20 columns, this is
            // platform independent, so it will size accordingly
            JTextArea ta = new JTextArea(10, 20);
            JScrollPane sp = new JScrollPane(ta);
            add(sp);
        }

    }

}

关于Java - 放入 JscrollBar 后 JTextArea 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34219421/

相关文章:

java - 如何合并两个并行执行的两个 react 器通量,返回列表并合并结果

java - 如何将字符串完美居中 Jpanel 中心

java - 变量存储在 session 中?/在 JFrame 之间传递变量

java - 从另一个类调用 IntelliJ Swing Forms

java - 我怎样才能让我的程序回复答案

java - JFrame 的 keylistener 和 actionlistener

java - 如何在 JFrame 中的其他内容下面添加鼠标监听器?

java - MapStruct 无法映射需要外部变量的嵌套对象

java - 用于从 IOB 带注释的训练集训练命名实体识别器模型的文档

java - 排除同时受多个 spring @Profiles 控制的 bean