简体   繁体   English

Java中的JScrollPane

[英]JScrollPane in Java

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);

} }
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. 当我运行它时,我得到一个带有textArea的小窗口,在文本区域旁边有一个很小的白色正方形,这是我想的滚动窗格,因为当我从代码中删除它时,这个正方形消失了。 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. 上面的代码从滚动窗格中删除了标签,因此滚动窗格中现在什么也没有。

Try something like: 尝试类似:

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

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. 为了获得更好的解决方案,请从Swing教程“ 如何使用文本区域”中找到的工作示例开始,然后修改代码。 This way you will start with a better structured program that follows Swing standards. 这样,您将从遵循Swing标准的结构更好的程序开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM