无法使用动作监听器来使用JButton和JText

时间:2015-10-07 16:26:49

标签: java

今天我正在尝试使用文本字段和三个按钮创建GUI。用户将能够在文本字段中输入各种数字,并且根据他们键入的数字和点击的按钮将发生各种决策陈述。

我已经创建了我的代码并添加了动作侦听器,但由于某种原因,我的第一个按钮的动作侦听器不起作用。我希望它改变我用作计数器或其他方法的变量的值,如果它更好地工作。如果有人可以告诉并告诉我如何完成我想要的东西,我会在下面粘贴我的代码我会非常感激!

package nacha;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Testing extends javax.swing.JFrame
{

    static  String code = null;
    static int button1Toggle = 0;

    public static void main(String args[]){

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

        JPanel p = new JPanel();
        JPanel p2 = new JPanel();

        BorderLayout layout = new BorderLayout();

        layout.setHgap(10);
        layout.setVgap(10);

        final JTextField reasonCode = new JTextField(10);


        reasonCode.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev){
                Object source = ev.getSource();
                }
                });

        JButton button1 = new JButton("Next");
        button1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev){
            button1Toggle=1;
            }
            });

        final JButton button2 = new JButton("Next Batch");
        button2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev){

            }
        });

        final JButton button3 = new JButton("Submit");
        button3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ev){

            }
        });

        JLabel label = 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.setLayout(layout);
        p.add(label,BorderLayout.NORTH);
        p.add(reasonCode,BorderLayout.SOUTH);
        p2.add(button1,BorderLayout.WEST);
        p2.add(button2,BorderLayout.CENTER);
        p2.add(button3, BorderLayout.EAST);

        JFrame frame = new JFrame();
        frame.setSize(450,450);

        BorderLayout BorderLayout = new BorderLayout();
        BorderLayout.setHgap(10);
        BorderLayout.setVgap(10);

        frame.setLayout(BorderLayout);
        frame.add(p,BorderLayout.NORTH);
        frame.add(p2,BorderLayout.SOUTH);

        frame.setVisible(true);


            code = reasonCode.getText();
            if (reasonCode.getText().equals("1") && button1Toggle==1){
                System.out.println("Pass");


            }



        }

    }

1 个答案:

答案 0 :(得分:0)

气垫船在评论中真的回答了这个问题。

引用:

如果最后的块将被调用一次且仅一次,并且在用户甚至有机会以任何方式与GUI交互之前将被调用。如上所述@ 3kings,如果你想按下按钮时调用的代码,最好是在ActionListener中。这个问题涉及编写事件驱动程序意味着什么的核心问题,而且你想要思考这个错误以及为什么它没有像你编写的那样工作,因为这个如果你要进步,必须要达到理解。