通过替换JTabbedPane中的另一个JPanel来更新JPanel



我正在使用NetBeans GUI为我的应用程序开发图形界面。总之,我在JFrame中有一个JTabbedPane,简单地描述为:

public class MyApplication extends JFrame(){

    private JTabbedPane tabbedpanel_tasks;
    private JPanel jpanel_father;
    ...
    // Settings of JTabbedPane
    tabbedpanel_tasks = new JTabbedPane();
    tabbedpanel_tasks.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    tabbedpanel_tasks.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    jpanel_father = new JPanel();
    ...
}

我有一个特定的JPanel——我将命名为jpanel_father——它是组成JTabbedPane的几个JPanel之一:

public class MyApplication extends JFrame(){
    ...
    // Initializing a new JPanel and inserting into JTabbedPane
    jpanel_father = new JPanel();
    tabbedpanel_tasks.addTab("Father tab", jpanel_father);
    ...
}

查看jpanel_father,有一个JComboBox (combobox)和一个JPanel (jpanel_generic),它假定假定一个特定的JPanel扩展类,作为NetBeans的JPanel Form创建。组合框存储2个值,这意味着当我选择其中一个值时,关联的JPanel将出现在jpanel_generic的位置(如下面的代码所示):

public class MyApplication extends JFrame(){
    private JComboBox combobox;
    private JPanel jpanel_generic;
    private JPanelSon1 jpanel_son1;
    private JPanelSon2 jpanel_son2;
    ...
    // Code block for constructing jpanel_father
    combobox = new JComboBox(new String[] { "JPanel 1", "JPanel 2" });
    jpanel_generic = new JPanel();
    jpanel_son1 = new JPanelSon1();
    jpanel_son2 = new JPanelSon2();
    combobox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int option = combobox.getSelectedIndex();
            setCurrentGenericPanel(option);
        }
});
    jpanel_father.add(combobox);
    jpanel_father.add(jpanel_generic);
    jpanel_father.setSize(300,300);
    jpanel_father.setVisible(true);
}
public void setCurrentGenericPanel(int option){
    jpanel_generic.removeAll();
    if(option == 0)
       jpanel_generic = jpanel_son1;
    else
       jpanel_generic = jpanel_son2;
    jpanel_generic.validate();
    jpanel_generic.repaint();
}

public class JPanelSon1 extends JPanel {
    private JLabel labelson1;
    public JPanelSon1(){
        labelson1 = new JLabel("This is JPanel Son 1");
        add(labelson1);
        this.setSize(300,300);
        this.setVisible(true);
    }
}
public class JPanelSon2 extends JPanel {
    private JLabel labelson2;
    public JPanelSon2(){
        labelson2 = new JLabel("This is JPanel Son 2");
        add(labelson2);
        this.setSize(300,300);
        this.setVisible(true);
    }
}

然而,当我在组合框上选择一个值时,什么也没有发生。我的意思是……没有使用相应的正确JPanel更新panel_generic。我的代码可能有什么问题?我很抱歉遗漏了导入、中间代码等细节

谢谢!

代码:

public void setCurrentGenericPanel(int option){    
    jpanel_generic.removeAll();    
    if(option == 0)
       jpanel_generic = jpanel_son1;
    else
       jpanel_generic = jpanel_son2;
    jpanel_generic.validate();
    jpanel_generic.repaint();
}

使用jpanel_generic.removeAll();方法调用,您将使用removalAll()方法从中删除所有组件。您没有从其容器中删除jpanel_generic JPanel,我相信这是您使用此调用的目标。

同样,更改jpanel_generic变量引用的变量不会交换GUI中显示的对象。这个问题可以归结为对象和变量之间的巨大差异。要手动完成您尝试完成的工作,您需要从保存 jpanel_generic JPanel的JPanel容器中删除所有组件,然后将新组件添加到同一容器中,然后重新验证并重新绘制容器

但是为了不把这些弄乱,我建议一个更简单的解决方案,即容器JPanel使用CardLayout,然后使用该布局来交换JPanel。谷歌Java CardLayout Tutorial。第一个谷歌点击血腥的细节

相关内容

  • 没有找到相关文章

最新更新