我需要使用 Java Swing 设计一个类似于Firefox > tools > options
(如下所示)的窗口。我必须创建两个顶部标题。单击每个我必须打开选项卡窗格(与 相同Firefox > tools > options > advanced
。我对 Swing 非常陌生。
感谢您的快速回复。我已尝试实施相同的操作。基本窗口是 JDialog。我在其中创建了顶部面板。工具栏(在 topPanel 的 BorderLayout-North 处)和内容面板(在 topPanel 的 BorderLayout-Center 处)被添加到这个顶部面板中。创建了两个工具栏按钮——“通用”和“高级”,它们是我在上一篇文章中谈到的标题。“contentPanel”是占位符面板。每当单击工具栏按钮时,我想在其中动态添加选项卡式面板。默认值是通用的。
单击高级工具栏按钮时,我想删除 _generalTabbedPane 并显示 _advanceTabbedPane ,反之亦然。
但它没有正确发生。单击工具栏按钮时添加和删除/隐藏面板是我需要修复的。我附上了下面的代码。
请帮忙。
@Override
public JComponent createContentPanel()
{
topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
JToolBar toolBar = new JToolBar();
toolBar.add(new tabButton("general",GENERAL));
toolBar.add(new tabButton("advanced",ADVANCED));
contentPanel = new JPanel();
contentPanel.add(getGeneralTabs());
topPanel.add(toolBar, BorderLayout.NORTH);
topPanel.add(contentPanel, BorderLayout.CENTER);
return topPanel;
}
private final class JCenterLabel extends JLabel
{
public JCenterLabel(final String s)
{
super(s);
setAlignmentX(Component.CENTER_ALIGNMENT);
}
public JCenterLabel(final Icon i)
{
super(i);
setAlignmentX(Component.CENTER_ALIGNMENT);
}
}
private class tabButton extends JButton
{
public tabButton ( String tabId,String actionCommand)
{
super();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(new JCenterLabel(UIManager.getIcon("OptionPane.informationIcon")));
add(new JCenterLabel(tabId));
this.setActionCommand(actionCommand);
this.setName(tabId);
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (GENERAL.equals(cmd)) {
if(contentPanel != null){
contentPanel.remove(_advanceTabbedPane);
contentPanel.add(getGeneralTabs());
contentPanel.revalidate();
contentPanel.repaint();
}
} else if (ADVANCED.equals(cmd)) {
if(contentPanel != null){
contentPanel.remove(_generalTabbedPane);
contentPanel.add(getAdvancedTabs());
contentPanel.revalidate();
contentPanel.repaint();
}
}
}
});
}
}
private JideTabbedPane getGeneralTabs(){
_generalTabbedPane = new JideTabbedPane();
_generalTabbedPane.setModel(new TabbedPaneWithValidationModel());
//adding the tabs to the _generalTabbedPane
//-----
//---
return _generalTabbedPane;
}
private JideTabbedPane getAdvancedTabs(){
_advanceTabbedPane = new JideTabbedPane();
_advanceTabbedPane.setModel(new TabbedPaneWithValidationModel());
//adding the tabs to the _advanceTabbedPane
//-----
//---
return _advanceTabbedPane;
}