gpt4 book ai didi

java - 向 JPane 添加操作监听器或 if 语句

转载 作者:行者123 更新时间:2023-12-02 04:19:14 24 4
gpt4 key购买 nike

我正在尝试为与工作相关的项目创建 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());

}

});



}

}

最佳答案

直到 showOptionDialog 返回(当对话框已经关闭时),您才可以设置 actionListener。另外,整个事情应该从 EventThread 运行。

关于java - 向 JPane 添加操作监听器或 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32975676/

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