我的JFrame有一个中央面板,它的宽度和高度都比屏幕尺寸大,我希望这个中央面板显示在JScrollPane里面,现在问题出现在没有出现的垂直滚动条中。 JScrollPane只显示其水平位置不垂直。以下是我的JFrame的代码。JScrollPane中的JPanel:没有出现竖线
import java.awt.*;
import javax.swing.*;
public class TestScroll extends JFrame {
public TestScroll() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(screenSize.width,screenSize.height);
getContentPane().setLayout(new BorderLayout());
JPanel centerPanel=new JPanel(new BorderLayout());
centerPanel.setSize(screenSize.width+50,screenSize.height+50);//I want centralPanel to be of more width and height so to test JScollPane
JPanel northPanel = new JPanel();
Dimension d1=centerPanel.getSize();
northPanel.setPreferredSize(new Dimension(d1.width,d1.height/3));
northPanel.setBackground(Color.BLACK);
centerPanel.add(northPanel, BorderLayout.NORTH);
JPanel innerPanel = new JPanel();
//Dimension d1=centerPanel.getSize();
//panel2.setPreferredSize(new Dimension(d1.width,d1.height/2));
innerPanel.setBackground(Color.ORANGE);
centerPanel.add(innerPanel, BorderLayout.CENTER);
JScrollPane pane=new JScrollPane(centerPanel);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(pane, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args) {
new TestScroll();
}
}
请帮我显示垂直滚动条..
可能[重复](http://stackoverflow.com/q/9092198/230513)。 – trashgod