是否可以制作这种类型的 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();
我想在我的框架中创建一些标签,但我不知道是否可以按照我的意愿制作它们。这些选项卡必须在左侧,它们下面必须是一些 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();