JPanel 和 LayerManager 之间的区别
好吧,我认为每个 JComponent 都可以设置其位置...打赌 JPanel、JButton、JLabel...等等。它可以使用 .setLocation(x,y)
设置其位置。
我开始怀疑,实际上当我这样做时,
JButton btn = new JButton("Click me!") ;
btn.setLocation(10,200);
我实际上是在更改面板中按钮的位置,而不是框架中的位置。如果我这样做,
JPanel jPanel = new JPanel();
jPanel.setLocation(10,100);
我将更改 jPanel 的位置,而不是在 JFrame 中,而是在默认提供的默认 JPanel 中。既然 JComponent 可以改变它们的位置,为什么不把所有东西直接转储到该死的 jFrame 对象中呢? :S
我还没有尝试过,但我相信我可以通过使用 setLocation(x,y) 方法来安排东西......我想这会是一个很大的痛苦。 这引出了我的问题..如果我们可以使用该方法设置组件位置,那么 LayoutManager 是做什么用的?
您能提供示例来说明差异吗?
Ok I gather that every JComponent can set its location...bet it JPanel, JButton, JLabel..whatever. It can set its location use .setLocation(x,y)
.
I have come to suspect that actually when I do
JButton btn = new JButton("Click me!") ;
btn.setLocation(10,200);
I am actually changing the location of a button in a panel, and not in the frame. And if I do
JPanel jPanel = new JPanel();
jPanel.setLocation(10,100);
I am changing the location of jPanel not in the JFrame but in the default JPanel provided by default. So JComponents can change their locations, why not just dump everything directly into a bloody jFrame object? :S
I haven't tried but I believe I can arrange stuff just by using the setLocation(x,y)
method..and I guess that'd be a big pain the butt.
This leads to my question..if we can set components location by using the method, what is the LayoutManager for?
Can you please provide example to show the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用布局的一个示例,以及一个挑战。
挑战是使用
setLocation()
/setBounds 制作该 UI 的可调整大小、PLAF 可更改版本()
。如果您(或任何人)可以管理它(代码足够小,可以发布到线程中),我将为答案贡献500 赏金点。
Here is an example of using layouts, as well as a challenge.
The challenge is to make a resizable, PLAF changable version of that UI using
setLocation()
/setBounds()
.If you (or anyone) can manage it (in code that is small enough to post to the thread), I'll contribute 500 bounty points to the answer.