简体   繁体   English

在JFrame中布局JPanel

[英]Layouting JPanel inside 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. 我对Java上的GUI知之甚少,因为我更多的是在后端,而不是在视图方面,所以我遇到了在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. 图像已经说明了,但是当我运行我的代码时我拥有的就是这个。

在此输入图像描述

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. 两端都没有填充,面板正好在Jframe的边框上,我想要纠正,我不希望这种布局,其中面板与Jframe的尺寸相同,在至少在两端会有一些填充物。

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上设置边框:

listPahel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  1. add EmptyBorders to JPanel 将EmptyBorders添加到JPanel

    or 要么

  2. add empty JPanels (four) to the EAST, WEST, NORTH, SOUTH area of JFrame and JPanel with JScrollPane put to the CENTER area, then is created gap in 10pixels around CENTER area 将空JPanels(四个)添加到JFrame和JPanel的EAST,WEST,NORTH,SOUTH区域,将JScrollPane添加到CENTER区域,然后在CENTER区域周围创建10个像素的间隙

    then to 然后到

  3. change LayoutManager for JPanel contains JScrollPane from FlowLayout (default implemented in API) to BorderLayout 更改Layout Manager for JPanel包含从FlowLayout(默认在API中实现)到BorderLayout的JScrollPane

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM