简体   繁体   English

JTextArea不在JTabbedPane内的JPanel上显示

[英]JTextArea Doesn't Show Up on JPanel inside JTabbedPane

I have a JTextArea that sits inside a JScrollPane which in turn sits inside a JPanel and that in turn sits inside a Tab of a JTabbedPane. 我有一个JTextArea,它位于JScrollPane内,而JScrollPane又位于JPanel内,又位于JTabbedPane的Tab内。

I know that text gets added to my JTextArea, but when I move between the tabs, the JTextArea is not visible. 我知道文本已添加到我的JTextArea中,但是当我在选项卡之间移动时,JTextArea不可见。 To read the text, I have to select the text inside the JTextArea, and that then brings up the White colour of the background of the JTextArea. 要阅读文本,我必须在JTextArea中选择文本,然后调出JTextArea背景的白色。 If I don't select, I don't see anything. 如果我没有选择,我什么也看不到。

I've tried the usual revalidate(); 我已经尝试了通常的revalidate(); and repaint() but they're not working for me. repaint()但它们对我不起作用。 Here is some of the code in question: 这是一些有问题的代码:

public void writeLogEntry(Alarm alarm)
{


    String value = "Blah Blah Blah";
    logTextArea.append(value);
    SwingUtilities.getWindowAncestor(contentPane).revalidate();
    repaint();
    revalidate();
    setVisible(true);
}

And here is the code for the elements related to the JTextArea: 以下是与JTextArea相关的元素的代码:

JPanel logPnl = new JPanel();
logPnl.setLayout(new BorderLayout(10, 10));
JScrollPane logScrollPane = new JScrollPane();
logScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
logTextArea = new JTextArea("blah blah");
logTextArea.setBounds(10, 10, 550, 300);
logTextArea.setEditable(false);
logScrollPane.add(logTextArea);
logPnl.add(logScrollPane);

contentTabs.addTab("Alarms Log", null, logPnl, "View Log");
contentPane.add(contentTabs);

What am I doing wrong? 我究竟做错了什么?

You should not be adding components directly to a scrollpane. 您不应该将组件直接添加到滚动窗格中。 Instead you add components to the viewport. 而是将组件添加到视口。 Or, you specify the component when you create the scrollpane and the component will get added to the viewport for you: 或者,在创建滚动窗格时指定组件,该组件将为您添加到视口中:

//JScrollPane logScrollPane = new JScrollPane();
logScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//logTextArea = new JTextArea("blah blah");
logTextArea = new JTextArea(5, 40);
logTextArea.setText("some text");
//logTextArea.setBounds(10, 10, 550, 300);
logTextArea.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(logTextArea);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM