java删除选项卡并将jpanel恢复为原始版本

时间:2016-02-23 12:26:24

标签: java tabs

我有一个带按钮的jpanel(jpanel A),按下时 将打开一个标签jpanel(例1)

enter image description here

因此,当您按下按钮时,您最终会得到2个选项卡jpanel A和示例1。

原始日本现在是一个标签式面板

当我删除示例1时,我留下了jpanel A但是此面板位于选项卡中 。 enter image description here 是否有一种方法可以在删除其他jpanel(示例1)时恢复原始jpanel。

enter image description here

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

enter image description here

点击通话后你会得到一个jpanel。 enter image description here

然后单击按下选项卡2次,即可获得2个新选项卡 enter image description here

删除2个标签后,您会得到: enter image description here

我正在尝试做的事情是: enter image description here 即返回原始状态删除TAB保存内容

申请分为三类。

  1. CreatePanel制作右侧的jpanel并按住按钮制作标签。
  2. MainGUI创建jframe左侧以调用右侧的面板。
  3. CloseButtonTabbedPane创建选项卡并控制它们。
  4. 这是代码: 的 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
    

    CreatePanel

                        import java.awt.Color;
                        import java.awt.Dimension;
                        import java.awt.event.ActionListener;
                        import javax.swing.BorderFactory;
                        import javax.swing.JButton;
                        import javax.swing.JPanel;
    
    
                       public class CreatePanel extends JPanel {
                        private JPanel jPanel1;
                        private final JButton tabBTN;
    
    public CreatePanel(ActionListener t){
    jPanel1 = new JPanel();
    jPanel1.setLayout(null);
    jPanel1.setBorder(BorderFactory.createBevelBorder(1, Color.lightGray, Color.lightGray));
    jPanel1.setPreferredSize(new Dimension(850, 300));
    jPanel1.setBackground(Color.green);
    add(jPanel1);
    
     //add button for tabs
        tabBTN = new JButton("Press for Tab");
        tabBTN.setBounds(400, 100, 150, 30);
        tabBTN.addActionListener(t);
        jPanel1.add(tabBTN);
        }//end constructor
       }//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);
    
        }
    }
        }
    

1 个答案:

答案 0 :(得分:0)

我不知道是否有工厂解决方案,但对于所有JComponents,您可以设置一个自定义UI对象来处理Look&amp;感觉是指使用setUI方法的外观。 JTabbedPanes的一个实现称为BasicTabbedPaneUI,其方法为calculateTabAreaHeight,可以计算顶部标签栏的高度。
因此,如果只存在一个标签,您可以将BasicTabbedPaneUI的{​​{1}}实例设置为覆盖此方法的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);
    }
});