【问题标题】:Java Swing scrollable FrameJava Swing 可滚动框架
【发布时间】:2017-03-17 03:40:15
【问题描述】:

如何使用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); 将阻止 JScrollPane 计算窗格的大小,这将阻止它确定何时应显示滚动条

标签: java eclipse swing


【解决方案1】:
panel.setLayout(null);

不要使用空布局。

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

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

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

【讨论】:

  • 另外,他可以直接设置首选大小panel.setPreferredSize(new Dimension(465, 624)); 并保持空布局,但我同意必须不鼓励。我也推荐 MigLayout miglayout.com
  • @YagoMéndezVidal,不,您不想手动设置首选尺寸!!!它只是一个随机数,当您添加/删除组件时应该会更改。让布局管理器计算值,这是布局管理器的工作之一。
  • 这个数字和代码中的所有其他 setBounds 一样随机,这就是为什么我告诉它必须不鼓励,我为他的案例推荐了一个给定的布局。
  • @YagoMéndezVidal, That number is as random as all the other setBounds in the code, 没错,这就是为什么我的答案是不使用空布局(因此也不使用 setBounds)。你们这些 cmets 没有为答案添加任何内容,并且与我的建议相反。请删除它们。
猜你喜欢
  • 1970-01-01
  • 2017-05-05
  • 2013-11-25
  • 2011-09-05
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 2016-06-21
  • 2015-07-13
相关资源
最近更新 更多