Swing: Would drawing graphic in a JPanel changes its dimension?

不问归期 提交于 2019-12-25 14:46:53

问题


I got a JPanel class and its method paintComponent() is overridden to draw a custom chart.

Would the panel's dimension be stretched if I drew a graphic bigger than the panel?

If answer is no, how could I make it so?


回答1:


Would the panel's dimension be stretched if I drew a graphic bigger than the panel?

No it would not stretch.

..how could I make it so?

Easiest way: Instead of putting the logic of drawing the graph into the paint method, put that logic into drawing an image of the correct size, then display the image in a JLabel.




回答2:


Painting directly won't affect the size of a component, you can paint beyond the visible bounds of a component quite easily.

Generally the size of a component is determine by a combination of getPreferredSize, getMinimumSize and getMaximumSize and the layout manager been used to manage the container in which the component resides.

If you want to affect the size of the panel, you should have it change the return result of (in most cases) the getPreferredSize and invalidate the container hierarchy.

You'd want to do this directly by overriding the getPreferredSize method, rather then relying on setPreferredSize, as this prevents someone else from changing it.

See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details



来源:https://stackoverflow.com/questions/27119468/swing-would-drawing-graphic-in-a-jpanel-changes-its-dimension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!