java - 如何让 JPanel 使用滚动条来适应大型 J 组件?

标签 java swing

我遇到这样的问题:每当 GUI 组件太大而无法放入面板时,JPanel (contentPane) 就会将 (testLabel) GUI 组件的大小调整得非常小。我已向 JPanel 添加了滚动条,但组件仍会重新调整大小,而不是使用滚动条。这是我的类(class),我使用带有滚动条的 JPanel。

package marsPackage;

import java.awt.*;
import javax.swing.*;

public class DashTab{

    private JLabel testLabel; //test label
    private JLabel testLabel2;
    private JLabel testLabel3;
    private JLabel testLabel4;
    private JPanel dashPanel; //Panel that holds the JTabbedPane tab
    private JPanel contentPane; // Panel that holds all GUI components
    private JScrollPane scrollPane; // Scrollpane used on contentPane

    /*
     * Constructor
     * All of your GUI components should be added to
     * contentPane using the gridBagLayout.
     */

    public DashTab(){
    //Creating the dashpanel that holds everything
    dashPanel = new JPanel();
    dashPanel.setBackground(Color.WHITE);

    //Creating the contentPane that holds all GUI components and
    //uses vertical/horizontal sidebards as needed
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);

    //Giving the contentPane the GridBagLayout
    contentPane.setLayout(new GridBagLayout());
    GridBagConstraints g = new GridBagConstraints();
    contentPane.setPreferredSize(new Dimension(857, 725));

    //Adding scrollPane to Content Pane and adding those two to dashPanel
    scrollPane = new JScrollPane(contentPane);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setBackground(Color.WHITE);
    dashPanel.add(scrollPane);

    /*
     * You may begin adding your GUI components from this point forward.
     * Remember to only use GridBagLayout with GridBagConstraints using the
     * g variable.
     */

    testLabel = new JLabel("Testing Here 1");
    testLabel.setPreferredSize(new Dimension(500, 500));
    testLabel.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
    g.gridx = 0;
    g.gridy = 0;
    contentPane.add(testLabel, g);

    testLabel2 = new JLabel("Testing Here 2");
    testLabel2.setPreferredSize(new Dimension(250, 200));
    testLabel2.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
    g.gridx = 1;
    g.gridy = 0;
    contentPane.add(testLabel2, g);

    testLabel3 = new JLabel("Testing Here 3");
    testLabel3.setPreferredSize(new Dimension(200, 200)); 
    testLabel3.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
    g.gridx = 1;
    g.gridy = 1;
    contentPane.add(testLabel3, g);

    testLabel4 = new JLabel("Testing Here 4");
    testLabel4.setPreferredSize(new Dimension(200, 200)); 
    testLabel4.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.gray));
    g.gridx = 2;
    g.gridy = 2;
    contentPane.add(testLabel4, g);
    }

    /**
     * The getDashTab method returns a DashTab object.
     * @return a DashTab panel object.
     */

    public JPanel getDashTab(){
            return dashPanel;
    }
}

上面的代码是这样的: http://i.imgur.com/lBSTfrt.jpg

每当我删除 contentPane.setPreferredSize(new Dimension(857, 725)); 面板就会拉伸(stretch)并完全忽略滚动条,使其看起来像:

http://i.imgur.com/sMusbTm.jpg

最佳答案

contentPane.setPreferredSize(new Dimension(857, 725));

不要使用setPreferredSize()方法来设置大小。这是布局管理器的工作,在本例中是 GridBagLayout 来确定面板的大小。

当面板的首选尺寸大于滚动 Pane 的尺寸时,滚动条将自动出现。

GUI components to be really small whenever they are too big to fit in the panel.

GridBagLayout 将尝试尊重组件的首选大小。如果首选尺寸大于面板的尺寸,则将使用组件“最小尺寸”。对于 JLabel,最小尺寸是完全显示文本所需的空间。

再次强调,不要尝试使用 setPreferedSize() 方法。

关于java - 如何让 JPanel 使用滚动条来适应大型 J 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24458966/

相关文章:

java - 排除在休息响应中发送的java bean中的一些字段 - Spring Boot

java - 主要听众和图形不同意

java - 如何向我的 fillRect 添加长文本?

Java Swing 颜色选择器抛出错误

java - 在 Spring 中,如何创建具有配置默认值的 POJO?

java - 有没有办法更新 key 以包含祖先

java - 将多个字符串散列为一个哈希

java - 缺少 Artifact com.fasterxml.jackson.core :jackson-core:jar:1. 9.11

java - 将面板添加到主机架

java - JTabbedPane:更改选项卡标题时更改选项卡大小