2010-02-18 60 views
2

我正在寻找在Java桌面应用程序中创建Outlook风格的用户界面,在左侧窗格中列出上下文或节点,并在右侧窗格中选择上下文。我如何去做这件事?在Java中创建Outlook风格的用户界面?

我在寻找比'使用JFrame'更详细的信息。教程或漫步是好的,或者是一些框架代码,或者是一种框架/库,它提供了这种开箱即用的功能。

谢谢。

编辑

我(编)码到目前为止:

UIPanel

public class UIPanel extends javax.swing.JPanel { 

    private final JSplitPane splitPane; 

    public UIPanel() { 
     super(new BorderLayout()); 
     initComponents(); 

     JPanel contextPnl = new ContextPanel(); 
     JPanel treePnl = new NodePanel(contextPnl); 

     this.splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
      true, new JScrollPane(treePnl), new JScrollPane(contextPnl)); 

     add(splitPane, BorderLayout.CENTER); 

     //not sure I need these? 
     splitPane.setVisible(true); 
     treePnl.setVisible(true); 
     contextPnl.setVisible(true); 
} 

NodePanel

public class NodePanel extends javax.swing.JPanel { 

    JPanel _contextPanel; 

    public NodePanel(JPanel contextPanel) { 
     initComponents(); 
     _contextPanel = contextPanel; 
     initialise(); 
    } 

    private void initialise(){ 
     nodeTree.addTreeSelectionListener(getTreeListener()); 
    } 

    private TreeSelectionListener getTreeListener(){ 
     return new TreeSelectionListener() { 
      public void valueChanged(TreeSelectionEvent e) { 
       DefaultMutableTreeNode node = (DefaultMutableTreeNode) 
           nodeTree.getLastSelectedPathComponent(); 
      // if nothing is selected 
      if (node == null) 
       return; 

     // get selected node 
     Object nodeInfo = node.getUserObject(); 

     CardLayout layout = (CardLayout) _contextPanel.getLayout(); 
     //layout.show(_contextPanel, "test"); //show context for selected node 

     } 
    }; 
} 

ContextPanel

public class ContextPanel extends javax.swing.JPanel { 

    JPanel _cards; 
    final static String CONTEXT1 = "Context 1"; 
    final static String CONTEXT2 = "Context 2"; 
    JPanel _context1; 
    JPanel _context2; 


    public ContextPanel() { 
     initComponents(); 
     intialiseContexts(); 
    } 

    public void updateContext(String contextName){ 
     //TODO 
    } 

    private void intialiseContexts(){ 
     _context1 = new NodeContext(); 
     _context2 = new NodeContext(); 
     _cards = new JPanel(new CardLayout()); 
     _cards.add(_context1, CONTEXT1); 
     _cards.add(_context2, CONTEXT2); 
} 
+0

Malcom,你能发布一个图片的链接吗? – Pete 2010-09-09 14:53:02

回答

3

这里的关键概念是定义一个JSplitPane为您的顶级Component与水平分割。拆分窗格的左侧变成您的“树”视图,而右侧是上下文面板。

的技巧是使用CardLayout你的背景板和登记与TreeSelectionListener树面板的JTree,这样每当选择了树的节点,在CardLayoutshow方法被调用,以更新上下文面板是什么目前显示。您还需要将各种组件添加到上下文面板才能使此方法起作用。

public class UIPanel extends JPanel { 
    private static final String BLANK_CARD = "blank"; 
    private final JSplitPane splitPane; 

    public UIPanel() { 
    super(new BorderLayout()); 

    JPanel treePnl = createTreePanel(); 
    JPanel contextPnl = createContextPanel(); 

    this.splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
     true, new JScrollPane(treePnl), new JScrollPane(contextPnl)); 

    add(splitPane, BorderLayout.CENTER); 
    } 
} 

编辑:使用示例

public class Main { 
    public static void main(String[] args) { 
    // Kick off code to build and display UI on Event Dispatch Thread. 
    SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
     JFrame frame = new JFrame("UIPanel Example"); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setLayout(new BorderLayout()); 

     // Add UIPanel to JFrame. Using CENTER layout means it will occupy all 
     // available space. 
     frame.add(new UIPanel(), BorderLayout.CENTER); 

     // Explicitly set frame size. Could use pack() instead. 
     frame.setSize(800, 600); 

     // Center frame on the primary display. 
     frame.setLocationRelativeTo(null); 

     // Finally make frame visible. 
     frame.setVisible(true); 
     } 
    }); 
    } 
} 

额外建议

  • 我可以看到你创建单独的类为您NodePanelContextPanel。鉴于这些类的简单性以及它们的紧密耦合程度,可以将所有UI组件直接嵌入到UIPanel之内并且具有构建这两个子面板的实用方法。如果你确实保留了NodePanel和ContextPanel,试着让它们封装私有而非公开。

  • CardLayout方法运行良好,如果你有一个小(ish)数量的节点,并且你事先知道它们(因此可以预先将它们相应的组件添加到CardLayout中)。如果没有,您应该考虑简单地使用BorderLayout的上下文面板,并且无论您何时单击节点,只需将相关节点组件添加到NodePanelBorderLayout.CENTER位置和调用面板即可。 revalidate()以使其再次执行其布局。之前我使用过CardLayout的原因是这意味着我的节点只需记住一条信息:卡的名称。但是,现在我想起来,我认为这种方法并没有带来任何真正的劣势 - 事实上它可能更加灵活。

+0

谢谢,看起来像个玩家,我会试试:) – MalcomTucker 2010-02-18 16:39:46

+0

Adamski - 我已经创建了一个面板树,一棵树的监听器,一个上下文面板和一组上下文(卡片),两者都是在上面的uipanel中使用,但是当我将uipanel放到应用程序中时,什么都看不到 - 没有节点树,没有上下文。我错过了什么?有任何想法吗? – MalcomTucker 2010-02-18 18:24:23

+0

在安装uipanel的地方看不到代码很难说。我怀疑你没有在添加面板的JFrame/JDialog中使用正确的LayoutManager。尝试将布局设置为BorderLayout,然后调用frame.add(uiPanel,BorderLayout.CENTER); – Adamski 2010-02-18 18:45:50

0

您可能想要使用像eclipse这样的平台作为起点。它为创建这些应用程序提供了非常丰富的环境,因此您不必从头开始一切。在线指南和帮助非常好,有几本关于此主题的书籍。