繁体   English   中英

Java Swing可滚动框架

[英]Java Swing scrollable Frame

如何使用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);
}
panel.setLayout(null);

不要使用空布局。

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

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

阅读有关布局管理器的Swing教程中的部分,以获取更多信息和工作示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM