gpt4 book ai didi

java - 使用 switch 语句为 jbuttons java 提供操作

转载 作者:行者123 更新时间:2023-12-01 12:45:54 25 4
gpt4 key购买 nike

我正在尝试使用 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);
}

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

最佳答案

正如我的评论中所指出的,我认为应该做类似的事情,如以下代码示例所示:

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

关于java - 使用 switch 语句为 jbuttons java 提供操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24711264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com