Java 我的JScrollPane没有滚动

Java 我的JScrollPane没有滚动,java,swing,jscrollpane,layout-manager,Java,Swing,Jscrollpane,Layout Manager,我在使用JScrollPane时遇到一些问题。当窗格限制超过时,它不会滚动。有什么建议吗 使用此代码,滚动条显示在左侧,但它不工作 package gui; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MainPanel extends JPanel implements Acti

我在使用
JScrollPane
时遇到一些问题。当窗格限制超过时,它不会滚动。有什么建议吗

使用此代码,滚动条显示在左侧,但它不工作

package gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MainPanel  extends JPanel implements ActionListener
{
   private JLabel welcome = new JLabel ("\n  Insert your search type: \n");
   private JPanel radioPanel = new JPanel (new GridLayout (0,4));
   private FieldPanel name = new FieldPanel ("Name: " , 50);
   private FieldPanel year = new FieldPanel ("Year: " , 3);
   private FieldPanel genre = new FieldPanel ("Genre: " , 10);
   private FieldPanel rating = new FieldPanel ("Users' rating (1-5): " , 2);
   private JButton search = new JButton ("Search");
   private JButton clear = new JButton ("Clear");
   private JScrollPane scroll;   
   private JFormattedTextField textField;

   public MainPanel () 
   {
       ButtonGroup RBGroup = new ButtonGroup ();

       // Cria radio buttons
       JRadioButton nameRB      = new JRadioButton ("Name");
       JRadioButton yearRB      = new JRadioButton ("Year");
       JRadioButton genreRB     = new JRadioButton ("Genre");
       JRadioButton ratingRB    = new JRadioButton ("Rating");

       // Seta comandos para os radio buttons
       nameRB.setActionCommand("nameOfMovie");
       yearRB.setActionCommand("dataReleaseOfMovie");
       genreRB.setActionCommand("genreOfMovie");
       ratingRB.setActionCommand("noteOfMovie");

       // Seta o nome como padrao inicial
       nameRB.setSelected(true);

       // Adicionar radio buttons em um grupo
       RBGroup.add (nameRB);
       RBGroup.add (yearRB);
       RBGroup.add (genreRB);
       RBGroup.add (ratingRB);

       // Adicionar action listeners ao radio buttons
       nameRB.addActionListener(this);
       yearRB.addActionListener(this);
       genreRB.addActionListener(this);
       ratingRB.addActionListener(this);

       radioPanel.add(nameRB);
       radioPanel.add(yearRB);
       radioPanel.add(genreRB);
       radioPanel.add(ratingRB);

       // Adicionar coisinhas
       add (welcome);
       add (radioPanel);
       add (name);
       add (year);
       add (genre);
       add (rating);
       add (search);
       add (clear);

       // Adicionar action listeners nos botoes
       search.addActionListener(this);
       clear.addActionListener(this);

       // Criar e adicionar painel para exibir informacoes do filme
       JPanel informations = new JPanel ();
       informations.setLayout (new BoxLayout(informations, BoxLayout.Y_AXIS));
       informations.setPreferredSize(new Dimension(767,461));

       scroll = new JScrollPane(informations);
       scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);  
       scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

       add (scroll);

       informations.add(new JLabel ("oi oi oi oi oi oio i\noioioioioi"));
       informations.add(new JLabel ("uhuuul"));
       informations.add(new JLabel ("uhuuul"));
       //repeats 100 times, just to test the scroll
       informations.add(new JLabel ("fim"));
       //setLayout (new FlowLayout (FlowLayout.LEFT));

   }


   public void actionPerformed (ActionEvent e)
   {
       Object obj = e.getSource ();

       if (obj == search)
       {
           // Pegar as coisas dos edit text pra salvar
       }
       else if (obj == clear)
       {
           name.setTextField ("");
           year.setTextField ("");
           genre.setTextField ("");
           rating.setTextField ("");
       }
   }
}

我添加了
信息。添加
只是为了测试
JScrollPane
您在添加到JScrollPane的JPanel视图中遇到了一个问题:

informations.setPreferredSize(new Dimension(767,461));
这限制了JPanel的大小,不管它的内容是什么。不要这样做。相反,当您向JPanel添加更多组件时,让它变得更大

如果需要设置任何对象的首选大小(kleo原谅我),则应该是JScrollPane或其视口:

scroll.getViewport().setPreferredSize(new Dimension(767, 461));
虽然我自己,我会考虑使用JLIST并设置它的VisualBuroCube:

  DefaultListModel<String> listModel = new DefaultListModel<>();
  JList<String> infoList = new JList<>(listModel);

  infoList.setVisibleRowCount(20);

  listModel.addElement("oi oi oi oi oi oio i\noioioioioi");
  listModel.addElement("uhuuul");
  listModel.addElement("uhuuul");
  for (int i = 0; i < 100; i++) {
     listModel.addElement("uhuuul");
  }
  listModel.addElement("fim");

  JScrollPane scrollPane = new JScrollPane(infoList);
  scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

  add(scrollPane);
DefaultListModel listModel=新的DefaultListModel();
JList infoList=新的JList(listModel);
infoList.setVisibleRowCount(20);
listModel.addElement(“oi oi oi oi oi oi i\NOOIOI”);
listModel.addElement(“UUUUUL”);
listModel.addElement(“UUUUUL”);
对于(int i=0;i<100;i++){
listModel.addElement(“UUUUUL”);
}
listModel.addElement(“fim”);
JScrollPane scrollPane=新的JScrollPane(信息列表);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u ALWAYS);
添加(滚动窗格);

@PatriciaSilva:很高兴问题解决了!感谢您发布了一个“几乎”可运行的小程序,尽管在将来,如果您对组件布局有疑问,您会希望删除所有ActionListener、ButtonGroup和其他无关的代码,而是发布一个小的可运行的可编译程序,我们可以不经修改地运行它。请注意,我必须创建自己的
FieldPanel
类,我猜该类应该包含一个JLabel和一个JTextField,第二个int参数用于textfield列大小。将来,创建一个没有依赖关系的小程序。明白你的意思了,气垫船。执行的操作确实没有必要。下次我会注意的。:-)您需要将该滚动窗格添加到Jpanel,这将限制Jpanel的查看。