gpt4 book ai didi

java - 为什么循环禁用其他按钮或 Swing 组件?

转载 作者:行者123 更新时间:2023-12-01 06:58:24 24 4
gpt4 key购买 nike

当我按下其中包含 for 循环事件的 jbutton 时,其他组件被禁用。

这是我从 jbutton 获得的代码:

    try{
InetAddress localhost = InetAddress.getLocalHost();
byte[] ip = localhost.getAddress();

for(int i=1;i<254;i++){
ip[3]=(byte)i;
final InetAddress address = InetAddress.getByAddress(ip);

if(address.isReachable(1000)){
listModel1.addElement(address);
System.out.println(address + "-Machine is turned on and can be ping.");
Rectangle progressRec = jProgressBar1.getBounds();
progressRec.x = 0;
progressRec.y = 0;
jProgressBar1.setValue(i);
jProgressBar1.paintImmediately(progressRec);
}
}
jList1.setModel(listModel1);
}catch(Exception e){
e.printStackTrace();
}

那么循环结束后,其他组件就启用了吗?我该如何处理呢?提前致谢...

编辑的代码:由不擅长编码的人建议

    try{
InetAddress localhost = InetAddress.getLocalHost();
final byte[] ip = localhost.getAddress();

SwingWorker<ListModel, InetAddress> worker = new SwingWorker<ListModel, InetAddress>(){
public ListModel doInBackground() throws UnknownHostException, IOException{
for(int i=1;i<254;i++){
ip[3]=(byte)i;
final InetAddress address = InetAddress.getByAddress(ip);

if(address.isReachable(1000)){
publish(address);
listModel1.addElement(address);
Rectangle progressRec = jProgressBar1.getBounds();
progressRec.x = 0;
progressRec.y = 0;
jProgressBar1.setValue(i);
jProgressBar1.paintImmediately(progressRec);
}
}
return listModel1;
}

@Override
public void process(List<InetAddress> addresses){//there was a problem here.
for(InetAddress address : addresses){
System.out.println(address + "-Machine is turned on and can be ping.");
}
}

@Override
public void done(){
try {
jList1.setModel(get());
} catch (InterruptedException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
} catch (ExecutionException ex) {
Logger.getLogger(NetCafeTime.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
worker.execute();
}catch(Exception e){
e.printStackTrace();
}

最佳答案

大概,“禁用”意味着它们变得没有响应。

发生这种情况是因为您在 Event Dispatch Thread 中执行非常昂贵/长期的操作。它处理所有与 GUI 相关的操作,例如重新绘制组件。当您的代码阻塞此线程时,Swing 无法快速执行常规操作,并且 UI 将变得对用户操作无响应,并且绘画或绘制操作也可能出现滞后/卡住。

您应该生成一个新线程来完成繁重的工作并报告结果。 Swing 提供 SwingWorker可以帮助您做您想做的事情的类(class)。

快速而肮脏的代码:

try{
InetAddress localhost = InetAddress.getLocalHost();
final byte[] ip = localhost.getAddress();

SwingWorker<ListModel, InetAddress> worker = new SwingWorker<ListModel, InetAddress>()
{
public ListModel doInBackground()
{
for(int i=1;i<254;i++){
ip[3]=(byte)i;
final InetAddress address = InetAddress.getByAddress(ip);

if(address.isReachable(1000)){
publish(address);
listModel1.addElement(address);
}
}

return listModel1;
}

public void process(List<InetAddress> addresses)
{
for(InetAddress address : addresses)
{
System.out.println(address + "-Machine is turned on and can be ping.");
Rectangle progressRec = jProgressBar1.getBounds();
progressRec.x = 0;
progressRec.y = 0;
jProgressBar1.setValue(i);
jProgressBar1.paintImmediately(progressRec);
}
}

public void done()
{
jList1.setModel(get());
}
};

worker.execute();

}catch(Exception e){
e.printStackTrace();
}

建议阅读:

关于java - 为什么循环禁用其他按钮或 Swing 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5671699/

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