2017-10-19 91 views
0

我正在制作一个GUI来计算GPA并返回当前类中所需的GPA以获得目标GPA。我有用户输入他们所需的所有先前/当前/将来的类,并且我有几个按钮将这些信息输入到单独的变量中,以后我将对其进行访问以进行计算。但是,我的按钮都没有执行任何操作。有人可以向我解释我的代码可能有什么问题吗?我已经尝试了两种不同的方法(在这个例子中都有提到),而且他们都没有工作。如何让某些按钮执行不同的操作? Java Swing JButton

import java.awt.BorderLayout; 
import java.awt.ComponentOrientation; 
import java.awt.Container; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.lang.reflect.Array; 
import java.util.ArrayList; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class GUI extends JFrame implements ActionListener { 

    private FlowLayout flowlayout = new FlowLayout(); 
    private GridLayout gridlayout = new GridLayout(6, 6); 
    private JLabel instructions; 
    private JLabel enteredclasses; 
    private JLabel status1, status2, status3, status4, status5, status6; 
    private JTextField classinput1, classinput2, classinput3, classinput4, classinput5, classinput6; 
    private JTextField hoursinput1, hoursinput2, hoursinput3, hoursinput4, hoursinput5, hoursinput6; 
    private JComboBox<String> gradeinput1, gradeinput2, gradeinput3, gradeinput4, gradeinput5, gradeinput6; 
    private JPanel panel2 = new JPanel(); 
    private JButton add1, add2, add3, add4, add5, add6; 
    private JButton remove1, remove2, remove3, remove4, remove5, remove6; 
    private String[] grades = {"Select Grade (optional)", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"}; 
    private double gpa1, gpa2, gpa3, gpa4, gpa5, gpa6; 
    private ArrayList<Array> matrix; 
    private String[] row1 = new String[3]; 
    private String[] row2 = new String[3]; 
    private String[] row3 = new String[3]; 
    private String[] row4 = new String[3]; 
    private String[] row5 = new String[3]; 
    private String[] row6 = new String[3]; 

    public GUI(String name) { 
     super(name); 
    } 

    public void addComponentsToPanel(Container pane) { 
     JPanel panel1 = new JPanel(); 
     panel1.setLayout(flowlayout); 
     flowlayout.setAlignment(FlowLayout.LEADING); 

     instructions = new JLabel("Input the correct information below."); 
     panel1.add(instructions); 

     panel2.setLayout(gridlayout); 

     //creates row 1 
     addUserInput(classinput1, hoursinput1); 
     addGrades(gradeinput1); 
     addButton(add1, "first"); 
     removeButton(remove1, "second"); 
     addStatus(status1); 

     //creates row 2 
     addUserInput(classinput2, hoursinput2); 
     addGrades(gradeinput2); 
     addButton(add2, "third"); 
     removeButton(remove2, "fourth"); 
     addStatus(status2); 

     //creates row 3 
     addUserInput(classinput3, hoursinput3); 
     addGrades(gradeinput3); 
     addButton(add3, "fifth"); 
     removeButton(remove3, "sixth"); 
     addStatus(status3); 

......... 

    private static void createAndShowGUI() { 
     GUI frame = new GUI("GPA Calculator and Planner Design"); 
     frame.setSize(300, 300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.addComponentsToPanel(frame.getContentPane()); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public void addUserInput(JTextField name, JTextField credithours) { 
     name = new JTextField("Course Name (optional)"); 
     credithours = new JTextField("Credit Hours"); 

     panel2.add(name); 
     panel2.add(credithours); 
    } 

    public void addGrades(JComboBox<String> choices) { 
     choices = new JComboBox<String>(grades); 
     panel2.add(choices); 
    } 

    public double convertGrade(String grade) { 
     String[] grades = {"Select Grade (optional)", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F"}; 

     int index = 0; 
     for (int i = 0; i < 13; i++) { 
      if (grade.equals(grades[i])) { 
       index = i; 
      } 
     } 
     double[] gradeconversions = {0.0 , 4.0, 3.67, 3.3, 3.0, 2.67, 2.33, 2.0, 1.67, 1.33, 1.0, .67, 0.0}; 
     return gradeconversions[index]; 
    } 

    public void addButton(JButton b, String s) { 
     b = new JButton("ADD"); 
     b.setActionCommand(s); 
     b.addActionListener(this); 
     panel2.add(b); 
    } 

    public void removeButton(JButton b, String s) { 
     b = new JButton("REMOVE"); 
     b.setActionCommand(s); 
     b.addActionListener(this); 
     panel2.add(b); 
    } 

    public void addStatus(JLabel status) { 
     status = new JLabel("Status: -"); 
     panel2.add(status); 
    } 

    public void addClasses() { 

    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
       public void run() {  
        createAndShowGUI(); 
       } 
      }); 
    } 



    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

     if (e.getSource() == add1) { 
      row1[0] = classinput1.getText(); 
      row1[1] = hoursinput1.getText(); 
      row1[2] = (String) gradeinput1.getSelectedItem(); 
      status1.setText("Status: Added"); 
     } 

     if (e.getActionCommand().equals("third")) { 
      row2[0] = classinput1.getText(); 
      row2[1] = hoursinput1.getText(); 
      row2[2] = (String) gradeinput1.getSelectedItem(); 
      status2.setText("Status: Added"); 
     } 

..... 
+0

这与IDE无关。不要添加标签。 –

回答

2

actionPerformed方法if (e.getSource() == add1)永远不会成为true因为JButton add1总是null,因为addButton()方法不会初始化变量add1

因此,你必须声明JButton add1 = new JButton();为类变量和更改addButton方法如下

public void addButton(JButton b, String s) { 
    b.setText("ADD"); 
    b.setActionCommand(s); 
    b.addActionListener(this); 
    panel2.add(b); 
} 

将相同的ch也可以使用removeButton方法

+0

我做了这些改变,但按钮仍然没有执行任何动作 –

+0

首先检查你的'actionPerformed'方法是否正常(使用'system.out.println()')并检查'add1'变量不是'null'。那么你的按钮动作肯定应该工作 – aKilleR

相关问题