我编写了一个基本的 swing 代码,其中将 JTextArea
包装到 JScrollPane
中。但是,即使文本区域内容超出可见的 JFrame
区域,滚动条也不会显示。
代码如下 -
public class TestArea {
private JTextArea area;
private JScrollPane scroll;
private JFrame frame;
public TestArea(){
frame = new JFrame("Testing");
frame.setSize(new Dimension(200, 300));
area = new JTextArea();
area.setEditable(false);
scroll = new JScrollPane(area);
frame.getContentPane().add(scroll);
area.setLayout(new BoxLayout(area, BoxLayout.Y_AXIS));
area.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
area.setBackground(Color.WHITE);
addMessage();
}
private void addMessage() {
area.add(new JLabel("Can you see me..... can you see me"));
for(int i=0; i<30; i++){
area.add(new JLabel(Integer.toString(i)));
System.out.println(Integer.toString(i));
frame.setVisible(true);
}
}
public static void main(String[] args){
new TestArea();
}
}
使用BoxLayout
的原因是我严格希望输出在y轴上对齐。这是输出屏幕截图的链接 https://app.box.com/s/rgeyajgvk0ppude399my
正如您所看到的,滚动条没有出现。谁能帮我解决这个问题吗?
最佳答案
在本例中,JTextArea 不能成为 JComponent、JLabels 的容器
JTextArea 指定用于纯文本
- 使用 JPanel 作为 JLabels 数组的容器,而不是 JTextArea
关于java - 滚动条不适用于我的 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27188925/