我正在尝试创建一个带有滚动条的容器,并且容器内部有两个内部面板。顶部内面板内还有另一个 JScrollPane。
但目前我面临的问题是,当我的顶部内面板太长(宽度)时,顶部内面板内的滚动条被禁用,我只能滚动容器的滚动条。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestFrame {
public static void main(String... args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
for (int i = 0; i < 10; i++) {
panel.add(new JButton("Hello-" + i));
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
JPanel contentPane = new JPanel(new BorderLayout());
JPanel contentPaneSub = new JPanel();
contentPaneSub.add(scrollPane);
contentPane.add(contentPaneSub, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(new JButton("Example"));
contentPane.add(centerPanel, BorderLayout.CENTER);
JScrollPane scrollPane1 = new JScrollPane(contentPane);
scrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
frame.setContentPane(scrollPane1);
//for demo purpose we set this using hard coded way
//in real life project the java will auto adjust it size based on windows resolution
frame.setSize(new Dimension(500, 160));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
我希望得到的是,如果顶部内面板的宽度太长,那么顶部内面板内的滚动条将可见并允许滚动。不是容器中的滚动条。
www说
相关分类