[英]JScrollPane not working on my JTextArea?
I'm not very familiar with java GUIs and layouts but I gotta admit, I didn't think it would be this tricky! 我对java GUI和布局不是很熟悉,但我必须承认,我不认为这会很棘手! I'm not quite sure what I'm doing here. 我不太确定我在这里做什么。
I merely want to add 2 textareas ontop of each other and add a jscrollpane to each of them. 我只想在彼此上面添加2个textareas并为每个添加一个jscrollpane。 But I can't get the JScrollPanes to work. 但我无法让JScrollPanes工作。 Here's how I currently got it. 这是我目前如何得到它。
public class Class extends JFrame {
public Class() {
super("Title");
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setResizable(false);
JTextArea window1 = new JTextArea("text");
window1.setEditable(false);
window1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
window1.setPreferredSize(new Dimension(200, 250));
window1.setLineWrap(true);
add(window1);
JScrollPane scroll = new JScrollPane(window1);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroll);
JTextArea window2 = new JTextArea();
window2.setEditable(true);
window2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
window2.setPreferredSize(new Dimension(100, 50));
window2.setLineWrap(true);
add(window2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
It looks good, there's a box with "text" and right of it there's the scrollpane. 它看起来不错,有一个带有“文本”的框,右边有滚动窗格。 Below them both there's the second JTextArea (no scrollpane yet). 在他们下面都有第二个JTextArea(还没有滚动窗格)。 The problem is that when I write text in window 1 and the text goes below, outside of the jtextarea, I expected the JScrollPane to actually be scrollable so I could scroll down to see the text at the bottom but nothing happens when I press it (and it doesn't change in size or anything either). 问题是当我在窗口1中写文本并且文本在jtextarea之外的下面时,我希望JScrollPane实际上是可滚动的,所以我可以向下滚动以查看底部的文本但是当我按下它时没有任何反应(并且它的大小或任何东西都没有变化)。 Anything noticable I've missed? 我遗漏了一些值得注意的事情吗?
The problem is that when I write text in window 1 and the text goes below, outside of the jtextarea, I expected the JScrollPane to actually be scrollable 问题是当我在窗口1中写文本并且文本在jtextarea之外的下面时,我期望JScrollPane实际上是可滚动的
JTextArea
has been included to JScrollPane
. JTextArea
已包含在JScrollPane
。 You should not add window1
. 你不应该添加window1
。
add(window1);// Remove this.
JScrollPane scroll = new JScrollPane(window1);//here you added window1
add(scroll);// This statement also added the JTextArea
//and do the operaton of above add.
You add your JTextArea
to JScrollPane
you needn't to add it to your JFrame
. 您将JTextArea
添加到JScrollPane
您无需将其添加到JFrame
。 Also setPreferedSize()
use for JScrollPane
, instead of directly to JTextArea
, because your JTextArea
will be not scrollable. 另外setPreferedSize()
用于JScrollPane
,而不是直接用于JTextArea
,因为你的JTextArea
将不可滚动。 I have changed your code, try it: 我已经更改了你的代码,试试看:
public class Class extends JFrame {
public Class () {
super("Title");
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setResizable(false);
JTextArea window1 = new JTextArea("text");
window1.setEditable(false);
window1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
window1.setLineWrap(true);
JScrollPane scroll1 = new JScrollPane(window1);
scroll1.setPreferredSize(new Dimension(200, 250));
scroll1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroll1);
JTextArea window2 = new JTextArea();
window2.setEditable(true);
window2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
window2.setLineWrap(true);
add(window2);
JScrollPane scroll2 = new JScrollPane(window2);
scroll2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll2.setPreferredSize(new Dimension(100, 50));
add(scroll2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String... s){
new Class();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.