问题描述
我对Java上的GUI知之甚少,因为后端更多的是IM,而不是视图方面的,因此我在Jframe上正确对齐和布局内容时遇到了问题.基本上我想得到这些结果
i have some very little knowledge regarding GUIs on Java since im more on the backend not on the view side, so i am having a problem properly aligning and layouting stuff on the Jframe. Basically i wanted to have these result
图像已经说明了一切,但是运行代码时我所拥有的就是这个.
The image speaks for itself already, but what i have when i run my code is this.
两端没有填充,面板正好在Jframe的边框上打点,我要更正,我不想采用这种布局,其中面板的尺寸与面板的尺寸相同. Jframe,至少在两端都有填充.
There is no padding on both ends, the panel is right smack on the border of the Jframe, which i want to correct, i don't want to have this kind of layout wherein the panel is of the same size as the Jframe, at least there would be somekind of padding on both ends.
这是我的代码,这是在我的Frame的构造函数上
here is my code though, this is on the constructor of my Frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] cNames = {"First Name", "Middle Name","Last Name","Status"};
Object[][] data = {{"John Doe", "John Doe", "John Doe", "Single"},
{"Doe John", "Doe John", "Doe John", "Married"}};
JTable table = new JTable(data, cNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
jScrollPane1 = new JScrollPane(table);
listPahel = new JPanel();
listPahel.setLayout(new BorderLayout());
listPahel.add(jScrollPane1, BorderLayout.CENTER);
add(listPahel);
上面的代码重新显示在上面的第二张图片中
The Above code reuslts into that 2nd image above
如果您有任何见解,请赐教
if you have any insights please do enlighten me
推荐答案
最简单的方法是在JPanel
上设置边框:
The simplest way would be to set a border on your JPanel
:
listPahel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
这篇关于在JFrame中布局JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!