如何在Java中使用JScrollPane [英] How to use the JScrollPane in Java

查看:118
本文介绍了如何在Java中使用JScrollPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在下面给出的代码中围绕JList组件获取滚动条?它似乎无法正常工作:(

How can I get the scroller around my JList component in the code given below? 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);


}

推荐答案

看,我可能没有回答您的需要,因为我不太记得挥杆布局.我很久以前不使用它了...

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...

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

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

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

  }

我认为问题是JPanel的默认layoutmaneger.由于其工作原理,您的滚动不够缩放"以创建滚动...

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

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

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

实际上:发布答案后,我看到了您的错误.现在我可以解释出什么问题了.您已经在这里的JScrollPane中添加了JList:

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 scroller = new JScrollPane(conList);

但是之后,您可以将其放入JPanel:

But after that you put it inside the JPanel:

panel.add(conList);

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

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

现在我想我帮助了= D

Now I think I helped =D

这篇关于如何在Java中使用JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆