Java 调整JScrollPane和JTabbedPane的大小时出现问题
我在调整java应用程序中某些组件的大小时遇到问题。当我调整主JFrame的大小时,它们保持原来的大小,而不是填充周围的组件 例如,我的程序中有一个JTabbedPane,它位于另一个JTabbedPane(位于主JFrame内)的选项卡中。我通过各种.setPreferredSize()使添加到内部JTabbedPane的选项卡填充整个周围空间。然后,当我通过拖动窗口的角来调整JFrame的大小时,外部JTabbedPane中的选项卡调整得很好,但是位于其中的JTabbedPane保持原来的大小 当我用JScrollPane测试它时,也会发生同样的事情:外部JTabbedPane会根据JScrollPane调整大小,保持相同的大小 下面是创建选项卡式面板的代码,以防您看到我明显遗漏的内容。它是createEndGamePane()中的tabPane/jsp对象,在调整JFrame的大小时不会调整大小。mainTabPane会根据需要调整大小Java 调整JScrollPane和JTabbedPane的大小时出现问题,java,resize,jscrollpane,jtabbedpane,Java,Resize,Jscrollpane,Jtabbedpane,我在调整java应用程序中某些组件的大小时遇到问题。当我调整主JFrame的大小时,它们保持原来的大小,而不是填充周围的组件 例如,我的程序中有一个JTabbedPane,它位于另一个JTabbedPane(位于主JFrame内)的选项卡中。我通过各种.setPreferredSize()使添加到内部JTabbedPane的选项卡填充整个周围空间。然后,当我通过拖动窗口的角来调整JFrame的大小时,外部JTabbedPane中的选项卡调整得很好,但是位于其中的JTabbedPane保持原来的大
class MyFrame extends JFrame {
private JTabbedPane mainTabPane;
private JPanel endGameInfo;
//....
private void createTabbedCenterPane(){
this.mainTabPane = new JTabbedPane();
this.endGameInfo = new JPanel();
this.createEndGamePane();
this.mainTabPane.addTab("Turneringschema", null, this.scheduleHolder, "Spelschemat för turneringen");
this.mainTabPane.addTab("Grupper & Pooler", null, this.poolInfo, "Information om grupper, lag och pooler");
this.mainTabPane.addTab("Slutspelsträd", null, this.endGameInfo, "Information om slutspelen");
this.add(this.mainTabPane, BorderLayout.CENTER);
}
private void createEndGamePane(){
JTabbedPane tabPane = new JTabbedPane();
tabPane.setPreferredSize(this.endGameInfo.getSize());
for (int i = 0; i < this.tournament.data.getGroups().size(); i++) {
EndGame e = this.tournament.data.getEndGames().get(i);;
//Create the gameTree
EndGameTreeCanvas gameTree = new EndGameTreeCanvas(e, this.endGameInfo.getSize());
//Create the ScrollPane
JScrollPane jsp = new JScrollPane(gameTree);
jsp.setPreferredSize(tabPane.getSize());
jsp.setBorder(BorderFactory.createEmptyBorder());
//Add to the endGameIndo panel
tabPane.addTab(this.tournament.data.getGroups().get(i).getName(), jsp);
}
this.endGameInfo.add(tabPane);
}
}
类MyFrame扩展了JFrame{
专用JTabbedPane主选项卡窗格;
私人JPanel endGameInfo;
//....
私有void createTabbedCenterPane(){
this.mainTabPane=新的JTabbedPane();
this.endGameInfo=new JPanel();
这个.createEndGamePane();
this.mainTabPane.addTab(“Turneringschema”,null,this.scheduleHolder,“Spelschemat för Turnerigen”);
this.mainTabPane.addTab(“Grupper&Pooler”,null,this.poolfo,“Grupper信息,lag och Pooler”);
this.mainTabPane.addTab(“Slutspelsträd”,null,this.endGameInfo,“关于slutspelen的信息”);
this.add(this.mainTabPane,BorderLayout.CENTER);
}
私有void createEndGamePane(){
JTabbedPane tabPane=新的JTabbedPane();
tabPane.setPreferredSize(this.endGameInfo.getSize());
对于(int i=0;i
如果希望组件动态调整大小,应避免告诉Swing它们的大小。相反,这个怎么样:
JTabbedPane tabs = new JTabbedPane();
JScrollPane inner1 = new JScrollPane(myOtherComponent);
tabs.add("Scroll", inner1);
JTabbedPane inner2 = new JTabbedPane();
tabs.add("Tabs", inner2);
就这些。否
setPreferredSize()
您应该阅读swing布局教程:
我喜欢的布局管理器是gridbaglayout,它允许最大的灵活性
()
特别是,如果希望组件填充其周围的所有可用空间,则不应使用BorderLayout.CENTER
gridbaglayout管理器示例:
this.add(this.mainTabPane,
new GridBagConstraints(
0,0, // position
1,1, // size
1.0,1.0, // fill ratio
GridBagConstraints.CENTER, GridBagConstraints.BOTH, // position inside the cell
new Insets(2,2,2,2),0,0)); // margins
正如其他人所说,您需要使用来处理GUI的布局需求 但是,我建议您的主布局管理器是基于表的布局,如、或其他任意一种免费布局 使用基于表的布局将使GUI编码简化大约一个数量级,因为它将大大减少所需的布局嵌套量。我发现90%的GUI使用一个布局管理器,其余10%使用Java提供的更简单的布局管理器
我从来没有理由使用GridBagLayout,我绝对不推荐它-它不必要地复杂,使日常维护更加困难。BorderLayout.CENTER完全用于填充空间。