做课设时碰到了这样的问题,已经把JTextArea放入JScrollPane,但无法正确显示,不是只能显示部分文字就是连文字都显示不出来。研究了好久终于解决,贴部分代码如下,我用这两种方法解决。
直接将JTextArea放进JScrollPane,再将JScrollPane加到JFrame里
JFrame frame = new JFrame();
frame.setBounds(100, 100, 400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea text1= new JTextArea("置换结果显示区域");
JScrollPane scrollpane=new JScrollPane();
scrollpane.setBounds(20,20,100,50);
scrollpane.setViewportView(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();
frame.setBounds(100, 100, 400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea text1= new JTextArea("置换结果显示区域");
JPanel panel=new JPanel();
panel.setBounds(20,20,100,50);
panel.setLayout(new BorderLayout());
panel.add(text1);
JScrollPane scrollpane = new JScrollPane();
scrollpane.setBounds(20,20,100,50);
scrollpane.getViewport().add(panel);
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)和上面这两个也没有什么区别。
开发swing时给JTextArea添加滚动条。
JPanel panel = new JPanel();
JTextArea textArea = new JTextArea();
textArea.setBounds(0, 21, 237, 324);//文本区位置大小
textArea.setLineWrap(true);//自动换行
panel.add(textArea);
JScrol...
做swing给JTextArea添加滚动条的时候折腾了好久,一直出不来效果,我直接用SWING加进去,可是看不到效果,后来就直接添加代码了,但是滚动条大小又一直困扰了我,位置大小如何设定呢?
其实做法很简单,只要给滚动条加一个setViewportView(JTextArea);的方法就可以了,之前一直用add(JTextArea);这个方法,界面总是出不来,后来仔细查看了一下之前做的例子,原来是用
[size=small]应将JTextArea置于JScrollPanel中
若要使只有垂直滚动条而没有水平滚动条,使用JTextArea.setLineWrap(true),自动换行。
以下摘自[url]http://zhidao.baidu.com/question/31571035.html?fr=qrl[/url]
JTextArea txaDisplay = new J...
第一个是计算器 感觉有点无聊
第二个是记事本 倒是蛮有趣的
参考了@WANGHA_1的代码 但是觉得他写的不是很适合新手来看 所以稍微改动了一下 就算是记录一个开发流程 希望能给大家带来启发
这是他的代码 对我帮助很大 谢谢
https://blog.csdn.net/wangha_1/article/details/78937248
OK 进...
实现滚动条的置底
直接设置JTextArea显示区域的起始或结束位置;通过调用public void setSelectionStart(int selectionStart)或者
public void setSelectionEnd(int selectionEnd
//给JTextArea设置换行
text.setLineWrap(true); //激活自动换行功能
text.setWrapStyleWord(true); // 激活断行不断字功能
//实现滚轮
frame.add(new JScrollPane(text)); //将text放入JScrollPane内