Java JTextArea 中的滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18292659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ScrollBar in JTextArea
提问by John Black
I want to create a scroll bar in the textarea but If I set the JPanel Layout to null, the scrollbar won't show!
我想在 textarea 中创建一个滚动条,但是如果我将 JPanel Layout 设置为 null,滚动条将不会显示!
I tried
我试过
JScrollPane scrollbar1 =
new JScrollPane(
ta1,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
But didn't work because of the null layout.
但由于空布局而不起作用。
Here is my current code:
这是我当前的代码:
import javax.swing.*;
import java.awt.*;
public class app extends JFrame {
public static void main(String[] args)
{
new app();
}
public app()
{
this.setSize(400,400);
this.setLocation(0,0);
this.setResizable(false);
this.setTitle("Application");
JPanel painel = new JPanel(null);
// Creating the Input
JTextField tf1 = new JTextField("Some random text", 15);
tf1.setBounds(5,5,this.getWidth()-120,20);
tf1.setColumns(10);
tf1.setText("Omg");
painel.add(tf1);
// Creating the button
JButton button1 = new JButton("Send");
button1.setBounds(290, 5, 100, 19);
painel.add(button1);
// Creating the TextArea
JTextArea ta1 = new JTextArea(15, 20);
JScrollPane scr = new JScrollPane();
ta1.setBounds(5, 35, 385, 330);
ta1.setLineWrap(true);
ta1.setWrapStyleWord(true);
painel.add(ta1);
this.add(painel);
this.setVisible(true);
}
}
I want to make it work correctly. If someone can help me, leave a comment below please!
我想让它正常工作。如果有人可以帮助我,请在下面发表评论!
采纳答案by Jabir
I have corrected all the problems following is the working code. Please read comments for the changes.
我已经纠正了以下所有问题是工作代码。请阅读有关更改的评论。
import javax.swing.*;
import java.awt.*;
public class app extends JFrame {
public static void main(String[] args) {
new app();
}
public app() {
this.setSize(400, 400);
this.setLocation(0, 0);
this.setResizable(false);
this.setTitle("Application");
JPanel painel = new JPanel(null);
// Creating the Input
JTextField tf1 = new JTextField("Some random text", 15);
tf1.setBounds(5, 5, this.getWidth() - 120, 20);
tf1.setColumns(10);
tf1.setText("Omg");
// resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
painel.add(tf1);
// Creating the button
JButton button1 = new JButton("Send");
button1.setBounds(290, 5, 100, 19);
painel.add(button1);
// Creating the TextArea
JTextArea ta1 = new JTextArea(15, 20);
JScrollPane scr = new JScrollPane(ta1,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane
ta1.setBounds(5, 35, 385, 330);
ta1.setLineWrap(true);
ta1.setWrapStyleWord(true);
scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout
painel.add(scr);// Add you scroll pane to container
this.add(painel);
this.setVisible(true);
}
}
EDIT. Please read tutorial from oracle on Java. And start using appropriate layout manager... Hope it helps
编辑。请阅读 oracle 关于 Java 的教程。并开始使用适当的布局管理器...希望它有所帮助
回答by Josh M
You have to pass in your JTextArea
to your JScrollPane
constructor, and then add your JScrollPane
object to your Container
as opposed to just the JTextArea
. So it would look something like this:
您必须将您的对象传递JTextArea
给您的JScrollPane
构造函数,然后将您的JScrollPane
对象添加到您的对象中Container
,而不仅仅是JTextArea
. 所以它看起来像这样:
JScrollPane scr = new JScrollPane(ta1);
panel.add(scr);
回答by mKorbel
If someone can help me, leave a comment below please!
If someone can help me, leave a comment below please!
why please you are you going to smash with your head wall,
JScrollPane
is designated for Dynamic, ResizableLayoutManager
,AbsoluteLayout
can broken this its basic propertiesstarting from top
public class app extends JFrame {
public class App {
---> Java Naming Conventions- and not extends anything, create
JFrame
as local variable
new app();
---> se Oracle tutorial Initial Threadcreate another
JPanel
, put thereJTextField
andJButton
did you overlay something
tf1.setBounds(5,5,this.getWidth()-120,20);
NullLayout
doesn't works correctly without usingInsets
change built_in
FlowLayout
forJPanel painel = new JPanel(null);
toBorderLayout
, there putJScrollPane
withJTextArea
toCENTER area
you can to put
JScrollPane
withJTextArea
toJFrames CENTER area
directly and anotherJPanel
withJTextField
andJButton
toSOUTH
orNORTH
,JFrame
hasBorderLayout
implemented in APIJScrollPane
showingJScrollbars
only in the case that itsDimension
is smaller thanJComponent
placed thereuse
JFrame.pack()
instead ofsetSize
, this line should be beforesetVisible
为什么你要砸你的头墙,
JScrollPane
被指定为动态,可调整大小LayoutManager
,AbsoluteLayout
可以打破它的基本属性从顶部开始
public class app extends JFrame {
public class App {
---> Java 命名约定- 并且不扩展任何东西,创建
JFrame
为局部变量
new app();
---> se Oracle 教程初始线程创建另一个
JPanel
,放在那里,JTextField
然后JButton
你有没有覆盖一些东西
tf1.setBounds(5,5,this.getWidth()-120,20);
NullLayout
不使用就不能正常工作Insets
将 built_in 更改
FlowLayout
为JPanel painel = new JPanel(null);
toBorderLayout
,然后JScrollPane
使用JTextArea
toCENTER area
您可以直接
JScrollPane
使用JTextArea
toJFrames CENTER area
和另一个JPanel
withJTextField
和JButton
toSOUTH
orNORTH
,JFrame
已BorderLayout
在 API 中实现JScrollPane
JScrollbars
仅在其Dimension
小于JComponent
放置在那里的情况下显示使用
JFrame.pack()
而不是setSize
,这一行应该在之前setVisible
回答by trashgod
Here's a basic example of many of @mKorbels points. Note how the default layout of JPanel()
, FlowLayout()
, uses the preferred size of its components. The call to f.setSize()
is optional to force the scrollbar to appear.
这是许多@mKorbels点的基本示例。注怎样的默认布局JPanel()
,FlowLayout()
,使用其组件的首选尺寸。f.setSize()
对强制滚动条显示的调用是可选的。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.*;
public class App {
public static void main(String[] args) {
new App();
}
public App() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame("Application");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTextField tf1 = new JTextField("Some random text", 15);
tf1.setColumns(10);
tf1.setText("Omg");
panel.add(tf1);
JButton button1 = new JButton("Send");
panel.add(button1);
JTextArea ta = new JTextArea(15, 20);
JScrollPane scr = new JScrollPane(ta);
scr.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
f.add(panel, BorderLayout.NORTH);
f.add(scr, BorderLayout.CENTER);
f.pack();
Dimension d = scr.getPreferredSize();
f.setSize(d.width, d.height);
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}