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

use*_*519 2 java applet swing 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); 
    this.setVisible(true); 
}
Run Code Online (Sandbox Code Playgroud)

Hov*_*els 6

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

这个:

frame.getContentPane().add(scrPane):
this.setVisible(true);   // this != frame
Run Code Online (Sandbox Code Playgroud)

因为您创建了一个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); 
}
Run Code Online (Sandbox Code Playgroud)