[英]How to Center Jpanel in JFrame?
There is a class which extends JFrame
. 有一个扩展
JFrame
的类。 then Further JFrame
have JPanel
named contentPane and this Jpanel contains 2 more jpanel. 然后,进一步的
JFrame
具有名为contentPane的JPanel
,此Jpanel包含另外2个jpanel。 as Tree shown in picture. 如图所示的树。
I Want to Center that contentPane in JFrame
so that on changing size of JFrame
JPanel
(contentPane) will remain in center. 我想在
JFrame
将该contentPane居中,以便在更改JFrame
大小时, JPanel
(contentPane)保持居中。 I have tried with different Layouts
but did not come up with right one. 我尝试使用不同的
Layouts
但没有提出正确的Layouts
。 is there any way ? 有什么办法吗? Full page picture is here
整张图片在这里
Code is this. 代码是这个。
public class Purchases extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Purchases frame = new Purchases();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 513, 438);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBounds(10, 10, 477, 193);
contentPane.add(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBounds(10, 214, 477, 174);
contentPane.add(panel_1);
panel_1.setLayout(null);
}
This code is Eclipse automatically generated. 这段代码是Eclipse自动生成的。 I did not find where the contentPane is added in JFrame.
我没有在JFrame中找到contentPane的位置。
Set the frame's layout to GridBagLayout
. 将框架的布局设置为
GridBagLayout
。 Wrap your existing content in another JPanel
, add this panel to the frame 将现有内容包装到另一个
JPanel
,将此面板添加到框架中
Take a look at Laying Out Components Within a Container and How to Use GridBagLayout for more details 看一看在容器中布置组件以及如何使用GridBagLayout以获得更多详细信息
You really should avoid using null
layouts, pixel perfect layouts are an illusion within modern ui design. 您确实应该避免使用
null
布局,像素完美布局是现代ui设计中的一种幻觉。 There are too many factors which affect the individual size of components, none of which you can control. 有太多因素会影响组件的单个大小,您无法控制。 Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正
I have made a sample for you. 我已经为您制作了样品。 Modify it as per your need.
根据需要进行修改。 Just want to show you what you want, is working.
只是想向您展示您想要什么,正在工作。
TestClass()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 513, 438);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
JButton b1 = new JButton("hello");
JPanel panel = new JPanel(new BorderLayout());
//panel.setBounds(10, 10, 477, 193);
panel.add(b1, BorderLayout.CENTER);
contentPane.add(panel, BorderLayout.EAST);
//panel.setLayout(null);
JButton b2 = new JButton("why");
JPanel panel_1 = new JPanel(new BorderLayout());
//panel_1.setBounds(10, 214, 477, 174);
panel_1.add(b2, BorderLayout.CENTER);
contentPane.add(panel_1, BorderLayout.WEST);
//panel_1.setLayout(null);
this.getContentPane().add(contentPane);
}
Hope this will help. 希望这会有所帮助。 :-)
:-)
I think you should set the maximum size of the element & set it to align center: 我认为您应该设置元素的最大尺寸并将其设置为对齐中心:
setAlignmentX(JComponent.CENTER_ALIGNMENT);
setMaximumSize(new Dimension(100, 100));
As explained here: How can I properly center a JPanel ( FIXED SIZE ) inside a JFrame? 如此处所述: 如何正确地将JPanel(FIXED SIZE)放在JFrame内部?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.