使用switch语句向jbuttons java提供动作 [英] using switch statement to provide action to jbuttons java

查看:69
本文介绍了使用switch语句向jbuttons java提供动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用switch触发按钮的语句.我已将frame1编程为打开第二类的JFrame.在第二类中,有四个按钮可用于Switch语句,并且我正在使用第一类中的变量作为Switch的选择,但我遇到了空指针异常.为什么这样?

I'm trying to use switch for triggering a button's statement. I've programmed frame1 to open JFrame of 2nd class. In 2nd class there are four buttons which will work on Switch statement and I'm using a variable from 1st class to work as choice for Switch, but I'm getting a null pointer exception. Why so?

Class1(BasicInfo.java)

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

第2类(ViewSample.java)

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

推荐答案

正如我的评论中所指出的,我认为应执行类似的操作,如以下代码示例所示:

As noted in my comments, I was of the opinion of doing something like, as shown in this code example:

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

这篇关于使用switch语句向jbuttons java提供动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆