Java中的JScrollPane [英] JScrollPane in Java

查看:589
本文介绍了Java中的JScrollPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一些代码来查看滚动窗格的功能,但我的代码从未起作用。
这里是代码,

I wrote a little code to see how the scroll Pane functions but my code never worked. here's the code,

public Fenetre(){
this.setTitle("Data Simulator");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
String hello = "hello";
int number = 69;
JPanel content = new JPanel();
content.setBackground(Color.LIGHT_GRAY);
//Box imad = Box.createHorizontalBox();
JTextArea textArea = new JTextArea(10, 10);
JLabel imad = new JLabel();
imad.setText(hello + " your favorite number is " + number + "\nRight?");
JScrollPane scrollPane = new JScrollPane();
setPreferredSize(new Dimension(450, 110));

scrollPane.setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setEnabled(true);
scrollPane.setWheelScrollingEnabled(true);
scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
add(scrollPane, BorderLayout.CENTER);
//---------------------------------------------
//On ajoute le conteneur
scrollPane.add(textArea);
scrollPane.add(imad);
content.add(textArea);
content.add(imad);
content.add(scrollPane);
this.setContentPane(content);
this.setVisible(true);
this.setResizable(false);

}

当我运行它时,我得到一个带有textArea的小窗口并且在文本区域旁边有一个非常小的白色方块,这是我想的滚动窗格,因为当我从代码中删除它时,这个方块消失了。当我在文本区域中写入并超出窗口的尺寸时,我无法使用鼠标滚轮垂直滚动,而不是水平滚动。我在互联网上看到很多例子,我无法理解为什么我的代码不起作用?
任何帮助解释scrollpane如何工作?

}
When I run it, I get a little window with the textArea and next to the text area a very little white square, which is the scrollpane i suppose because when I remove it from the code, this square disappears. When I write in the text area and exceed the window's dimension, I can't scroll vertically using the mouse wheel, and not horizontally at all. I saw many examples on internet and I can't understand why my code doesn't work?? Any help explaining how scrollpane works?

推荐答案

scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);

只能将一个组件添加到滚动窗格的视口中,因此标签会替换文本区域。

Only one component can be added to the viewport of the scroll pane, so the label replaces the text area.

content.add(textArea);
content.add(imad);

组件只能有一个父组件。上面的代码从滚动窗格中删除了标签,因此滚动窗格中没有任何内容。

A component can only have a single parent. The above code removes the label from the scrollpane, so nothing is now in the scrollpane.

尝试类似:

JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );

要获得更好的解决方案,请从如何使用文本区域然后修改代码。这样,您将从遵循Swing标准的更好的结构化程序开始。

For a better solution, start with the working example found in the Swing tutorial on How to Use Text Areas and then modify the code. This way you will start with a better structured program that follows Swing standards.

这篇关于Java中的JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆