如何将JPanel项目放在同一BorderLayout中的另一个项目下?

时间:2015-04-22 02:57:39

标签: java swing jtextfield layout-manager jspinner

我正在尝试将JTextFielddesc)置于JSpinner下,(dayspinner),但我不确定如何。如果我将文本字段放在SOUTH中,它就会一直到窗口的底部。我也想用我的开始菜单实现这个问题的解决方案,其中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();
  }
}

1 个答案:

答案 0 :(得分:1)

不要使用BorderLayout,它不是为了做你想做的事情而设计的,考虑尝试使用类似GridBagLayout的东西,这样会更加灵活

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