如何在 Java 中使用 JScrollPane

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9812980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:00:17  来源:igfitidea点击:

How to use the JScrollPane in Java

javaswingscrolljscrollpane

提问by helpdesk

How can I get the scroller around my JList component in the code given below? It doesn't seem to work properly :(

如何在下面给出的代码中获得 JList 组件周围的滚动条?它似乎不能正常工作:(

 public class JButtonO extends JFrame{

String[] values = {"henry", "Michael","Uche","John","Ullan","Nelly",
                              "Ime","Lekan","Austine","jussi","Ossi","Imam","Empo","Austine","Becky",
                             "Scholar","Ruth", "Anny"};

 public JButtonO()
{
   super("the button");
   this.setSize(400,200);
   JPanel panel =  new JPanel();
   JLabel label = new JLabel("Output Items:");
   label.setAlignmentX(1);
   label.setAlignmentY(1);
   JList conList = new JList(values);
   conList.setVisibleRowCount(3);
   JScrollPane scroller = new JScrollPane(conList);
   panel.add(label);
   panel.add(scroller);
   panel.add(conList);

   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   this.add(panel);
   this.setVisible(true);


}

采纳答案by Rodrigo Leit?o

Look, I may not answering what you need, because I don′t remember to much of swing layout. I don′t work with it a long time ago...

看,我可能不会回答你的需要,因为我不记得太多的秋千布局。我很久以前不使用它...

But removing setting a layout (I remember) on your JPanel it works with this code:

但是删除在您的 JPanel 上设置布局(我记得)它可以使用以下代码:

public JButtonO() {
    super("the button");
    this.setSize(400, 200);

    // Create a panel with a borderlayout
    JPanel jpanel = new JPanel(new BorderLayout());

    JLabel label = new JLabel("Output Items:");
    label.setAlignmentX(1);
    label.setAlignmentY(1);
    // Add Label to top of layout
    jpanel.add(label, BorderLayout.NORTH);

    JList conList = new JList(values);
    conList.setVisibleRowCount(3);
    JScrollPane scroller = new JScrollPane(conList);
    //AddScroll to center
    jpanel.add(scroller);

    //Add Panel to JFrame
    this.add(jpanel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

  }

I think the problems is the default layoutmaneger of JPanel. Because of how it works your scroll was not "srink" enough to create scrolls...

我认为问题是 JPanel 的默认布局管理器。由于它的工作原理,您的卷轴不足以“收缩”以创建卷轴......

Hope it helps, even without too much explanation...

希望它有所帮助,即使没有太多解释......

ACTUALLY: After I post the answer I saw your mistake. Now I can explain what is wrong. You already added your JList inside your JScrollPane here:

实际上:在我发布答案后,我看到了您的错误。现在我可以解释什么是错的。您已经在 J​​ScrollPane 中添加了 JList:

JScrollPane scroller = new JScrollPane(conList);

But after that you put it inside the JPanel:

但之后你把它放在 JPanel 中:

panel.add(conList);

this changes where yout JList will be displayed, and let the JScroll empty again. Without components it will be displayed with size 0x0 and will not be draw (even being there).

这会更改 JList 的显示位置,并让 JScroll 再次清空。如果没有组件,它将以 0x0 大小显示并且不会被绘制(即使在那里)。

Now I think I helped =D

现在我想我帮助了 =D

回答by Costis Aivalis

Adding the JScrollPane scrollerthat includes the JList conListto the JPanel panelis enough. The mistake is that you are adding the JList a second time.

scroller包含 JList的 JScrollPane 添加conList到 JPanelpanel就足够了。错误是您第二次添加 JList。

   JScrollPane scroller = new JScrollPane(conList);
   panel.add(label);
   panel.add(scroller);
   panel.add(conList); // <---THIS LINE SHOULD BE DELETED...

回答by Micha? Kosmulski

The JScrollPanehas settings called the scrollbar policies which say when the scrollbars are to be displayed. You can set them using JScrolPane(Component,int,int)constructor, or by calling setVerticalScrollBarPolicy()and setHorizontalScrollBarPolicy(). The default policies are "as needed", meaning the scrollbar is only displayed if the component is too large to display whole. So if your list fits inside the window, the scrolbars will not be visible, but will become visible when you e.g. make the window smaller using the mouse. You can change one or both policies to "always" using corresponding constants in order to make the scrollbar(s) always visible if that's what you need.

JScrollPane具有设置所谓的滚动条政策,说时滚动条显示。您可以使用JScrolPane(Component,int,int)构造函数或通过调用setVerticalScrollBarPolicy()and来设置它们setHorizontalScrollBarPolicy()。默认策略是“根据需要”,这意味着只有在组件太大而无法显示整体时才会显示滚动条。因此,如果您的列表适合窗口,滚动条将不可见,但当您使用鼠标将窗口缩小时,滚动条就会变得可见。您可以使用相应的常量将一个或两个策略更改为“始终”,以便在需要时使滚动条始终可见。