Java 选项卡窗格更改面板内部选项卡
我正在一个名为Dashboard的类中创建一个选项卡,该类在选项卡中包含“填充”面板。我想知道是否有办法创建一个新的仪表板并更改我存储在选项卡中的面板。我不确定这是否是正确的解释方式,但这里有一些代码Java 选项卡窗格更改面板内部选项卡,java,swing,user-interface,layout-manager,jtabbedpane,Java,Swing,User Interface,Layout Manager,Jtabbedpane,我正在一个名为Dashboard的类中创建一个选项卡,该类在选项卡中包含“填充”面板。我想知道是否有办法创建一个新的仪表板并更改我存储在选项卡中的面板。我不确定这是否是正确的解释方式,但这里有一些代码 public class Dashboard{ public Dashboard(){ tabPane = new JTabbedPane(); panel1 = new JPanel(); panel2 = new JPanel();
public class Dashboard{
public Dashboard(){
tabPane = new JTabbedPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel1.add(new JLabel("This is the first panel"));
panel2.add(new JLabel("This is the second panel"));
panel3.add(new JLable("This is the third panel"));
tabPane.add("One", panel1);
tabPane.add("Two", panel2);
tabPane.add("Three", panel3);
}
现在我想创建一个新类来创建仪表板的实例,但要更改选项卡中显示的面板。我试着这样做:
public class Changer{
public Changer(){
Dashboard d = new Dashboard();
// assuming I have getters and setters in the above class and that the
// panels are fields in Dashboard
JPanel new = new JPanel();
d.setPanel1(new);
}
}
我不确定这是否可行,或者是否有其他方法可以做到。获取选项卡的索引
int index = tabPane.indexOfTab("One");
在指定索引处设置组件
tabPane.setComponentAt(index, new Dashboard());
CardLayout
如中所示。@andreWhompson我可以将面板添加到CardLayout面板吗?因为我需要在两个面板之间切换的面板上都有多个组件,当然是“我可以将面板添加到CardLayout面板吗?”。一个面板和一个卡片布局可以容纳多张卡片,这些卡片本身就是面板。这些面板可以有任何布局,并且包含可以添加到面板的任何组件(包括具有自己布局的其他面板)。@AndrewThompson谢谢这非常有用information@user1998581发生这种情况很好:P