Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使某些按钮执行不同的操作?Java Swing按钮_Java_Swing_Jbutton - Fatal编程技术网

如何使某些按钮执行不同的操作?Java Swing按钮

如何使某些按钮执行不同的操作?Java Swing按钮,java,swing,jbutton,Java,Swing,Jbutton,我正在制作一个GUI,用于计算GPA并返回当前类中所需的GPA,以获得目标GPA。我让用户输入他们以前/现在/将来学习的所有课程,我有几个按钮将这些信息输入到单独的变量中,稍后我将访问这些变量进行计算。但是,我的按钮没有执行任何操作。有人能给我解释一下我的代码有什么问题吗?我尝试了两种不同的方法(都在示例中),但都不起作用 import java.awt.BorderLayout; import java.awt.ComponentOrientation; import java.awt.Con

我正在制作一个GUI,用于计算GPA并返回当前类中所需的GPA,以获得目标GPA。我让用户输入他们以前/现在/将来学习的所有课程,我有几个按钮将这些信息输入到单独的变量中,稍后我将访问这些变量进行计算。但是,我的按钮没有执行任何操作。有人能给我解释一下我的代码有什么问题吗?我尝试了两种不同的方法(都在示例中),但都不起作用

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

.....
导入java.awt.BorderLayout;
导入java.awt.ComponentOrientation;
导入java.awt.Container;
导入java.awt.FlowLayout;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.lang.reflect.Array;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
公共类GUI扩展JFrame实现ActionListener{
私有FlowLayout FlowLayout=新的FlowLayout();
私有网格布局=新网格布局(6,6);
专用JLabel指令;
私有JLabel进入类;
专用JLabel状态1、状态2、状态3、状态4、状态5、状态6;
私有JTextField classinput1、classinput2、classinput3、classinput4、classinput5、classinput6;
私有JTextField hoursinput1、hoursinput2、hoursinput3、hoursinput4、hoursinput5、hoursinput6;
私人JComboxGradeInput1、gradeinput2、gradeinput3、gradeinput4、gradeinput5、gradeinput6;
private JPanel panel2=新的JPanel();
私有JButton add1、add2、add3、add4、add5、add6;
私有JButton remove1,remove2,remove3,remove4,remove5,remove6;
私有字符串[]等级={“选择等级(可选)”,“A”,“A-”,“B+”,“B”,“B-”,“C+”,“C”,“C-”,“D+”,“D-”,“F”};
私人双gpa1、gpa2、gpa3、gpa4、gpa5、gpa6;
私有数组列表矩阵;
私有字符串[]行1=新字符串[3];
私有字符串[]行2=新字符串[3];
私有字符串[]行3=新字符串[3];
私有字符串[]行4=新字符串[3];
私有字符串[]行5=新字符串[3];
私有字符串[]行6=新字符串[3];
公共GUI(字符串名称){
超级(姓名);
}
公共void addComponentsToPanel(容器窗格){
JPanel panel1=新的JPanel();
面板1.设置布局(流程布局);
flowlayout.setAlignment(flowlayout.LEADING);
说明=新的JLabel(“在下面输入正确的信息”);
小组1.添加(说明);
面板2.设置布局(网格布局);
//创建第1行
addUserInput(classinput1,小时Input1);
添加等级(等级输入1);
addButton(add1,“第一”);
移除按钮(移除1,“第二”);
addStatus(状态1);
//创建第2行
addUserInput(classinput2,hoursinput2);
添加等级(等级输入2);
添加按钮(添加2,“第三”);
移除按钮(移除2,“第四”);
addStatus(状态2);
//创建第3行
添加用户输入(classinput3,小时输入3);
添加等级(等级输入3);
添加按钮(添加3,“第五”);
移除按钮(移除3,“第六”);
addStatus(状态3);
.........
私有静态void createAndShowGUI(){
GUI框架=新GUI(“GPA计算器和规划器设计”);
框架。设置尺寸(300300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPanel(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public void addUserInput(JTextField名称,JTextField credithours){
name=新JTextField(“课程名称(可选)”;
credithours=新的JTextField(“学分小时”);
第2组:添加(姓名);
小组2.增加(学分小时);
}
公共void addGrades(JComboBox选项){
选项=新JComboBox(等级);
小组2.添加(选项);
}
公共双级(字符串级){
字符串[]等级={“选择等级(可选)”,“A”,“A-”,“B+”,“B”,“B-”,“C+”,“C”,“C-”,“D+”,“D-”,“F”};
int指数=0;
对于(int i=0;i<13;i++){
如果(等级等于(等级[i])){
指数=i;
}
}
双[]等级转换={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};
返回等级转换[索引];
}
公共无效添加按钮(JButton b,字符串s){
b=新按钮(“添加”);
b、 setActionCommand(多个);
b、 addActionListener(此);
第2组:添加(b);
}
公共无效删除按钮(JButton b,字符串s){
b=新的按钮(“移除”);
b、 setActionCommand(多个);
b、 addActionListener(此);
第2组:添加(b);
}
公共无效添加状态(JLabel状态){
状态=新的JLabel(“状态:-”);
第2组:添加(状态);
}
public void addClasses(){
}
公共静态void main(字符串[]args){
javax.swing.SwingUtilities.invokeLater(新的Runnable(){
public void run(){
createAndShowGUI();
}
});
}
@凌驾
已执行的公共无效操作(操作事件e){
//TODO自动生成的方法存根
如果(例如getSource()==add1){
行1[0]=classinput1.getText();
行1[1]=小时输入1.getText();
行1[2]=(字符串)gradeinput1.getSelectedItem();
status1.setText(“状态:已添加”);
}
如果(如getActionCommand().equals(“第三”)){
行2[0]=classinput1.getText();
行2[1]=小时输入1.getText();
行2[2]=(字符串)gradeinput1.getSelectedItem();
status2.setText(“状态:已添加”);
}
.....

您的
操作已执行
方法
if(e.getSource()==add1)
ne
public void addButton(JButton b, String s) {
    b.setText("ADD");
    b.setActionCommand(s);
    b.addActionListener(this);
    panel2.add(b);
}