简体   繁体   English

如何在Java中使用JScrollPane

[英]How to use the JScrollPane in Java

How can I get the scroller around my JList component in the code given below? 如何在下面给出的代码中围绕JList组件获取滚动条? It doesn't seem to work properly :( 它似乎无法正常工作:(

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


}

Adding the JScrollPane scroller that includes the JList conList to the JPanel panel is enough. 将包含JList conList的JScrollPane scroller添加到JPanel panel就足够了。 The mistake is that you are adding the JList a second time. 错误是您正在第二次添加JList。

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

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. 我认为问题是JPanel的默认layoutmaneger。 Because of how it works your scroll was not "srink" enough to create scrolls... 由于其工作原理,您的滚动不够“缩放”以创建滚动...

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: 您已经在这里的JScrollPane中添加了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. 这将更改JList的显示位置,并再次使JScroll为空。 Without components it will be displayed with size 0x0 and will not be draw (even being there). 如果没有组件,它将以0x0大小显示,并且不会被绘制(即使在那里)。

Now I think I helped =D 现在我想我帮助了= D

The JScrollPane has settings called the scrollbar policies which say when the scrollbars are to be displayed. JScrollPane具有称为滚动条策略的设置,该设置表示何时显示滚动条。 You can set them using JScrolPane(Component,int,int) constructor, or by calling setVerticalScrollBarPolicy() and setHorizontalScrollBarPolicy() . 您可以使用JScrolPane(Component,int,int)构造函数或通过调用setVerticalScrollBarPolicy()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 eg 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. 您可以使用相应的常量将一个或两个策略更改为“始终”,以便在需要时使滚动条始终可见。

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

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