簡體   English   中英

Java-放置JscrollBar后未顯示JTextArea

[英]Java - JTextArea not shown after putting JscrollBar

我是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,但無法弄清楚。

由於我仍處於學習階段,將不勝感激地提供經過更正的代碼的詳細說明。

提前致謝。

首先看一下在容器布置組件以及如何使用滾動窗格如何使用文本區域可能不會造成傷害

現在的問題是我在窗口上獲得了一個禁用的滾動條,但是我看不到我的文本區域。

可能的問題是,您看到的是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);
        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM