Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java删除选项卡并将jpanel恢复到原始状态_Java_Tabs - Fatal编程技术网

java删除选项卡并将jpanel恢复到原始状态

java删除选项卡并将jpanel恢复到原始状态,java,tabs,Java,Tabs,我有一个带按钮的jpanel(jpanel a),按下时 将打开一个选项卡jpanel(示例1) 因此,当您按下按钮时,您将看到两个选项卡jpanel A和示例1 原来的japanel现在是一个选项卡式面板 当我删除示例1时,我只剩下jpanel A,但该面板位于选项卡中 . 当另一个jpanel(示例1)被删除时,有没有办法恢复到原来的jpanel 我制作了一个与我类似的示例应用程序: 点击呼叫一个你得到的jpanel。 然后单击按tab键2次,您将获得2个新选项卡 删除2个选项卡后

我有一个带按钮的jpanel(jpanel a),按下时 将打开一个选项卡jpanel(示例1)

因此,当您按下按钮时,您将看到两个选项卡jpanel A和示例1

原来的japanel现在是一个选项卡式面板

当我删除示例1时,我只剩下jpanel A,但该面板位于选项卡中 . 当另一个jpanel(示例1)被删除时,有没有办法恢复到原来的jpanel

我制作了一个与我类似的示例应用程序:

点击呼叫一个你得到的jpanel。

然后单击按tab键2次,您将获得2个新选项卡

删除2个选项卡后,您将获得:

我想做的是以这样的方式结束: i、 e.返回原始内容删除选项卡保留内容

该应用程序分为3个类

  • CreatePanel创建右侧jpanel,按住按钮创建选项卡
  • MainGUI创建jframe左侧以调用右侧的面板
  • CloseButton选项卡以创建选项卡并控制它们
  • 代码如下: MainGUI

                   import java.awt.BorderLayout;
                   import java.awt.Color;
                   import java.awt.Dimension;
                   import java.awt.GridLayout;
                   import java.awt.event.ActionEvent;
                   import java.awt.event.ActionListener;
                   import javax.swing.JButton;
                   import javax.swing.JFrame;
                   import javax.swing.JPanel;
                   import javax.swing.JTabbedPane;
                   import javax.swing.SwingUtilities;
                   import javax.swing.border.EmptyBorder;
    
          public class MainGUI extends JFrame {
    
         private JPanel jPanelLeft;
        private JPanel jPanelRight;
        private JButton callPanelBtn;
        private JTabbedPane tab;
    
            public MainGUI() {
    
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1280, 600); // set frame size
        this.setVisible(true); // display frame
        this.setTitle("Tab Example");
    
        setLayout(new BorderLayout()); // layout manager
        jPanelLeft = new JPanel();
        jPanelLeft.setLayout(null);
        jPanelLeft.setPreferredSize(new Dimension(400, 800));  // to set the size of the left panel
        jPanelLeft.setBackground(Color.blue);
        this.add(jPanelLeft, BorderLayout.WEST);
    
        callPanelBtn = new JButton("Call a jPanel");
        callPanelBtn.addActionListener(btn);
        callPanelBtn.setBounds(150, 200, 150, 40);
        jPanelLeft.add(callPanelBtn);
    
        jPanelRight = new JPanel();
        jPanelRight.setBorder(new EmptyBorder(0, 0, 0, 0));
        jPanelRight.setLayout(new GridLayout(0, 1));
        this.add(jPanelRight);
    
        tab = new CloseButtonTabbedPane();
    
    }//endd constructor
    
    ActionListener btn = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
    
            if (ae.getActionCommand() == "Call a jPanel") {
                jPanelRight.add(new CreatePanel(t));
                jPanelRight.revalidate();
    
            }
        }
    
    };
    
    //    Actionlistener for CreatePanel
    ActionListener t = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
    
            if (tab.getTabCount() <= 8) {
    
                // if no tabs make main tab
                if (tab.getTabCount() == 0) {
    
                    tab.addTab("Main Content", jPanelRight); // name for listener not to close
                    add(tab);
                    revalidate();
                    repaint();
                }
    
                if (tab.getTabCount() > 0) {
                    JPanel newJPanel = new JPanel();
                    tab.addTab("new ", newJPanel);
    
                    tab.setSelectedIndex(tab.getTabCount() - 1);
                }
            }
    
        }
    
    };
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
    
                new MainGUI();
            }
    
        });
    
    } // end main
    
     }//end class
    
    CloseButtonTabbedPane

                          import javax.swing.*;
                          import javax.swing.plaf.metal.MetalIconFactory;
                          import javax.swing.plaf.basic.BasicTabbedPaneUI;
                          import java.awt.image.BufferedImage;
                          import java.awt.*;
                          import java.awt.event.MouseListener;
                          import java.awt.event.MouseEvent;
                          import java.io.IOException;
                          import java.util.logging.Level;
                          import java.util.logging.Logger;
    
                        public class CloseButtonTabbedPane extends JTabbedPane {
    
    public CloseButtonTabbedPane() {
    
    }
    
    @Override
    public void addTab(String title, Icon icon, Component component, String tip) {
        super.addTab(title, icon, component, tip);
        int count = this.getTabCount() - 1;
        setTabComponentAt(count, new CloseButtonTab(component, title, icon));
    }
    
    @Override
    public void addTab(String title, Icon icon, Component component) {
        addTab(title, icon, component, null);
    }
    
    @Override
    public void addTab(String title, Component component) {
        addTab(title, null, component);
    }
    
    public class CloseButtonTab extends JPanel {
    
        private Component tab;
    
        public CloseButtonTab(final Component tab, String title, Icon icon) {
            this.tab = tab;
            setOpaque(false);
            FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 3, 3);
            setLayout(flowLayout);
            setVisible(true);
    
            JLabel jLabel = new JLabel(title);
            jLabel.setIcon(icon);
            add(jLabel);
    
            JButton button = new JButton(MetalIconFactory.getInternalFrameCloseIcon(16));
            button.setMargin(new Insets(0, 0, 0, 0));
            button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
            button.addMouseListener(new MouseListener() {
                public void mouseClicked(MouseEvent e) {
                    JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent();
                    if (tabbedPane.getSelectedIndex() >= 1) {
                        tabbedPane.remove(tab);
                    }
    
                }
    
                public void mousePressed(MouseEvent e) {
                }
    
                public void mouseReleased(MouseEvent e) {
                }
    
                public void mouseEntered(MouseEvent e) {
                    JButton button = (JButton) e.getSource();
                    button.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
                }
    
                public void mouseExited(MouseEvent e) {
                    JButton button = (JButton) e.getSource();
                    button.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
                }
            });
    
            add(button);
    
        }
    }
        }
    

    我不知道是否有出厂解决方案,但对于所有
    JComponents
    组件,您可以使用
    setUI
    方法设置一个自定义UI对象来处理外观。
    JTabbedPanes
    的一个实现称为
    BasicTabbedPaneUI
    ,它有一个方法
    CalculateBareahHeight
    ,可以计算顶部选项卡栏的高度。
    因此,您可以将自己的
    BasicTabbedPaneUI
    实例设置为
    JTabbedPane
    如果只存在一个选项卡,则重写此方法以返回
    0

    把这个放在你的构造函数里

    [...]
    tab = new CloseButtonTabbedPane();
    
    tab.setUI(new BasicTabbedPaneUI(){
        @Override
        protected int calculateTabAreaHeight(int tabPlacement, int horizRunCount, int maxTabHeight){
            if (tab.getTabCount() == 1) return 0;
            return super.calculateTabAreaHeight(tabPlacement, horizRunCount, maxTabHeight);
        }
    });  
    

    我不知道是否有出厂解决方案,但对于所有
    JComponents
    组件,您可以使用
    setUI
    方法设置一个自定义UI对象来处理外观。
    JTabbedPanes
    的一个实现称为
    BasicTabbedPaneUI
    ,它有一个方法
    CalculateBareahHeight
    ,可以计算顶部选项卡栏的高度。
    因此,您可以将自己的
    BasicTabbedPaneUI
    实例设置为
    JTabbedPane
    如果只存在一个选项卡,则重写此方法以返回
    0

    把这个放在你的构造函数里

    [...]
    tab = new CloseButtonTabbedPane();
    
    tab.setUI(new BasicTabbedPaneUI(){
        @Override
        protected int calculateTabAreaHeight(int tabPlacement, int horizRunCount, int maxTabHeight){
            if (tab.getTabCount() == 1) return 0;
            return super.calculateTabAreaHeight(tabPlacement, horizRunCount, maxTabHeight);
        }
    });  
    

    这太棒了,谢谢它在我的示例应用程序中起作用。我将把它集成到我的应用程序中并进行测试。。再次谢谢你,不客气。如果有用的话,别忘了带上它!这太棒了,谢谢它在我的示例应用程序中起作用。我将把它集成到我的应用程序中并进行测试。。再次谢谢你,不客气。如果有用的话,别忘了带上它!