简体   繁体   English

我的JScrollPane不滚动

[英]My JScrollPane is not scrolling

I'm having some trouble with a JScrollPane . 我在使用JScrollPane遇到了一些麻烦。 When the pane limits exceeds, it does not scroll. 当窗格限制超出时,它不会滚动。 Any tips? 有小费吗?

With this code, the ScrollBar is shown on the left, but it doesn't work... 使用此代码,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 ("");
       }
   }
}

I added the informations.add just for testing the JScrollPane . 我添加了informations.add只是为了测试JScrollPane

You have a problem here in the JPanel view that you add to your JScrollPane: 您在添加到JScrollPane的JPanel视图中遇到问题:

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

This limits the size of the JPanel, no matter what its contents are. 这限制了JPanel的大小,无论其内容是什么。 Don't do this. 不要这样做。 Instead, let the JPanel get larger as you add more components to it. 相反,当您向其添加更多组件时,让JPanel变大。

If you need to set the preferred size of anything (kleo forgive me), it should be the JScrollPane or its viewport: 如果你需要设置任何东西的首选大小(kleo原谅我),它应该是JScrollPane或其视口:

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

Though myself, I'd consider using a JList and setting its visibleRowCount: 虽然我自己,但我会考虑使用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);

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

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