简体   繁体   English

如何将JLabel添加到JUNG图?

[英]How do I add a JLabel to a JUNG graph?

I'm trying to add a label to my graph, but can't get it out of the default center position. 我正在尝试向图形添加标签,但是无法使其脱离默认的中心位置。 This is what I have right now in my update method (it refreshes the display): 这就是我现在在更新方法中所拥有的(刷新显示):

Graph<String, String> graph = getGraph();
BasicVisualizationServer<String, String> vv = getViewer(graph);
frame.getContentPane().removeAll();
frame.getContentPane().add(vv);
JLabel label = new JLabel("<html>ndp: blue<br>mdp: dark green</html>", JLabel.LEFT);
vv.add(label);
frame.pack();

It shows the label, but won't put it on the left no matter what I attach the label to or how I specify that it should be left... What am I doing wrong? 它显示了标签,但是无论我将标签附加到什么地方或如何指定应该保留标签,都不会将其放在左侧...我在做什么错了?

Just a thought, but try programming how far from the top, bottom, left and right and adjust from there. 只是一个想法,但是尝试编程从顶部,底部,左侧和右侧有多远,然后从那里进行调整。 If you are using Eclipse and the WindowBuilder, you will see the code on how it programs the position of things for starters. 如果您使用的是Eclipse和WindowBuilder,则将看到有关如何为初学者编写事物位置的代码。 I believe it uses NORTH, SOUTH, EAST, WEST instead of the previous directions to place components. 我相信它使用NORTH,SOUTH,EAST,WEST而不是先前的指示来放置组件。 From there, you should be able to adjust the values (programmatically) and place the JLabel where you want it to be. 从那里,您应该能够(以编程方式)调整值并将JLabel放置在所需的位置。

It's ugly, but I fixed it by just adding lots of non-breaking spaces to one row of the label content (Alt+0160), which expanded the width of the Label to be roughly the size of the window so when it is centered, the text is left-aligned. 这很丑陋,但我通过在标签内容的一行(Alt + 0160)中添加了许多不间断的空格来解决此问题,这将Label的宽度扩展为与窗口大致相同的大小,因此在居中时文本左对齐。 setSize() didn't work. setSize()无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM