gpt4 book ai didi

java - 我们怎样才能拥有一个包含的 GridBagLayout,具有 PreferredSize,并且仅具有垂直滚动?

转载 作者:行者123 更新时间:2023-12-01 09:47:03 24 4
gpt4 key购买 nike

有谁知道如何使用 Gridbaglayout 将 ScrollBar 添加到 JPanel,从而允许 Gridbaglayout 仍然在水平轴上重新定位组件。我尝试添加滚动条 - 但是当我执行 Gridbaglayout 时,水平方向上的组件重新定位会停止工作,并且当我减小窗口宽度时,组件会离开屏幕一侧。我们如何包含垂直滚动,同时允许 Gridbaglayout 仍然在水平轴上重新定位组件。如有帮助,我们将不胜感激。

GridBagLayout 仍应移动组件。

代码:

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

public class SwingTest {



public static void main(String[] args) {

final JScrollPane scrollPane = new JScrollPane(addCustomer());


JFrame frame = new JFrame("Swing Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(640, 480);
frame.setMinimumSize(new Dimension(300, 480));
frame.setLocation(200, 200);
frame.getContentPane().add(scrollPane);

frame.setVisible(true);
}

public static Component addCustomer(){
JTextField textField1;
/***********************Initialize Fields******************************/

JLabel labels[] = new JLabel[2];

labels[0] = new JLabel("Left side - more components to add here");
labels[1] = new JLabel("Right side - more components to add here");

textField1 = new JTextField(10);
JComboBox<String> materialTitlesCombo = new JComboBox<String>();

materialTitlesCombo.addItem("-");
materialTitlesCombo.addItem("test");
materialTitlesCombo.setPreferredSize(new Dimension(250, 500));
materialTitlesCombo.setMinimumSize(materialTitlesCombo.getPreferredSize());
/****************************Create the canvas**************************/
GridBagConstraints constraints;

constraints = new GridBagConstraints();


final JPanel panel = new JPanel(new GridBagLayout());

constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridheight = 1;
constraints.gridwidth = 1;
constraints.weightx = 0.5;
constraints.weighty = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(-20,20,0,0);
panel.add(labels[0], constraints); //Left side

constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridheight = 1;
constraints.gridwidth = 1;
constraints.weightx = 0.5;
constraints.weighty = 1;
constraints.anchor = GridBagConstraints.CENTER;
constraints.insets = new Insets(-10,-9,0,9);
panel.add(textField1, constraints); // TextField 1

constraints.gridx = 8;
constraints.gridy = 3;
constraints.gridheight = 1;
constraints.gridwidth = 1;
constraints.weightx = 0.5;
constraints.weighty = 1;
constraints.anchor = GridBagConstraints.WEST;
constraints.insets = new Insets(-15,15,0,0);
panel.add(labels[1], constraints);//Right side

return panel;
}
}

最佳答案

要将可滚动的 JPanel 添加为选项卡,您可以执行以下操作:

JScrollPane scrollPane = new JScrollPane(addCustomer());
pane.addTab("...", scrollPane);

我注意到您从未将 JTabbedPane 组件添加到 CustomerPane 中,这意味着选项卡式 Pane 永远不会在 UI 中呈现。您可以尝试以下方法:

private void initializeUI() {
...
add(pane);
}

关于java - 我们怎样才能拥有一个包含的 GridBagLayout,具有 PreferredSize,并且仅具有垂直滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37908484/

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