0
我正在研究GUI GUI GPA計算程序。我設置了我的按鈕,這樣一旦它被點擊,一些文本字段和組合框將被添加到面板中,但每次點擊時,所有附加組件都以單行水平排列。有沒有一種方法來操縱佈局,以便新的附加組件可以在現有組件下面新建一行?謝謝!如何在面板上垂直堆疊文本字段?
這裏是我的代碼:
public class gpaCalculator extends JFrame {
ArrayList<Class> allClasses = new ArrayList<Class>();
ArrayList<Class> classesTaken = new ArrayList<Class>();
ArrayList<Class> classesTaking = new ArrayList<Class>();
ArrayList<Class> classesWillTake = new ArrayList<Class>();
private JFrame frame;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JButton addNewCourse;
private JButton finishedCalculate;
private JButton showClassesEntered;
private JButton removeAllClass;
/**
* Courses
**/
public gpaCalculator() {
frame = new JFrame("GPA Calculator");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int height = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
int width = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
this.setSize(width, height);
this.setLayout(new FlowLayout(8));
panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
addNewCourse = new JButton("Add a new course!");
addNewCourse.addActionListener(new ButtonListener());
finishedCalculate = new JButton("Calculate my GPA & Enter target GPA");
finishedCalculate.addActionListener(new ButtonListener2());
showClassesEntered = new JButton("Display classes entered");
showClassesEntered.addActionListener(new ButtonListener3());
removeAllClass = new JButton("Remove all entered classes");
removeAllClass.addActionListener(new ButtonListener4());
this.add(addNewCourse);
this.add(finishedCalculate);
this.add(showClassesEntered);
this.add(removeAllClass);
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent args) {
String[] courseCreditHourCombo = {"1", "2", "3", "4"};
String[] courseGradeCombo = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D", "E", "F", "U"};
String[] courseTypeCombo = {"Have taken", "Currently taking", "Will be taking"};
JLabel creditHourLabel = new JLabel("Course credit houe");
JLabel nameLabel = new JLabel("Course name: ");
JLabel calculatedGPALabel = new JLabel("Calculated GPA");
JLabel courseTypeLabel = new JLabel("Course status");
JLabel gradeLabel = new JLabel("Course grade ");
JComboBox courseCreditHour = new JComboBox(courseCreditHourCombo);
JTextField courseName = new JTextField(10);
courseName.setEditable(true);
JComboBox courseGrade = new JComboBox(courseGradeCombo);
JComboBox courseType = new JComboBox(courseTypeCombo);
JTextField calculatedGPA = new JTextField(10);
panel1.add(nameLabel);
panel1.add(courseName);
panel1.add(creditHourLabel);
panel1.add(courseCreditHour);
panel1.add(courseTypeLabel);
panel1.add(courseType);
panel1.add(gradeLabel);
panel1.add(courseGrade);
int classCredit = (Integer) courseCreditHour.getEditor().getItem();
String className = courseName.getText();
String classGrade = (String) courseGrade.getEditor().getItem();
String classType = (String) courseType.getEditor().getItem();
Class theClass = new Class(className, classGrade, classType, classCredit);
allClasses.add(theClass);
if (classType.equals("Have taken")) {
Class newClass = new Class(className, classGrade, classType, classCredit);
classesTaken.add(newClass);
} else if (classType.equals("Currently taking")) {
Class newClass = new Class(className, classType, classCredit);
classesTaking.add(newClass);
} else {
Class newClass = new Class(className, classType, classCredit);
classesWillTake.add(newClass);
}
}
}
private class ButtonListener2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
panel2.removeAll();
JLabel currentGPALabel = new JLabel("Current GPA: ");
JLabel currentGPA = new JLabel();
JLabel targetGPALabel = new JLabel("Target GPA: ");
JTextField targetGPA = new JTextField(10);
targetGPA.setEditable(true);
panel2.setSize(200, 200);
panel2.add(currentGPALabel);
panel2.add(currentGPA);
panel2.add(targetGPALabel);
panel2.add(targetGPA);
double counter = 0;
for (Class c : classesTaken) {
counter = counter + c.getWeightedClassGPA();
}
double currentGPADouble = counter/(classesTaken.size() + 1);
currentGPA.setText(String.valueOf(currentGPADouble));
//System.out.println(currentGPADouble);
}
}
private class ButtonListener3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
panel3.removeAll();
JLabel classesEntered = new JLabel("Courses entered: ");
panel3.setSize(200, 200);
panel3.add(classesEntered);
classesEntered.setText("Classes entered: " + allClasses);
}
}
private class ButtonListener4 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
allClasses.clear();
}
}
}
'GridLayout'或' – MadProgrammer
你需要做Java佈局管理器的一些研究GridBagLayout'將是我的第一個想法。有很多問題和答案可以幫助你現在知道要查找什麼;-) – John3136
如果這是一個JavaFX應用程序,使用VBox來包含你的字段。如果這是一個Swing應用程序,請使用'JTextField.setBounds()'或[WindowBuilder](https://www.eclipse.org/windowbuilder/)插件 –