在jtabbedPane中添加标签,如谷歌浏览器

时间:2013-11-26 22:51:34

标签: java swing google-chrome jtabbedpane

您好我需要制作一个像谷歌浏览器一样的标签面板。您按下另一个选项卡,并且chrome放置了选项卡。我有这个。 first screen

但是当我按下带有符号“+”的标签时,这就是效果: second screen

我认为用新标签上的位置用符号“+”交换位置,但我不能这样做。我有问题。真的我想像导航铬一样做,那个效果。谢谢。我在这里有代码:

public class Main {

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
    VentanaPrincipalFrame ventana = new     VentanaPrincipalFrame();
                ventana.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

}

主要框架:

public class VentanaPrincipalFrame extends JFrame {

private JTabbedPane tabbedPane;

private JPanel panelAdd;


public VentanaPrincipalFrame() {

    initFrame();

    panelAdd = new JPanel();
    tabbedPane = new JTabbedPane(JTabbedPane.TOP);

    tabbedPane.addChangeListener(new ChangeListener() {

        int i = 1;

        @Override
        public void stateChanged(ChangeEvent e) {

            JTabbedPane tab = (JTabbedPane) e.getSource();

            if( tab.getSelectedIndex() == tabbedPane.getTabCount()-1 && tabbedPane.getTabCount() > 1){
                i++;
                addPestana("Tab"+i);
                //mensaje("Hola soy el ultimo");
            }

        }
    });


    getContentPane().add(tabbedPane, BorderLayout.CENTER);
    tabbedPane.addTab("New tab", new PanelDinamicoPanel());
    tabbedPane.addTab( "+", panelAdd );

}

public void addPestana(String titulo){

    tabbedPane.addTab( titulo , new PanelDinamicoPanel() );

}



private void initFrame() {

    setTitle("Principal");
    setSize(400,400);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);

}



}

我的自定义标签:

public class PanelDinamicoPanel extends JPanel{

private JTextField textField;


public PanelDinamicoPanel() {

    setLayout(new BorderLayout(0, 0));

    JPanel botonesPanel = new JPanel();
    FlowLayout flowLayout_1 = (FlowLayout) botonesPanel.getLayout();
    flowLayout_1.setAlignment(FlowLayout.RIGHT);
    add(botonesPanel, BorderLayout.SOUTH);

    JPanel panel = new JPanel();
    FlowLayout flowLayout = (FlowLayout) panel.getLayout();
    flowLayout.setAlignment(FlowLayout.RIGHT);
    botonesPanel.add(panel);

    JButton btnNewButton = new JButton("New button");
    panel.add(btnNewButton);

    JButton btnNewButton_1 = new JButton("New button");
    panel.add(btnNewButton_1);

    JPanel panel_1 = new JPanel();
    add(panel_1, BorderLayout.CENTER);
    GridBagLayout gbl_panel_1 = new GridBagLayout();
    gbl_panel_1.columnWidths = new int[]{0, 0, 0};
    gbl_panel_1.rowHeights = new int[]{0, 0};
    gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE};
    panel_1.setLayout(gbl_panel_1);

    JLabel lblNombre = new JLabel("Name");
    GridBagConstraints gbc_lblNombre = new GridBagConstraints();
    gbc_lblNombre.insets = new Insets(0, 0, 0, 5);
    gbc_lblNombre.anchor = GridBagConstraints.EAST;
    gbc_lblNombre.gridx = 0;
    gbc_lblNombre.gridy = 0;
    panel_1.add(lblNombre, gbc_lblNombre);

    textField = new JTextField();
    GridBagConstraints gbc_textField = new GridBagConstraints();
    gbc_textField.fill = GridBagConstraints.HORIZONTAL;
    gbc_textField.gridx = 1;
    gbc_textField.gridy = 0;
    panel_1.add(textField, gbc_textField);
    textField.setColumns(10);

}



}

感谢大家。

2 个答案:

答案 0 :(得分:3)

基本上,您不需要“添加”标签,而是需要“插入”一个标签,例如......

 Component tabComp = ...
 tab.insertTab("Title", null, tabComp, null, index);

例如,index是您希望添加新标签的位置。如果你只想在结尾处保持“+”,那么像tab.getTabCount() - 1这样的东西就可以了。

如果您想在最后一个活动标签旁边添加标签,那么您需要更多冒险...

问题是,当触发stateChanged事件时,选择已经更改,因此执行tab.getSelectedIndex()将始终返回“+”标签。

您需要知道的是之前选择的标签。

您可以做的是在状态更改时检查选定的选项卡,如果它不是“+”选项卡,您将需要记录选定的索引值。

当“+”标签变为活动状态时,您只需使用您要跟踪的“最后选择的索引”值作为插入新标签的基础......

例如......

import java.awt.BorderLayout;
import java.awt.EventQueue;
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.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TestTabSelection {

    public static void main(String[] args) {
        new TestTabSelection();
    }

    public TestTabSelection() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final JTabbedPane tab = new JTabbedPane();
                tab.addTab("A", new JPanel());
                tab.addTab("+", new JPanel());
                tab.getModel().addChangeListener(new ChangeListener() {
                    private int lastSelected;
                    private boolean ignore = false;

                    @Override
                    public void stateChanged(ChangeEvent e) {
                        if (!ignore) {
                            ignore = true;
                            try {
                                int selected = tab.getSelectedIndex();
                                String title = tab.getTitleAt(selected);
                                if ("+".equals(title)) {
                                    JPanel pane = new JPanel();
                                    tab.insertTab("Tab" + (tab.getTabCount() - 1), null, pane, null, lastSelected + 1);
                                    tab.setSelectedComponent(pane);
                                } else {
                                    lastSelected = selected;
                                }
                            } finally {
                                ignore = false;
                            }
                        }
                    }
                });
                final JButton btn = new JButton("Add");
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println(tab.getTabCount());
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(tab);
                frame.add(btn, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

答案 1 :(得分:0)

我有一个解决方案:

tab.setTabComponentAt(tab.getTabCount() - 1, new PanelDinamicoPanel());
tab.addTab("+", null);

希望有所帮助,萨拉姆