2010-02-18 33 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這樣的平臺作爲起點。它爲創建這些應用程序提供了非常豐富的環境,因此您不必從頭開始一切。在線指南和幫助非常好,有幾本關於此主題的書籍。