java冻结账号功能代码_java-JFrame在连续运行代码时冻结

在Java应用中,当使用JFrame并尝试连续运行代码时,遇到了UI冻结问题。具体表现为在MainLoop()函数的while循环中,由于需要停止循环必须将Running变量设为false,但这需要通过btnHalt按钮触发。然而,点击btnHalt时,JFrame变得无响应,导致无法更新文本区域的日志。问题可能在于事件处理或线程同步上。
摘要由CSDN通过智能技术生成

我在使用JFrame时遇到问题,而在

连续运行代码.下面是我的代码:

>单击btnRun时,我调用了函数MainLoop():

ActionListener btnRun_Click = new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

MainLoop();

}

};

> MainLoop()的实现:

void MainLoop()

{

Hopper = new CHopper(this);

System.out.println(Hopper);

btnRun.setEnabled(false);

textBox1.setText("");

Hopper.getM_cmd().ComPort = helpers.Global.ComPort;

Hopper.getM_cmd().SSPAddress = helpers.Global.SSPAddress;

Hopper.getM_cmd().Timeout = 2000;

Hopper.getM_cmd().RetryLevel = 3;

System.out.println("In MainLoop: " + Hopper);

// First connect to the validator

if (ConnectToValidator(10, 3))

{

btnHalt.setEnabled(true);

Running = true;

textBox1.append("

Poll Loop

"

+ "*********************************

");

}

// This loop won't run until the validator is connected

while (Running)

{

// poll the validator

if (!Hopper.DoPoll(textBox1))

{

// If the poll fails, try to reconnect

textBox1.append("Attempting to reconnect...

");

if (!ConnectToValidator(10, 3))

{

// If it fails after 5 attempts, exit the loop

Running = false;

}

}

// tick the timer

// timer1.start();

// update form

UpdateUI();

// setup dynamic elements of win form once

if (!bFormSetup)

{

SetupFormLayout();

bFormSetup = true;

}

}

//close com port

Hopper.getM_eSSP().CloseComPort();

btnRun.setEnabled(true);

btnHalt.setEnabled(false);

}

>在MainLoop()函数中,while循环一直运行,直到“运行为真”问题是,如果我想停止while循环,则必须将Running设置为false,这是在另一个按钮btnHalt上完成的:

ActionListener btnHalt_Click = new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

textBox1.append("Poll loop stopped

");

System.out.println("Hoper Stopped");

Running = false;

}

};

但是btnHalt没有响应,整个帧被冻结,也没有

显示文本区域中的任何日志.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值