如何在Panel上垂直堆叠文本字段?

时间:2017-10-19 21:43:03

标签: java user-interface layout jframe panel

我正在研究小GUI GPA计算程序。我设置了我的按钮,一旦点击它,一些文本字段和组合框就会被添加到一个面板中,但每次点击它时,所有附加组件都会在一行中水平放置。有没有办法操纵布局,以便新的附加组件可以在现有的下面开始一个新的线? THX!

这是我的代码:

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)