GUI JPanel&框架布局

时间:2017-05-26 19:17:37

标签: java swing user-interface jpanel layout-manager

我想问一些关于Java GUI的问题,尤其是关于JPanel语法的问题。我真的很困惑如何准确配置面板,尝试使用BorderLayoutGridLayout,但它仍然不符合我的要求。

我想制作如下表格:

当前结果:

从图像中,我需要配置2点

  1. 输入面板和按钮面板之间的填充/边距
  2. tablePanel和inputPanel之间的填充/边距
  3. 代码

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.border.BevelBorder;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.LineBorder;
    import javax.swing.table.DefaultTableModel;
    
    
    public class manageForm extends JFrame {
        //Vector<String> header=new Vector<String>();
        //Vector<Vector<String>>data=new Vector<Vector<String>>();
    
        DefaultTableModel defaultTableModel;
        JTable table;
        JScrollPane scrollPane;
    
        JLabel titleLabel=new JLabel("Manage Handphone");
    
        JTable hpTable = new JTable();
    
        JLabel idLabel = new JLabel("ID");
        JLabel nameLabel = new JLabel("Name");
        JLabel priceLabel = new JLabel("Price");
        JLabel weightLabel = new JLabel("Weight");
        JLabel cableLabel = new JLabel("Cable Length");
        JLabel typeLabel = new JLabel("Type");
    
        JTextField idtxt = new JTextField();
        JTextField nametxt = new JTextField();
        JTextField pricetxt = new JTextField();
        JTextField weighttxt = new JTextField();
        JTextField cabletxt = new JTextField();
    
        JComboBox typeBox = new JComboBox();
    
        JButton insertBtn = new JButton("INSERT");
        JButton updateBtn = new JButton("UPDATE");
        JButton deleteBtn = new JButton("DELETE");
        JButton confirmBtn = new JButton("CONFIRM");
        JButton cancelBtn = new JButton("CANCEL");
    
        String header[] = {"ID","Name","Type","Price","Weight","Cable Length"};
        String data[][] = {
                {"2","Bose Quite","In-Ear","Price","Weight","Cable Length"},
                {"2","Bose Quite","In-Ear","Price","Weight","Cable Length"},
                {"2","Bose Quite","In-Ear","Price","Weight","Cable Length"}
    
        };
    
    
        public manageForm() {
    
            JPanel headerPanel = new JPanel();
            JPanel tablePanel = new JPanel();
            tablePanel.setBorder(new EmptyBorder(0,0,0,0));
            JPanel inputPanel = new JPanel(new GridLayout(6,2));
            inputPanel.setBorder(new EmptyBorder(20,10,20,10));
            //inputPanel.setBorder(new LineBorder(Color.black));
            JPanel buttonPanel = new JPanel(new GridLayout(1,5));
            buttonPanel.setBorder(new EmptyBorder(100,20,100,20));
            JPanel footerPanel = new JPanel(new GridLayout (2,1,0,0));
    
    
            headerPanel.add(titleLabel);
    
            inputPanel.add(idLabel);
            inputPanel.add(idtxt);
            inputPanel.add(nameLabel);
            inputPanel.add(nametxt);
            inputPanel.add(priceLabel);
            inputPanel.add(pricetxt);
            inputPanel.add(weightLabel);
            inputPanel.add(weighttxt);
            inputPanel.add(cableLabel);
            inputPanel.add(cabletxt);
            inputPanel.add(typeLabel);
            inputPanel.add(typeBox);
    
            buttonPanel.add(confirmBtn);
            buttonPanel.add(cancelBtn);
            buttonPanel.add(insertBtn);
            buttonPanel.add(updateBtn);
            buttonPanel.add(deleteBtn);
    
    
            footerPanel.add(inputPanel);
            footerPanel.add(buttonPanel);
    
    /*
            JPanel panel0=new JPanel();
            JPanel panel1=new JPanel();
            JPanel panel2=new JPanel();
            JPanel panel3=new JPanel();
            JPanel panel4=new JPanel();
            JPanel panel5=new JPanel();
            JPanel panel6 = new JPanel();
            JPanel panel7 = new JPanel();
            JPanel panel8 = new JPanel();
    
    
    
            panel1.setLayout(new GridLayout(1, 6));
    
    
    
            panel2.setLayout(new GridLayout(1, 2));
            panel2.add(idLabel);
            panel2.add(idtxt);
    
            panel3.setLayout(new GridLayout(1, 2));
            panel3.add(nameLabel);
            panel3.add(nametxt);
    
            panel4.setLayout(new GridLayout(1, 2));
            panel4.add(priceLabel);
            panel4.add(pricetxt);
    
            panel5.setLayout(new GridLayout(1, 2));
            panel5.add(weightLabel);
            panel5.add(weighttxt);
    
            panel6.setLayout(new GridLayout(1, 2));
            panel6.add(cableLabel);
            panel6.add(cabletxt);
    
            panel7.setLayout(new GridLayout(1, 2));
            panel7.add(typeLabel);
            panel7.add(typeBox);
    
            panel8.setLayout(new GridLayout(1, 5));
    
    
            mainPanel.add(panel0);
            mainPanel.add(panel1);
            mainPanel.add(panel2);
            mainPanel.add(panel3);
            mainPanel.add(panel4);
            mainPanel.add(panel5);
            mainPanel.add(panel6);
            mainPanel.add(panel7);
            mainPanel.add(panel8);
        */  
    
            /*
            header.add("ID");
            header.add("Name");
            header.add("Type");
            header.add("Price");
            header.add("Weight");
            header.add("Cable Length");
            */
    
            defaultTableModel=new DefaultTableModel(data, header);
            hpTable =new JTable(defaultTableModel);
            scrollPane=new JScrollPane(hpTable);
    //      tablePanel.add(scrollPane);
    
    
            setLayout(new BorderLayout(0,5));
            setTitle("Manage Form");
            setSize(800,600);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
            add(headerPanel,BorderLayout.NORTH);
            add(scrollPane,BorderLayout.CENTER);
            add(footerPanel,BorderLayout.SOUTH);
            pack();
        }
    
    
        public static void main(String[] args) {
        new manageForm();   
        }
    
    }
    

    如何精确管理面板布局以满足我们的要求?

    我尝试了EmptyBorder,但结果是,它只在另一个面板之间留出了很大的空间。

2 个答案:

答案 0 :(得分:2)

使用GridBagLayout它是强大的,最适合您的要求。它以水平和垂直的方式排列组件。

您可以使用单元格和行将组件放入网格中。举个例子:

enter image description here

了解详情 - GridBagLayout

答案 1 :(得分:1)

强烈反对使用GridBagLayout的建议。对于任何Swing程序员来说,该布局基本上都是punchline

如果您要编写Swing编程,请自己做一件大事并使用MiGLayout。这是Swing应该内置的布局管理器。