为什么我的滚动条没有滚动条?

时间:2014-02-25 01:58:56

标签: java swing applet jscrollpane

出于某种原因,我无法将滚动窗格显示在applet中。

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

1 个答案:

答案 0 :(得分:6)

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

此:

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

无效,因为您创建了一个JFrame然后忽略它。

你不应该让applet显示JFrame。如果需要显示单独的窗口,请考虑显示JDialog。更好的是,为什么不简单地将JScrollPane放在applet本身?

如,

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); 
}
相关问题