2017-10-19 17 views
0

私は小さなGUI GPA計算プログラムに取り組んでいます。私は自分のボタンを設定して、一度それがいくつかのテキストフィールドをクリックし、コンボボックスがパネルに追加されるようにしましたが、それがクリックされるたびに、すべてのアドオンが1行で水平になりました。新しいアドオンが既存のものの下に新しい行を開始するようにレイアウトを操作する方法はありますか?どうも!ここでパネルにテキストフィールドを縦に積み重ねる方法は?

は私のコードです:

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

'GridLayout'または' GridBagLayout'は、Javaレイアウトマネージャ上のいくつかの研究を行う必要があり – MadProgrammer

+2

私の最初の考えだろう。 – John3136

+0

これはJavaFXアプリケーションの場合は、フィールドを格納するために 'VBox'を使います。これがSwingアプリケーションの場合は、 'JTextField.setBounds()'または[WindowBuilder](https://www.eclipse.org/windowbuilder/)プラグインを使用してください –

答えて

関連する問題