简体   繁体   中英

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. 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.

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

    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

    then to

  3. change LayoutManager for JPanel contains JScrollPane from FlowLayout (default implemented in API) to BorderLayout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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