有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带约束的java传递面板

如何添加jpanel并在jtabbedpane选项卡中传递一些约束

addTab("name",null,here to pass panel but can't pass constraints, "tooltip");

更多详细说明

在选项卡中添加jpanel时,面板中的内容会延伸到整个框架,因此我决定将框架设置为GridBagLayout,但在添加选项卡时,通常必须将面板作为参数之一传递,不允许任何约束!我的问题是,在选项卡中添加面板时,如何添加约束,以准确告知框架放置面板的位置


共 (1) 个答案

  1. # 1 楼答案

    简单的回答是,您不能,至少不能使用addTab方法的那个版本。使用特定的addTab方法添加到JTabbedPane的任何内容都将在选中其关联的选项卡时获得所有可用的屏幕空间

    在我看来,您似乎想将多个Component放在同一个选项卡中,或者更改该选项卡中某个组件的大小调整行为。如果是这种情况,您应该首先使用panel.add(Component component,Object constraints)方法将任何要可视化的组件添加到JPanel的实例中,然后将该面板添加到JTabbedPane

    另一种选择是这样做:

    JTabbedPane tabs = ...
    
    int index = ... //whatever index you're currently at
    Object constraints = ...//these are your constraints
    JPanel panel = ...//this is the panel you want constraints for
    panel.setName("name"); //will be used as the title when added to tabs in next line
    tabs.add(panel, constraints, index);
    tabs.setToolTipTextAt(index, "this is a tooltip for 'panel'");
    

    当然没有一艘客轮那么吸引人,但仍然完成了任务