向JPane

时间:2015-10-06 17:08:22

标签: java

我正在尝试为与工作相关的项目创建GUI并遇到一些问题。

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

我遇到的问题是带有JTextField的ActionListener似乎无法正常工作。当我测试它时,我得不到任何结果。任何帮助将不胜感激,下面是我的代码。

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

            }

        });



        }

    }

1 个答案:

答案 0 :(得分:2)

在对话框已经关闭之前,在showOptionDialog返回之前,不要设置actionListener。整个事情应该从EventThread运行。