大家好,我正在为我的一个软件类制作一个类调度程序,而且在允许用户调整窗口大小时我遇到了问题。每当用户调整大小时,一切的布局都会发生变化。例如,当用户使窗口变大时,文本移动到窗口中的不同位置。我该如何预防?
这是我的代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class FirstGUI extends JFrame {
private JTextArea textAreaMon;
private JTextArea textAreaTue;
private JTextArea textAreaWed;
private JTextArea textAreaThu;
private JTextArea textAreaFri;
private JTextArea description;
private Container pane;
public FirstGUI() {
// create the frame
JFrame frame = new JFrame("Final Exam Scheduler");
frame.setSize(900, 900);
// closing frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.getContentPane().setBackground(Color.WHITE);
JButton openButton = new JButton("Open File");
frame.add(openButton);
// textField = new JTextField(20);
textAreaMon = new JTextArea(5, 20);
textAreaTue = new JTextArea(5, 20);
textAreaWed = new JTextArea(5, 20);
textAreaThu = new JTextArea(5, 20);
textAreaFri = new JTextArea(5, 20);
description = new JTextArea(5,20);
textAreaMon.setEditable(false);
textAreaTue.setEditable(false);
textAreaWed.setEditable(false);
textAreaThu.setEditable(false);
textAreaFri.setEditable(false);
description.setEditable(false);
// frame.add(textField);
frame.add(description);
frame.add(textAreaMon);
frame.add(textAreaTue);
frame.add(textAreaWed);
frame.add(textAreaThu);
frame.add(textAreaFri);
String gE1 = "Group Exam 1";
String gE2 = "Group Exam 2";
String gE3 = "Group Exam 3";
String gE4 = "Group Exam 4";
String gE5 = "Group Exam 5";
String gE6 = "Group Exam 6";
String gE7 = "Group Exam 7";
String gE8 = "Group Exam 8";
String gE9 = "Group Exam 9";
textAreaMon.setText(
"Monday \n \n 8:00 - 10:00:\t" + gE1 + " \n 10:15-12:15:\t" + gE2 + " \n 12:45 - 2:45:\t\n 3:00 - 5:00:\t\n 5:30 - 7:30:\t" + gE3 + " \n 7:45 - 9:45:\t\n \n");
textAreaTue.setText(
"Tuesday \n \n 8:00 - 10:00:\t\n 10:15-12:15 :\t" + gE4 + " \n 12:45 - 2:45:\t\n 3:00 - 5:00:\t\n 5:30 - 7:30:\t\n 7:45 - 9:45:\t\n \n");
textAreaWed.setText(
"Wednesday \n \n 8:00 - 10:00:\t\n 10:15-12:15:\t\n 12:45 - 2:45 :\t" + gE5 + " \n 3:00 - 5:00:\t\n 5:30 - 7:30:\t\n 7:45 - 9:45:\t\n \n");
textAreaThu.setText(
"Thursday \n \n 8:00 - 10:00:\t\n 10:15-12:15 :\t" + gE6 + " \n 12:45 - 2:45:\t\n 3:00 - 5:00:\t\n 5:30 - 7:30:\t" + gE7 + " \n 7:45 - 9:45:\t\n \n");
textAreaFri.setText("Friday \n \n 8:00 - 10:00:\t" + gE8 + " \n 10:15-12:15:\t\n 12:45 - 2:45:\t" + gE9);
description.setText("Click the open button to import your .csv file into the program. Your schedule and times will be listed in the schedule below.");
//frame.getContentPane().add(openButton, BorderLayout.CENTER);
// attach the fileOpener to the "Open" button
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
new InterfacePanel();
}
});
// show it
frame.setVisible(true);
}
public static void main(String[] args) {
new FirstGUI();
}
}