[英]How to connect JLabels with a line on JPanel with MigLayout?
我在Google上搜索了很多有關此主題的內容,但無法提出正確的解決方案。 我嘗試使用Graphics paintComponent
“繪畫”,但一切似乎都很好,但是行沒有出現在我的JPanel
。 使用JLabels
創建的部分代碼:
frame = new JFrame();
frame.setTitle("New family tree");
...
JPanel panel = new JPanel();
panel.setBackground(new Color(30, 144, 255));
frame.getContentPane().add(panel, BorderLayout.EAST);
panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));
JButton newPersonButton = new JButton("New Person");
panel.add(newPersonButton, "cell 0 5");
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
scrollPane = new JScrollPane();
tabbedPane.addTab("Tree", null, scrollPane, null);
panel_1 = new JPanel();
scrollPane.setViewportView(panel_1);
panel_1.setLayout(new MigLayout("",
"[][][][][][][][][][][][][][][][][]",
"[][][][][][][][][][][][][][][][][][][][][]"));
final JLabel lblAddGreatgrandmother = new JLabel("Add Great-grandmother");
panel_1.add(lblAddGreatgrandmother, "cell 3 4,growx");
final JLabel lblAddGrandmother_1 = new JLabel("Add Grandmother");
panel_1.add(lblAddGrandmother_1, "cell 2 5");
我應該使用繪畫嗎? 或者將JLabels
放在數組列表中並使用Point
? 我會尋求任何幫助。
編輯:可運行的例子- http://pastebin.com/NFug1QA1
java-sl.com/connector解決方案的問題在於,連接本身成為一個組件,即需要布局,接受事件等。我幾年前提出了一個解決方案,您可以在我的sourceforge項目中找到它。 具體來說,請參見ConnectionPanel和Connection。
...此外,以我的經驗來看,當布局管理器設置為null且組件不限於固定在適當位置時,它們最有用。 但是您會知道最適合您的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.