gpt4 book ai didi

java - 滚动条不适用于我的 JTextArea

转载 作者:行者123 更新时间:2023-11-30 08:18:49 25 4
gpt4 key购买 nike

我编写了一个基本的 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 不能是 JComponents 的容器,在这种情况下是 JLabels

  • JTextArea 指定为纯文本


  • 使用 JPanel 作为 JLabel 数组的容器而不是 JTextArea

关于java - 滚动条不适用于我的 JTextArea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27188925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com