Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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 在JPanel中滚动不起作用_Java_Swing_Jpanel_Jscrollpane - Fatal编程技术网

Java 在JPanel中滚动不起作用

Java 在JPanel中滚动不起作用,java,swing,jpanel,jscrollpane,Java,Swing,Jpanel,Jscrollpane,我有一个JFrame,我在其中添加了两个带有JScrollPane的JPanel,一个是JFrame的一半。 然后,我将向这两个JPanel添加一个JPanel编号(每个编号在flowlayout中包含两个JLabel)。 到目前为止,一切都很好,但当JPanel的数量增加时,JPanel的大小减小,而滚动不起作用。 你能帮我找出为什么滚动不起作用吗 下面是一些只包含一个JPanel的代码 class scrol extends JFrame { scrol() { /

我有一个JFrame,我在其中添加了两个带有JScrollPane的JPanel,一个是JFrame的一半。 然后,我将向这两个JPanel添加一个JPanel编号(每个编号在flowlayout中包含两个JLabel)。
到目前为止,一切都很好,但当JPanel的数量增加时,JPanel的大小减小,而滚动不起作用。 你能帮我找出为什么滚动不起作用吗

下面是一些只包含一个JPanel的代码

class scrol extends JFrame
 {

  scrol()
   {

     //getContentPane().setLayout(new GridLayout(1,2));
     JPanel p1=new JPanel();
     p1.setLayout(new GridLayout(0,1));
     p1.setPreferredSize(new Dimension(300,350));
     int n=1;
     p1.setBorder(BorderFactory.createLineBorder(new Color(200,167,0)));
     JScrollPane scrol1=new JScrollPane(p1);
     scrol1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//scrol1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
     scrol1.setBounds(0,0,420,405);
     getContentPane().add(scrol1,BorderLayout.CENTER);
     while(n<100)
      {
       n=n+1;
       p1.add(new JPanel().add(new JLabel("hello")));
     }
     pack();

     setSize(500,500);
     setVisible(true);
  }
类scrol扩展JFrame
{
阴囊
{
//getContentPane().setLayout(新的GridLayout(1,2));
JPanel p1=新的JPanel();
p1.设置布局(新网格布局(0,1));
p1.设置首选尺寸(新尺寸(300350));
int n=1;
p1.setboorder(BorderFactory.createLineBorder(新颜色(200167,0));
JScrollPane scrol1=新的JScrollPane(p1);
scrol1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL\u SCROLLBAR\u ALWAYS);
//scrol1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HorizontalScrollBar\uAlways);
1.立根(0,0420405);
getContentPane().add(scrol1,BorderLayout.CENTER);

而(n工作代码
您需要删除下面的行

 p1.setPreferredSize(new Dimension(300,350));
您可以在while循环中添加下面的代码来控制大小,尽管没有这些代码也可以工作

  tempP.setPreferredSize(new Dimension(10, 30));
下面是修改后的工作代码

class Scrol extends JFrame
{

Scrol()
{

 //getContentPane().setLayout(new GridLayout(1,2));
 JPanel p1=new JPanel();
 p1.setLayout(new GridLayout(0,1));
// p1.setPreferredSize(new Dimension(300,350));
 int n=1;
 p1.setBorder(BorderFactory.createLineBorder(new Color(200,167,0)));
 JScrollPane scrol1=new JScrollPane(p1);
 scrol1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
 //scrol1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
 //scrol1.setBounds(0,0,420,405);
 getContentPane().add(scrol1,BorderLayout.CENTER);
 while(n<200)
  {
   n=n+1;
   JPanel tempP=new JPanel();
   tempP.setPreferredSize(new Dimension(10, 30));
   p1.add(tempP.add(new JLabel("hello")));
 }
 pack();

 setSize(500,500);
 setVisible(true);
}

 public static void main(String[] args) {
new Scrol();
 }
 }
类Scrol扩展JFrame
{
阴囊
{
//getContentPane().setLayout(新的GridLayout(1,2));
JPanel p1=新的JPanel();
p1.设置布局(新网格布局(0,1));
//p1.设置首选尺寸(新尺寸(300350));
int n=1;
p1.setboorder(BorderFactory.createLineBorder(新颜色(200167,0));
JScrollPane scrol1=新的JScrollPane(p1);
scrol1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL\u SCROLLBAR\u ALWAYS);
//scrol1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HorizontalScrollBar\uAlways);
//1.立根(0,0420405);
getContentPane().add(scrol1,BorderLayout.CENTER);

而(n由于您只表示希望在此处进行滚动,因此:

public class Scrol extends JFrame {

    Scrol() {

        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(0, 1));
        p1.setBorder(BorderFactory.createLineBorder(new Color(200, 167, 0)));
        getContentPane().add(new JScrollPane(p1), BorderLayout.CENTER);

        for (int n = 1; n < 100; n++)
            p1.add(new JPanel().add(new JLabel("hello")));

        setSize(500, 500); // or pack()
        setVisible(true);
    }

    public static void main(String[] args) {

        new Scrol();
    }
}
public类Scrol扩展JFrame{
阴囊{
JPanel p1=新的JPanel();
p1.设置布局(新网格布局(0,1));
p1.setboorder(BorderFactory.createLineBorder(新颜色(2001670));
getContentPane().add(新的JScrollPane(p1),BorderLayout.CENTER);
对于(int n=1;n<100;n++)
p1.add(newjpanel().add(newjlabel(“hello”));
setSize(500500);//或pack()
setVisible(真);
}
公共静态void main(字符串[]args){
新阴囊();
}
}
注意事项:

  • 通常你不想弄乱组件的尺寸,你有一个布局管理器。我删除了所有的尺寸规格,因为你没有指定任何约束,它们通常会导致意外的结果
  • 类名应以大写字母开头
  • <>你的代码是一个混乱,在逻辑上分类(<代码> int n=1 < /COD>在设置组件的中间)?< /LI>
您能至少发布一些代码吗?为了更快地获得更好的帮助,请发布一个(最小完整的可验证示例)。
setVisible(true);
应该最后调用,并且在
pack();
p1.setPreferredSize(新维度(300350));
请参阅(是)。
请参阅谢谢andrew,但我的问题仍然没有解决。在
pack
之后调用
setSize
会使
pack
变得多余。非常感谢,我可以对大小限制执行相同的操作吗,因为我的应用程序无法正常工作,但滚动是可以的now@Ambuj你可以,但为此你需要问一个新问题和确切的要求不起作用的内容和细节,以及你希望它如何起作用。你需要在那里发布相关代码。