Java Swing可滚动框架

时间:2017-03-16 19:39:14

标签: java eclipse swing

如何使用Java Swing向框架添加滚动条?我在面板中有很多内容,需要使其可滚动才能达到全屏高度。

我尝试了以下代码,但它只是添加了一个滚动条而没有滚动内容的可能性

// add the panel to a JScrollPane
JScrollPane jScrollPane = new JScrollPane(panel);
// only a configuration to the jScrollPane...
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

// Then, add the jScrollPane to your frame
frame.getContentPane().add(jScrollPane);

这是我的代码:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 500, 665);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    panel.setLayout(null);

    JLabel lblPersonalInfo = new JLabel("Personal Information");
    lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
    lblPersonalInfo.setBounds(110, 11, 185, 14);
    panel.add(lblPersonalInfo);

    JLabel lblGender = new JLabel("Gender");
    lblGender.setBounds(10, 111, 46, 14);
    panel.add(lblGender);

    JLabel lblAddress = new JLabel("Address");
    lblAddress.setBounds(10, 164, 58, 14);
    panel.add(lblAddress);

    JLabel lblMobile = new JLabel("Mobile");
    lblMobile.setBounds(10, 189, 46, 14);
    panel.add(lblMobile);

    JLabel lblEmail = new JLabel("E-mail");
    lblEmail.setBounds(10, 214, 46, 14);
    panel.add(lblEmail);

    JRadioButton rdbtnM_2 = new JRadioButton("M");
    rdbtnM_2.setBounds(74, 133, 109, 23);
    panel.add(rdbtnM_2);

    JRadioButton rdbtnF = new JRadioButton("F");
    rdbtnF.setBounds(74, 107, 109, 23);
    panel.add(rdbtnF);

    JTextPane textName = new JTextPane();
    textName.setBounds(95, 36, 302, 20);
    panel.add(textName);

    JTextPane textNationality = new JTextPane();
    textNationality.setBounds(95, 61, 302, 20);
    panel.add(textNationality);

    JTextPane textDate = new JTextPane();
    textDate.setBounds(95, 86, 302, 20);
    panel.add(textDate);

    JTextPane textAddress = new JTextPane();
    textAddress.setBounds(95, 164, 302, 20);
    panel.add(textAddress);

    JLabel lblWebsiteblog = new JLabel("Website/Blog");
    lblWebsiteblog.setBounds(10, 244, 78, 23);
    panel.add(lblWebsiteblog);

    JTextPane textWebsite = new JTextPane();
    textWebsite.setBounds(95, 239, 302, 20);
    panel.add(textWebsite);

    JLabel lblProfesional = new JLabel("Profesional Experience");
    lblProfesional.setBounds(10, 267, 133, 23);
    panel.add(lblProfesional);


    JLabel lblEducationAndTraining = new JLabel("Education and Training");
    lblEducationAndTraining.setBounds(10, 441, 133, 14);
    panel.add(lblEducationAndTraining);

    JTextArea textProfesional = new JTextArea();
    textProfesional.setWrapStyleWord(true);
    textProfesional.setBounds(40, 301, 384, 129);
    panel.add(textProfesional);

    JScrollPane scrollPane = new JScrollPane(textProfesional);
    scrollPane.setBounds(40, 301, 384, 129);
    panel.add(scrollPane);

    JTextArea textEducation = new JTextArea();
    textEducation.setBounds(40, 466, 384, 142);
    panel.add(textEducation);

    scrollPane_1 = new JScrollPane(textEducation);
    scrollPane_1.setBounds(40, 466, 384, 142);
    panel.add(scrollPane_1);
}

1 个答案:

答案 0 :(得分:3)

panel.setLayout(null);

不要使用空布局。

仅当面板的首选大小大于滚动窗格的大小时,才会显示滚动条。使用空布局时,首选大小为(0,0),因此没有理由显示滚动条。

Swing旨在与布局管理器一起使用。使用布局管理器时,面板的首选大小将由布局管理器计算。

阅读Layout Managers上Swing教程中的部分,了解更多信息和工作示例。