gpt4 book ai didi

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

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

我遇到这样的问题:每当 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/

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