[英]JTextArea Doesn't Show Up on JPanel inside JTabbedPane
我有一个JTextArea,它位于JScrollPane内,而JScrollPane又位于JPanel内,又位于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);
我究竟做错了什么?
您不应该将组件直接添加到滚动窗格中。 而是将组件添加到视口。 或者,在创建滚动窗格时指定组件,该组件将为您添加到视口中:
//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.