我的JScrollPane不滚动

时间:2014-11-17 02:12:45

标签: java swing jscrollpane layout-manager

我在使用JScrollPane时遇到了一些问题。当窗格限制超出时,它不会滚动。有什么提示吗?

使用此代码,ScrollBar显示在左侧,但它不起作用...

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 ("");
       }
   }
}

我添加了informations.add仅用于测试JScrollPane

1 个答案:

答案 0 :(得分:3)

您在JPanel视图中遇到的问题是您添加到JScrollPane:

informations.setPreferredSize(new Dimension(767,461));

这限制了JPanel的大小,无论其内容是什么。不要这样做。相反,当你向它添加更多组件时,让JPanel变大。

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

scroll.getViewport().setPreferredSize(new Dimension(767, 461));

虽然我自己,我考虑使用JList并设置其visibleRowCount:

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