[英]Java Swing JTextArea scrollbar not showing inside
I am trying to add scrollbars in JTextArea but scrollbars are not displaying in the textarea.. 我正在尝试在JTextArea中添加滚动条,但滚动条未显示在textarea中。
Here is my code, 这是我的代码,
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("MySql Console");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
panel.setLayout(null);
JTextArea txtQuery = new JTextArea ();
txtQuery.setBounds(10, 10, 365, 45);
JScrollPane scroll = new JScrollPane (txtQuery,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//scroll.setPreferredSize(new Dimension(100, 50));
panel.add(scroll);
panel.add(txtQuery);
frame.setVisible(true);
}
}
tried some alternatives from internet but still not working. 尝试了一些来自互联网的替代方法,但仍然无法正常工作。
panel.setLayout(null);
JTextArea txtQuery = new JTextArea ();
txtQuery.setBounds(10, 10, 365, 45);
JScrollPane scroll = new JScrollPane (txtQuery,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//scroll.setPreferredSize(new Dimension(100, 50));
panel.add(scroll);
panel.add(txtQuery);
Don't use a null layout. 不要使用空布局。 The layout manager will determine the preferred size of a component and in turn the scrollpane can then determine when a scrollbar is required.
布局管理器将确定组件的首选大小,然后滚动窗格可以确定何时需要滚动条。 The scrollbar will appear automatically as you add text to the text area.
将文本添加到文本区域时,滚动条将自动出现。
Don't add the text area to the panel. 不要将文本区域添加到面板中。 A component can only have a single parent.
一个组件只能有一个单亲。 You already added the text area to the scrollpane.
您已经将文本区域添加到了滚动窗格。
Give the text area a preferred size by specifying the row/column of the text area. 通过指定文本区域的行/列,为文本区域提供首选大小。
So your modified code should be something like: 因此,修改后的代码应类似于:
//panel.setLayout(null);
JTextArea txtQuery = new JTextArea (5, 20);
//txtQuery.setBounds(10, 10, 365, 45);
JScrollPane scroll = new JScrollPane (txtQuery);
//scroll.setPreferredSize(new Dimension(100, 50));
panel.add(scroll);
//panel.add(txtQuery);
please remove them below line, then everything will work fine. 请删除它们在行下面,然后一切都会正常。
panel.setLayout(null);
to work your application more effectively consider points in @camickr answer also. 为了更有效地工作您的应用程序,请同时考虑@camickr答案中的要点。
Would it help to add a viewport to your JTextArea? 将视口添加到您的JTextArea是否有帮助?
Something along the lines of jScrollPane.setViewportView(jTextArea);
jScrollPane.setViewportView(jTextArea);
? ? If that doesn't work you may need to make a GroupLayout block.
如果这样不起作用,则可能需要制作一个GroupLayout块。 Which isn't necessarily hard all things considered.
所有事情都不一定很困难。 I have this :
我有这个 :
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(118, Short.MAX_VALUE)
.addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(116, 116, 116))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(105, Short.MAX_VALUE)
.addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(99, 99, 99))
);
pack();
}
Hope this helps! 希望这可以帮助! Good luck :D
祝你好运:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.