Java - 放置JscrollBar后未显示JTextArea

时间:2015-12-11 08:51:47

标签: java swing jframe jscrollpane

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

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,但我无法弄明白。

由于我还处于学习阶段,因此我们将会对使用更正后代码的详细解释表示赞赏。

提前致谢。

1 个答案:

答案 0 :(得分:2)

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

  

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

可能的问题是,您看到了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);
        }

    }

}