在Java中,如何通过按钮使用输入的值生成序列?

在Java中,如何通过按钮使用输入的值生成序列?,java,user-interface,listener,actionlistener,Java,User Interface,Listener,Actionlistener,我想写程序谁在t1(文本字段)输入多少用户想要输入和b1(按钮)确认数字。在t2(文本字段)中,用户给出第一个值并通过b2(按钮)输入,下次用户给出第二个值并再次通过b2(按钮)输入。它发生的次数是n(从第一个侦听器开始) 我必须如何更改该程序的代码才能执行此操作 现在,当我给t2第一个值并点击按钮时,b2仍然被按下,用户不能做任何事情 final AtomicInteger n = new AtomicInteger(); ActionListener lis5 = new ActionLis

我想写程序谁在t1(文本字段)输入多少用户想要输入和b1(按钮)确认数字。在t2(文本字段)中,用户给出第一个值并通过b2(按钮)输入,下次用户给出第二个值并再次通过b2(按钮)输入。它发生的次数是n(从第一个侦听器开始)

我必须如何更改该程序的代码才能执行此操作

现在,当我给t2第一个值并点击按钮时,b2仍然被按下,用户不能做任何事情

final AtomicInteger n = new AtomicInteger();
ActionListener lis5 = new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        String a = t1.getText();
        n.set(Integer.parseInt(a));
    } 
};
b1.addActionListener(lis5); 

ActionListener lis6 = new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        //String b = t2.getText();     ??
        //n.set(Integer.parseInt(b));  ??

        int nn = n.get();
        int [] tab = new int[nn];
        for(int i=0;i<nn;i++){
            tab[i] = in.nextInt();
        }
    } 
};
b2.addActionListener(lis6);
final AtomicInteger n=新的AtomicInteger();
ActionListener lis5=新建ActionListener(){
已执行的公共无效操作(操作事件e){
字符串a=t1.getText();
n、 set(Integer.parseInt(a));
} 
};
b1.addActionListener(lis5);
ActionListener lis6=新建ActionListener(){
已执行的公共无效操作(操作事件e){
//字符串b=t2.getText()??
//n、 set(Integer.parseInt(b))??
int nn=n.get();
int[]tab=新的int[nn];

对于(int i=0;i首先,去掉ActionListener中的for循环。for循环适用于线性控制台程序,其中您使用扫描仪输入数据,但不适用于事件驱动GUI。在这里,不要使用for循环,而是使用计数器int变量,该变量在ActionListener中递增,并用于帮助您决定将数据放置在何处。also将int数组从ActionListener中取出并放入类中,以便类的其余部分可以使用它。在伪代码中:

in the class
    int array declared here BUT not initialized here.

    first Actionlistener
        get total count value
        initialize array using this value
    end first action listener

    second ActionListener
        int counter variable set to 0.
        action performed method
            if counter < total count
                get value from text field
                convert it to an int
                place value into intArray[counter]
                increment counter
            end if
        end action performed
    end second ActionLisener    
类中的

int数组在此处声明,但未在此处初始化。
第一个Actionlistener
获取总计数值
使用此值初始化数组
结束第一个动作侦听器
第二个ActionListener
int计数器变量设置为0。
动作执行法
如果计数器<总计数
从文本字段获取值
将其转换为int
将值放入数组[计数器]
增量计数器
如果结束
已执行的结束操作
第二个动作结束

您可以在计数器控制的循环中使用JOptionPane的showInputDialog。如果.nextInt()
中的
正在读取一个
扫描仪的
,线程将阻塞,直到有一个整数要读取。这就是您正在做的吗?@belusa16:他正在使用一个数组,
int[]tab=new int[nn]所以我在上面的回答中继续说,但是是的,最好使用更灵活的
ArrayList
,因为它可以立即初始化,因为他不必知道总计数。