JFrame 布局 helping/guide,

JFrame layout helping/guide,

我的 JFRAME 布局有问题 布局对我来说非常困难,因为我刚刚开始学习它。 我正在寻找的布局是
select 数据开始
天:"textField"
月份:"textField"
年份:"textField"
结束日期
天:"textField"
月份:"textField"
年份:"textField"

好像在最后阶段,为什么 top 没有工作

下面的代码显示了上图

import javax.swing.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;

public class TopUpHistoryScreen extends JDialog {

    private JPanel mainPanel;
    private JTextArea historyScreen;    

    public TopUpHistoryScreen()
    {
        setPanels();

        setModalityType(ModalityType.APPLICATION_MODAL);
        setSize(600, 600);
        setVisible(true);
    }
    public void setPanels()
    {
        mainPanel = new JPanel(new GridLayout(0, 2));
        JPanel containerPanel = new JPanel(new GridLayout(0, 1));
        JPanel lowerPanel = new JPanel(new FlowLayout());
        //JButton apply = new JButton("Select data area");
        JButton exit = new JButton("Okay!");
        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                dispose();
            }
        });
        JButton checkDate = new JButton("check dates");

        JLabel SelectData = new JLabel("Select data area\n");
        JLabel START = new JLabel("START DATE!");
        JLabel startDay = new JLabel("Day:");
        JTextField sDay = new JTextField();

        JLabel startMonth = new JLabel("Month:");
        JTextField sMonth = new JTextField();

        JLabel startYear = new JLabel("Year:");
        JTextField sYear = new JTextField();

        JLabel END = new JLabel("END DATE!");

        JLabel endDay = new JLabel("Day:");
        JTextField eDay = new JTextField();

        JLabel endMonth = new JLabel("Month:");
        JTextField eMonth = new JTextField();

        JLabel endYear = new JLabel("Year:");
        JTextField eYear = new JTextField();

        JTextField Data = new JTextField();
        JTextField touchOnTimeFieldminute = new JTextField();

        historyScreen = new JTextArea(5,30);
        JScrollPane scrolll = new JScrollPane(historyScreen);

        mainPanel.add(SelectData);
        mainPanel.add(START);
        mainPanel.add(startDay);
        mainPanel.add(sDay);
        mainPanel.add(startMonth);
        mainPanel.add(startYear);
        mainPanel.add(sYear);
        mainPanel.add(END);
        mainPanel.add(endDay);
        mainPanel.add(eDay);
        mainPanel.add(endMonth);
        mainPanel.add(eMonth);
        mainPanel.add(endYear);
        mainPanel.add(eYear);
        mainPanel.add(checkDate);
        //mainPanel.add(touchOnTimeFieldhour);
        //mainPanel.add(SelectData);
        //mainPanel.add(touchOnTimeFieldminute);
        //mainPanel.add(touchOnTimem);
        lowerPanel.add(scrolll);
        lowerPanel.add(exit);
        //lowerPanel.add(apply);
        //touchOnTimeFieldhour.setSize(10,10);
        containerPanel.add(mainPanel);
        containerPanel.add(lowerPanel);

        add(containerPanel);
    }    
}

setPanels() 方法中的 mainPanel.add(component); 序列更改为以下内容..

    mainPanel.add(SelectData);
    mainPanel.add(START);
    mainPanel.add(startDay);
    mainPanel.add(sDay);
    mainPanel.add(startMonth);
    mainPanel.add(sMonth);
    mainPanel.add(startYear);
    mainPanel.add(sYear);
    mainPanel.add(END);
    mainPanel.add(new JLabel());
    mainPanel.add(endDay);
    mainPanel.add(eDay);
    mainPanel.add(endMonth);
    mainPanel.add(eMonth);
    mainPanel.add(endYear);
    mainPanel.add(eYear);
    mainPanel.add(checkDate);

它会起作用..当你使用 GridViewmainPanel 时,元素将根据你添加到 mainPanel 的顺序获得位置...