1

我有一个 JTextArea,它位于 JScrollPane 内,而 JScrollPane 又位于 JPanel 内,而 JTextArea 又位于 JTabbedPane 的 Tab 内。

我知道文本被添加到我的 JTextArea,但是当我在选项卡之间移动时,JTextArea 不可见。要阅读文本,我必须选择 JTextArea 内的文本,然后显示 JTextArea 背景的白色。如果我不选择,我什么都看不到。

我已经尝试过通常的方法revalidate();repaint()但它们对我不起作用。以下是一些有问题的代码:

public void writeLogEntry(Alarm alarm)
{


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

这是与 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);

我究竟做错了什么?

4

1 回答 1

5

您不应该将组件直接添加到滚动窗格中。相反,您将组件添加到视口。或者,您在创建滚动窗格时指定组件,该组件将为您添加到视口中:

//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);
于 2013-04-26T04:08:15.860 回答