【问题标题】:Why doesn't my scrollpane have scrollbars?为什么我的滚动窗格没有滚动条?
【发布时间】:2014-02-25 01:58:56
【问题描述】:

由于某种原因,我无法在小程序中显示滚动窗格。

public void init() {
    JFrame frame = new JFrame(); 
    JPanel panel = new JPanel(); 
    JScrollPane scrPane = new JScrollPane(panel); 
    scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrPane.setLayout(new ScrollPaneLayout()); 
    frame.getContentPane().add(scrPane); 
    this.setVisible(true); 
}

【问题讨论】:

  • scrPane.setLayout(new ScrollPaneLayout()); - 不要使用布局。 JScrollPane 将自行设置布局。

标签: java swing applet jscrollpane


【解决方案1】:

您永远不会显示您创建的 JFrame!

这个:

frame.getContentPane().add(scrPane):
this.setVisible(true);   // this != frame

不工作,因为您创建了一个 JFrame 然后忽略它。

无论如何,您都不应该让小程序显示 JFrame。如果您需要显示一个单独的窗口,请考虑显示一个 JDialog。更好的是,为什么不简单地将 JScrollPane 放入小程序本身?

例如,

public void init() {
    //JFrame frame = new JFrame(); 

    JPanel panel = new JPanel(); 
    JScrollPane scrPane = new JScrollPane(panel); 
    scrPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    //  scrPane.setLayout(new ScrollPaneLayout()); 
    //  frame.getContentPane().add(scrPane); 

    getContentPane().add(scrPane);

    // this.setVisible(true); 
}

【讨论】:

  • 谢谢。就是这样。我没有意识到这不是框架。
猜你喜欢
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 2023-02-20
  • 1970-01-01
  • 2019-11-14
  • 2021-12-20
  • 2014-07-11
相关资源
最近更新 更多