我在谷歌上搜索了很多关于这个主题的内容,但就是找不到合适的解决方案。我试着用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并且组件不限于锁定到位时,这些类型的连接是最有用的。但你会知道什么对你的案子最有利。