如何在JPanel的顶部和底部安排JComponents

时间:2017-07-24 14:12:26

标签: java swing

我的计划中有三个JButton,两个JLabel和一个JTextField以及JPanel FlowLayout。我想在JPanel的开头安排三个按钮,其余部分安排在JPanel 的末尾。我需要三个按钮和最后三个组件之间的空间,但代码不起作用。任何人都可以帮助我吗?

这是代码:

JPanel panelMenu=new Jpanel(new FlowLayout())
panelMenu.add ( btnOpen );
panelMenu.add ( lblPage );
panelMenu.add ( txtCurrentPage );
panelMenu.add ( lblTotalPage );
panelMenu.add ( btnBackword );
panelMenu.add ( btnNext );
getContentPanel.add(panelMenu);

2 个答案:

答案 0 :(得分:2)

FlowLayout类将组件放在一行中。要获得所需内容,可以使用GridBagLayout

示例代码:

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LayoutExamle {
    JFrame frame;
    JPanel panel;
    JButton b1, b2, b3;
    JLabel l1, l2;
    JTextField t1;

    GridBagConstraints constraints;

    public LayoutExamle(){
        initComp();
    }

    public void initComp(){
        frame = new JFrame("Example");
        frame.setLayout(new CardLayout());

        panel = new JPanel();

        panel.setSize(400, 350);
        panel.setLayout(new GridBagLayout());
        panel.setBackground(Color.WHITE);

        constraints = new GridBagConstraints();

        b1 = new JButton("<<");
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(12, 43, 0, 0);
        panel.add(b1, constraints);

        b2 = new JButton("Open");
        constraints.gridx = 2;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(12, 18, 0, 0);
        panel.add(b2, constraints);

        b3 = new JButton(">>");
        constraints.gridx = 4;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(12, 12, 0, 0);
        panel.add(b3, constraints);

        l1 = new JLabel("jLabel1");
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(234, 43, 12, 0);
        panel.add(l1, constraints);

        l2 = new JLabel("jLabel1");
        l2.setText("jLabel2");
        constraints.gridx = 2;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(234, 18, 12, 0);
        panel.add(l2, constraints);

        t1 = new JTextField();
        constraints.gridx = 4;
        constraints.gridy = 1;
        constraints.gridwidth = 2;
        constraints.ipadx = 130;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.insets = new Insets(230, 12, 12, 31);
        panel.add(t1, constraints);

        frame.add(panel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new LayoutExamle();
    }
}

截图:

enter image description here

答案 1 :(得分:1)

一种方法是创建两个面板。然后,您可以将面板添加到框架的内容窗格中,默认情况下使用BorderLayout:

frame.add(topPanel, BorderLayout.PAGE_START);
frame.add(bottomPanel, BorderLayout.PAGE_END);