繁体   English   中英

是否可以制作这种类型的jtabbedpane?

[英]Is it possible to make this type of jtabbedpane?

我想在框架中创建一些选项卡,但我不知道是否可以根据需要创建它们。 这些选项卡必须在左侧,并且在它们下方必须是一些jpanel,分别针对每个选项卡。

在此处输入图片说明

JTabbedPane不能真正做到这一点。 我认为可以破解TabbedPaneUI,但我认为这种方法不可靠。

相反,我只是将JToggleButtons放在GridLayout中。 它们看起来并不完全像选项卡,但它们将像标签一样有用,我怀疑用户会关心这两种方式。

标签下方的小“信息”区域和主要内容区域均应均为CardLayout。 这将确保每个文件都足够大以容纳所有可能的内容。

String title1 = "Tab 1";
String title2 = "Tab 2";

Component infoPanel1 = new FileInfoPanel();
Component infoPanel2 = new OtherInfoPanel();

Component content1 = new WaveFormPanel();
Component content2 = new OtherPanel();

final CardLayout infoLayout = new CardLayout();
final JComponent infoArea = new JPanel(infoLayout);
infoArea.add(infoPanel1, "1");
infoArea.add(infoPanel2, "2");

final CardLayout contentAreaLayout = new CardLayout();
final JComponent contentArea = new JPanel(contentAreaLayout);
contentArea.add(content1, "1");
contentArea.add(content2, "2");

class TabSwitcher
implements ActionListener {
    private String cardID;

    TabSwitcher(String cardID) {
        this.cardID = cardID;
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        infoLayout.show(infoArea, cardID);
        contentAreaLayout.show(contentArea, cardID);
    }
}

JToggleButton tab1 = new JToggleButton("Tab 1");
tab1.setHorizontalAlignment(SwingConstants.LEADING);
tab1.addActionListener(new TabSwitcher("1"));

JToggleButton tab2 = new JToggleButton("Tab 2");
tab2.setHorizontalAlignment(SwingConstants.LEADING);
tab2.addActionListener(new TabSwitcher("2"));

ButtonGroup group = new ButtonGroup();
group.add(tab1);
group.add(tab2);

JComponent tabsPanel = new JPanel(new GridLayout(0, 1));
tabsPanel.setBorder(BorderFactory.createEmptyBorder(6, 0, 12, 0));
tabsPanel.add(tab1);
tabsPanel.add(tab2);

JComponent tabArea = new JPanel(new BorderLayout());
tabArea.add(tabsPanel, BorderLayout.PAGE_START);
tabArea.add(infoArea, BorderLayout.CENTER);

JComponent analyser = new JPanel(new BorderLayout());
analyser.add(tabArea, BorderLayout.LINE_START);
analyser.add(contentArea, BorderLayout.CENTER);

tab1.doClick();

frame.getContentPane().add(analyser);
frame.pack();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM