Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的JScrollPane_Java_Swing_User Interface_Scrollbar_Jscrollpane - Fatal编程技术网

Java中的JScrollPane

Java中的JScrollPane,java,swing,user-interface,scrollbar,jscrollpane,Java,Swing,User Interface,Scrollbar,Jscrollpane,我写了一个小代码来看看滚动窗格是如何工作的,但是我的代码从来没有工作过。 这是密码 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; JPane

我写了一个小代码来看看滚动窗格是如何工作的,但是我的代码从来没有工作过。 这是密码

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);
}
当我运行它时,我得到一个带有文本区域的小窗口,文本区域旁边有一个非常小的白色正方形,我想这是滚动窗格,因为当我从代码中删除它时,这个正方形消失了。当我在文本区域中写入内容并超出窗口的尺寸时,我无法使用鼠标滚轮垂直滚动,也无法水平滚动。我在网上看到了很多例子,我不明白为什么我的代码不起作用?? 有关于scrollpane工作原理的帮助吗

scrollPane.setViewportView(textArea);
scrollPane.setViewportView(imad);
只能将一个组件添加到滚动窗格的视口中,因此标签将替换文本区域

content.add(textArea);
content.add(imad);
组件只能有一个父级。上面的代码从滚动窗格中删除标签,因此滚动窗格中现在没有任何内容

尝试以下方法:

JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
为了获得更好的解决方案,请从上的Swing教程中的工作示例开始,然后修改代码。通过这种方式,您将从遵循Swing标准的更好的结构化程序开始

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

content.add(textArea);
content.add(imad);
组件只能有一个父级。上面的代码从滚动窗格中删除标签,因此滚动窗格中现在没有任何内容

尝试以下方法:

JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
为了获得更好的解决方案,请从上的Swing教程中的工作示例开始,然后修改代码。通过这种方式,您将从遵循Swing标准的更好的结构化程序开始

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

content.add(textArea);
content.add(imad);
组件只能有一个父级。上面的代码从滚动窗格中删除标签,因此滚动窗格中现在没有任何内容

尝试以下方法:

JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
为了获得更好的解决方案,请从上的Swing教程中的工作示例开始,然后修改代码。通过这种方式,您将从遵循Swing标准的更好的结构化程序开始

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

content.add(textArea);
content.add(imad);
组件只能有一个父级。上面的代码从滚动窗格中删除标签,因此滚动窗格中现在没有任何内容

尝试以下方法:

JScrollPane = new JScrollPane( textArea );
JPanel content = new JPanel( new BorderLayout() );
content.add(scrollPane, BorderLayout.CENTER);
content.add(imad, BorderLayout.PAGE_END);
setContentPane( content );
为了获得更好的解决方案,请从上的Swing教程中的工作示例开始,然后修改代码。通过这种方式,您将从遵循Swing标准的更好的结构化程序开始