Java 向JPane添加操作侦听器或if语句

Java 向JPane添加操作侦听器或if语句,java,Java,我正在尝试为一个与工作相关的项目创建GUI,但遇到了一些问题 我希望我的GUI有一个JTextField和三个按钮。我希望用户能够在文本字段中键入特定的数字,然后根据他们单击的按钮执行特定的操作 我遇到的问题是我的ActionListener和JTextField似乎不起作用。当我测试它时,我没有得到任何结果。任何帮助都将不胜感激,以下是我的代码 package nacha; import java.awt.Component; import java.awt.event.ActionEven

我正在尝试为一个与工作相关的项目创建GUI,但遇到了一些问题

我希望我的GUI有一个JTextField和三个按钮。我希望用户能够在文本字段中键入特定的数字,然后根据他们单击的按钮执行特定的操作

我遇到的问题是我的ActionListener和JTextField似乎不起作用。当我测试它时,我没有得到任何结果。任何帮助都将不胜感激,以下是我的代码

package nacha;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Testing 

{
    static  String code = null;
    public static void main(String args[]){

        int sixBatch1TotalCounter=1;
        int sixBatch1Total=2;
        int main = 1000001;




        final JTextField reasonCode = new JTextField(10);

        JPanel p = new JPanel();


p.add(new JLabel("<html>" +
                "Entry Detail: "+
                "<br>"+main+
                "<br>Entry Detail "+sixBatch1TotalCounter+" of "+sixBatch1Total+
                "<br><br>Please type 1-21 to apply reason code and addenda record to the entry detail record."+
                "<br>To omit displayed entry detail from the return, simply leave the input line blank and press enter."+
                "<br><br>Reason Code Descriptions:"+
                "<br>R01 - Insufficient Funds"+
                "<br>R02 - Account Closed"+
                "<br>R03 - No Account"+
                "<br>R04 - Invalid Account Number"+
                "<br>R05 - Unauthorized Debit to Consumer Account Using Corporate SEC Code"+
                "<br>R06 - Returned per ODFI Request"+
                "<br>R07 - Auth Revoked by Customer"+
                "<br>R08 - Payment Stopped"+
                "<br>R09 - Uncollected Funds"+
                "<br>R10 - Customer Advises Not Authorized"
                ));

        p.add(reasonCode);

        p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));

        Object[] choices = {"Next","Next Batch","Submit"};
        Object defaultChoice = choices[0];



        JOptionPane.showOptionDialog(null, p, "Return Builder",JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE,null,choices,defaultChoice);

        reasonCode.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ev){

                System.out.println(reasonCode.getText());

            }

        });



        }

    }
package-nacha;
导入java.awt.Component;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.BoxLayout;
导入javax.swing.JComboBox;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
公共类测试
{
静态字符串代码=null;
公共静态void main(字符串参数[]){
int-sixBatch1TotalCounter=1;
int-sixBatch1Total=2;
int main=1000001;
最终JTextField理性码=新的JTextField(10);
JPanel p=新的JPanel();
p、 添加(新JLabel(“”)+
“条目详细信息:”+
“
”+main+ “+sixBatch1TotalCounter+”中的“
条目详细信息”+sixBatch1TotalCounter+”+ “

请键入1-21将原因代码和补遗记录应用于条目详细信息记录。”+
要从报税表中省略显示的条目详细信息,只需将输入行留空,然后按enter键即可+ “

原因代码说明:”+ “
R01-资金不足”+ “
R02-帐户已关闭”+ “
R03-无账户”+ “
R04-无效的帐号”+ “
R05-未经授权使用公司SEC代码借记消费者帐户”+ “
R06-根据ODFI请求返回”+ “
R07-客户已撤销授权”+ “
R08-已停止付款”+ “
R09-未收款”+ “
R10-客户建议未经授权” )); p、 添加(代码); p、 setLayout(新的BoxLayout(p,BoxLayout.Y_轴)); 对象[]选项={“下一步”、“下一批”、“提交”}; 对象defaultChoice=choices[0]; JOptionPane.showOptionDialog(null,p,“返回生成器”,JOptionPane.DEFAULT\u选项,JOptionPane.QUESTION\u消息,null,choices,defaultChoice); reasonCode.addActionListener(新ActionListener(){ 已执行的公共无效操作(操作事件ev){ System.out.println(reasonCode.getText()); } }); } }
在对话框关闭后返回showOptionDialog之前,您不会设置actionListener。整个过程也应该从EventThread运行

谢谢你的评论,但我还是有点困惑。我理解你关于ActionListener的意思,但不明白你所说的事件线程是什么意思。如果你想做的是根据OptionDialog的哪个按钮进行操作,那么为什么在文本字段上需要ActionListener?您没有保存showOptionDialog呼叫的结果,因此无法确定按下了哪个按钮。您的回答足以让我朝着正确的方向前进,我让它正常工作。谢谢!