使用switch语句为jbuttons java提供动作

时间:2014-07-12 08:49:08

标签: java swing switch-statement jbutton

我正在尝试使用switch来触发按钮的声明。我已经编写了frame1来打开第二类的JFrame。在第二类中有四个按钮可用于Switch语句,我使用第一类中的变量作为Switch的选项,但我得到一个空指针异常。为什么这样?

Class1(BasicInfo.java)

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    name= n.getText();
    email=e.getText();
    street=s.getText();
    state=st.getText();
    city=c.getText();
    gender=(String)g.getSelectedItem();
    qualification=(String)q.getSelectedItem();
    date=Integer.parseInt((String)d.getSelectedItem());
    month=Integer.parseInt((String)m.getSelectedItem());
    year=Integer.parseInt(y.getText());
    phone_num=Integer.parseInt(nm.getText());
    //this.dispose();
                    new ViewSample().setVisible(true);
    }

Class 2(ViewSample.java)

void open()
{
    BasicInfo x= new BasicInfo();
    System.out.print(x.qualification);
    switch(x.qualification)
    {
        case "BE":
            System.out.print("a");
            break;
            case "MBA":
            System.out.print("b");
                break;
            default:
    }
}

private void s1ActionPerformed(java.awt.event.ActionEvent evt) {
    open();
}

private void s4ActionPerformed(java.awt.event.ActionEvent evt) {
    open();
}

private void s2ActionPerformed(java.awt.event.ActionEvent evt) {
    open();
}

private void s3ActionPerformed(java.awt.event.ActionEvent evt) {
    open();
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
    this.dispose();
}

1 个答案:

答案 0 :(得分:1)

正如我在评论中所指出的那样,我认为做了类似的事情,如下面的代码示例所示:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PassingValuesExample {

    private String qualification;
    private JTextField qualificationField;
    private JButton submitButton;

    private void displayGUI() {
        JFrame frame = new JFrame("Swing Worker Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        qualificationField = new JTextField(10);
        submitButton = new JButton("Submit");
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                submitButtonActionPerformed(ae);
            }
        });
        contentPane.add(qualificationField);
        contentPane.add(submitButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void submitButtonActionPerformed(ActionEvent ae) {
        qualification = qualificationField.getText();
        new Foo(this).open();
    }

    public String getQualification() {
        return qualification;
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new PassingValuesExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

class Foo {

    private PassingValuesExample pve;

    public Foo(PassingValuesExample pve) {
        this.pve = pve;
    }

    public void open() {
        String qual = pve.getQualification();
        switch(qual) {
            case "CA":
            case "MBA":
                System.out.println("Qualification: " + qual);
        }
    }
}