gpt4 book ai didi

Java Swing 可滚动框架

转载 作者:行者123 更新时间:2023-12-01 20:53:09 25 4
gpt4 key购买 nike

如何使用 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);

不要使用空布局。

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

Swing 被设计为与布局管理器一起使用。当您使用布局管理器时,面板的首选大小将由布局管理器计算。

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

关于Java Swing 可滚动框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42843393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com