Jframe执行时大小不正确,为什么?

Jframe执行时大小不正确,为什么?,第1张

Jframe执行时大小不正确,为什么?

我读得很快,正在寻找一种特定的方法

该方法是:

pack();

Jframe中的此方法可能非常有用,但也很难处理,您需要非常了解如何设置每个组件的正确大小。同样,使用“空”布局可以使所有工作正常进行。

我的建议是忘记使用

setLayout(null)

因为根据我的经验,它几乎没有比其他布局更好的结果。

我能告诉你的最好的事情是使用BoxLayout。使用此布局,您可以精确设置每个组件的位置和大小。窍门还在于您可以使用刚性区域和胶水。

您的代码可以是:

// This is a BoxLayout with top to bottom orientation, the trick is to nest many JPanel// with BoxLayout in both the direction to have all working// What I want to achieve is:////     1. Having a main box top to bottom where I will put://- Top margin (a rigid area with dimension (0,MARGIN)//- Main JPanel with BoxLayout and LINE_AXIS (left to right) orientation//- Bottom margin (a rigid area like top margin)////     2. In the main panel I will put://- Left Margin (a rigid area with dimensions (MARGIN,0)//- A JPanel (leftPanel) Boxed top to bottom containing the things on the left that actually are jLabel3 and jLabel2//- A little separator between the two panel, a rigid area (10,0) i.e.//- A JPanel (rightPanel) Boxed top to bottom containing the remaining 4 JLabels//- Right Margin (as left)////     3. In rightPanel JPanel (BoxLayout.PAGE_AXIS, top to bottom) I will have://- a rigid area space to match the position that I want//- the first label//- a rigid area.. etc so on//     //     For each JLabel I must set all://- setPreferredSize(dimension)//- setMinimumSize(dimension)//- setMaximumSize(dimension)////     Then, if you specify also the Jframe is better, but you can try to pack().//     BoxLayout take care of sizes, not exceeding maximum and not making it smaller than minimum. Yust try it and you will love it.getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS);JPanel main = new JPanel();main.setLayout(new BoxLayout(main, BoxLayout.LINE_AXIS));this.add(Box.createRigidArea(new Dimension(0,20))); // VERTICAL SPACe (top margin)this.add(main);this.add(Box.createRigidArea(new Dimension(0,20))); // VERTICAL SPACe (bottom margin)JPanel rightPanel = new JPanel();rightPanel.setLayout( new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));JPanel leftPanel = new JPanel();leftPanel.setLayout( new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));main.add(Box.createRigidArea(new Dimension(20,0))); // HORIZonTAL SPACe (left margin)main.add(leftPanel);main.add(Box.createRigidArea(new Dimension(10,0))); // HORIZonTAL SPACe (between the two)main.add(rightPanel);main.add(Box.createRigidArea(new Dimension(20,0))); // HORIZonTAL SPACe (right margin)// now you should have understood how it works, just try to fill the right and left panel with your labels. Remember to set preferredm, maximum and minimum sizes.

我希望这对您有用。编辑,我想念一些“)”



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5488619.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存