【Java笔记】给JTextArea添加滚动条

做课设时碰到了这样的问题,已经把JTextArea放入JScrollPane,但无法正确显示,不是只能显示部分文字就是连文字都显示不出来。研究了好久终于解决,贴部分代码如下,我用这两种方法解决。

方法一:

直接将JTextArea放进JScrollPane,再将JScrollPane加到JFrame里

	JFrame frame = new JFrame();//创建一个JFrame窗体
	frame.setBounds(100, 100, 400, 200);//自定义Jframe位置并设置大小为100*50
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用于关闭窗体
	frame.getContentPane().setLayout(null);//设置为绝对布局
	
	JTextArea text1= new JTextArea("置换结果显示区域");//创建一个JTextArea,这里不必设置大小
	JScrollPane scrollpane=new JScrollPane();//创建滚动条面板
	scrollpane.setBounds(20,20,100,50);//自定义该面板位置并设置大小为100*50
	
	scrollpane.setViewportView(text1);//(这是关键!不是用add)把text1组件放到滚动面板里
	frame.add(scrollpane);//将滚动条面板加到窗体
	frame.setVisible(true);//放在最后,让整个窗体可视

△下面是JScrollPane.setViewportView(Component view)的API:

Creates a viewport if necessary and then sets its view. Applicationsthat don’t provide the view directly to the JScrollPaneconstructorshould use this method to specify the scrollable child that’s goingto be displayed in the scrollpane. For example:

     JScrollPane scrollpane = new JScrollPane();
     scrollpane.setViewportView(myBigComponentToScroll);

Applications should not add children directly to the scrollpane.

也就是:不直接为JScrollPane构造方法提供视图的应用程序,应使用setViewportView()这种方法,指定将显示在滚动窗格中的滚动组件子级。应用程序不应将子级直接添加到滚动窗格。


△不能用JScrollPane.add(JTextArea),这是无法将JTextArea真正加入到滚动条面板中去的。下面是JScrollPane.add(comp)的API:

Appends the specified component to the end of this container.This is a convenience method for addImpl.

This method changes layout-related information, and therefore,invalidates the component hierarchy. If the container has already beendisplayed, the hierarchy must be validated thereafter in order todisplay the added component.

也就是:将指定的组件附加到此容器的末尾。这是AddImpl的一种方便方法。此方法更改布局相关信息,因此使组件层次结构无效。如果容器已被禁用,则必须在此后验证层次结构,以便显示添加的组件。


方法二:

先将JTextArea放进JPanel,再将JPanel嵌入JScrollPane,最后把JScrollPane放到JFrame里。

	JFrame frame = new JFrame();//创建一个JFrame窗体
	frame.setBounds(100, 100, 400, 200);//自定义Jframe位置并设置大小为100*50
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用于关闭窗体
	frame.getContentPane().setLayout(null);//设置为绝对布局
	
	JTextArea text1= new JTextArea("置换结果显示区域");//创建一个JTextArea
	JPanel panel=new JPanel();//创建一个普通面板
	panel.setBounds(20,20,100,50);//设置普通面板位置和大小,不能省略
	panel.setLayout(new BorderLayout());//让JTextArea平铺整个JPanel
	//文本域嵌入普通面板后用setBounds()设置JTextArea好像并没有用
	//可以很明显看出仅在有文字的部分会显示白色,其他部分显示灰色。
	panel.add(text1);//将文本域添加进普通面板

	JScrollPane scrollpane = new JScrollPane();//创建滚动条面板
	scrollpane.setBounds(20,20,100,50);//设置滚动条面板位置和大小
	/*将普通面板嵌入带滚动条的面板,所在位置由后者决定*/
	
	scrollpane.getViewport().add(panel);//(这是关键!同样不能用add)将普通面板加到滚动面板里
	frame.add(scrollpane);//将滚动条面板加到窗体
	frame.setVisible(true);//放在最后,让整个窗体可视

△ JScrollPane.getViewport().add(JPanel)不能用JScrollPane.add(JPanel)替代,同样无法将JTextArea正确显示。JScrollPane.getViewport()的API如下所示:

Returns the current JViewport

也就是:返回当前的JViewport。

getViewport()这个方法返回一个JViewport对象,JViewport是用于查看基础信息的“视口”或“观察孔”,可以把它看成是视图层。要将JPanel添加到这个视口才可见。



最后,顺手补充一下
【JFrame.setContentPane(JPanel) 与 JFrame.add(JPanel)的区别】

我们都知道,实例化一个新的Frame会有一个默认的panel产生。
JFrame.setContentPane(JPanel)是把Frame的默认Panel改为JP这个panel;
JFrame.add(JPanel)是在Frame的默认panel上添加JP这个panel。
现在一般认为没有什么区别,因为结果显示出来的效果一样。早期版本是不一样的。

JFrame.setContentPane(JPanel)和JFrame.getContentPane().add(JPanel)效果相同。
在JDK比较后面的版本重载了addImpl方法,所以现在JFrame.add(JPanel)和上面这两个也没有什么区别。




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值