问题
I am trying to get my JTextField (desc) to under the JSpinner, (dayspinner) but I'm not sure how. If I put the text field in SOUTH, it just goes all the way to the bottom of the window. I would also like to implement whatever solution there is to this problem with my beginning menu with the 3 buttons next to each other, I would much rather them be on top of one another.
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:
Don't use BorderLayout, it's not designed to do what you want, consider trying to use something like GridBagLayout instead, which is far more flexible
See How to Use GridBagLayout for more details
来源:https://stackoverflow.com/questions/29786921/how-do-i-make-a-jpanel-item-go-under-another-item-in-the-same-borderlayout