给 JTextArea 添加滚动条
<span style="font-family: Arial, Helvetica, sans-serif; color: #333333;">在 Container 中给 JTextArea 添加滚动条:<div class="bct fc05 fc11 nbw-blog ztag js-fs2" style="line-height: 22px; font-size: 14px; text-align: left; color: #333333; margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 0px;">JFrame jf = new JFrame();
Container c = jf.getContentPane();
c.setLayout(null); //设置布局管理器为 null,即绝对定位
JTextArea jta = new JTextArea();
jta.setLineWrap(true); //设置自动换行,自动换行则不会出现横向的滚动条
jta.setEditable(true); //设置可编辑
JScrollPane jsp = new JScrollPane(jta); //添加滚动条
jta.setBounds(20,20,100,500); //设置 JTextArea 宽100,高500
jsp.setBounds(20,20,100,200); //设置 JScrollPane 宽100,高200
c.add(jsp); //将组件加入容器
在 JPanel 中给 JTextArea 添加滚动条:
注: JPanel 默认为流布局管理器
JFrame jf = new JFrame();
Container c = jf.getContentPane();
JPanel jp = new JPanel();
JTextArea jta = new JTextArea();
jta.setPreferredSize(new Dimension(100,500)); //设置 JTextArea 宽100,高500
JScrollPane jsp = new JScrollPane(jta); //添加滚动条
jsp.setPreferredSize(new Dimension(100,200)); //设置 JScrollPane 宽100,高200
注: 这里 JTextArea 的高度一定要大于 JScrollPane 的高度
jp.add(jsp); //将组件加入面板
c.add(jp); //将面板加入容器
以上出自:http://xy0607.blog.163.com/blog/static/1626642920071202147295/
<span style=""><div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding: 0px;">应将JTextArea置于JScrollPanel中
页:
[1]