如何使 JPanel 项目位于同一 BorderLayout 中的另一个项目之下?

How do I make a JPanel item go under another item in the same BorderLayout?

我试图让我的 JTextField (desc) 低于 JSpinner, (dayspinner) 但我不确定如何。如果我将文本字段放在 SOUTH 中,它就会一直延伸到 window 的底部。我还想通过我的开始菜单实现这个问题的任何解决方案,其中 3 个按钮彼此相邻,我更希望它们彼此重叠。

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

public class Scheduler {

  JButton VDay, VWeek, Task, Exit;
  JFrame wframe, dframe, tframe;
  JTextField hin, min, desc;
  JLabel time;
  JSpinner dayspinner;

  class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == VDay) {
        dframe.setVisible(true);
      } else if (e.getSource() == VWeek) {
        wframe.setVisible(true);
      } else if (e.getSource() == Task) {
        tframe.setVisible(true);
      } else if (e.getSource() == Exit) {
        System.exit(0);
      }
    }
  }

  public String[] getDayStrings() {
    String[] dayStrings = {
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday",
    };
    return dayStrings;
  }


  public void CreateFrame() {
    JFrame frame = new JFrame("Main Menu");
    frame.setSize(350, 250);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    ButtonListener btnlst = new ButtonListener();
    VDay = new JButton("View Day");
    VWeek = new JButton("View Week");
    Task = new JButton("Assign/Edit Tasks");
    Exit = new JButton("Exit");

    VDay.addActionListener(btnlst);
    VWeek.addActionListener(btnlst);
    Task.addActionListener(btnlst);
    Exit.addActionListener(btnlst);

    JPanel center = new JPanel();
    JPanel south = new JPanel();
    south.add(Exit);
    center.add(Task);
    center.add(VDay);
    center.add(VWeek);
    frame.add(south, BorderLayout.SOUTH);
    frame.add(center, BorderLayout.CENTER);
    frame.setVisible(true);


    dframe = new JFrame("Today's Schedule");
    dframe.setSize(500, 500);
    dframe.setVisible(false);

    wframe = new JFrame("Week's Schedule");
    wframe.setSize(1050, 400);
    wframe.setVisible(false);

    tframe = new JFrame("Edit/Add Tasks");
    tframe.setSize(500, 500);
    tframe.setVisible(false);

    //GUI for tframe
    JPanel tnorth = new JPanel();
    JPanel tcenter = new JPanel();
    JPanel tsouth = new JPanel();
    tframe.add(tnorth, BorderLayout.NORTH);
    tframe.add(tcenter, BorderLayout.CENTER);
    tframe.add(tsouth, BorderLayout.SOUTH);

    //time input
    time = new JLabel("Time (HH:MM): ");
    tnorth.add(time);
    hin = new JTextField(2);
    hin.setColumns(5);
    tnorth.add(hin);
    min = new JTextField(2);
    min.setColumns(5);
    tnorth.add(min);

    //select day
    String[] dayStrings = getDayStrings();
    dayspinner = new JSpinner(new SpinnerListModel(dayStrings));
    dayspinner.setPreferredSize(new Dimension(85, 20));
    tcenter.add(dayspinner);

    //event description
    desc = new JTextField(35);
    desc.setColumns(30);
    tcenter.add(desc);


  }


  public static void main(String[] args) {
    Scheduler scheduler = new Scheduler();
    scheduler.CreateFrame();
  }
}

不要使用 BorderLayout,它不是为你所想而设计的,考虑尝试使用 GridBagLayout 之类的东西,它更灵活

有关详细信息,请参阅 How to Use GridBagLayout