我有一些我无法理解的东西:我的Swing GUI包含一个'播放'和'暂停'按钮.我还有一个定义'ON'和'OFF'状态的静态变量.(主程序生成GUI).通过克服'play',我将静态变量的状态更改为'ON',并在一个也修改GUI的线程中启动一个耗时的过程.只要静态变量在同一进程中为'ON'循环.单击"暂停"会将静态变量更改为OFF.但是通过点击"播放",GUI就会冻结,因此:
我听说过EDT和SwingWorker,但我有一个简单的方法可以做到这一点.
谢谢你的帮助,原谅我的英语不好......
下面的代码是我用来创建端口扫描程序的,但是,当我点击"扫描"它执行但gui冻结直到它完成扫描,是否有任何可能的方法允许它在扫描时执行其他操作?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class PortScannerGUI extends JFrame implements ActionListener
{
JPanel Panel = new JPanel(null);
JTextField Field = new JTextField();
JButton Button = new JButton();
JTextArea Area = new JTextArea();
JButton limit = new JButton("limit");
public PortScannerGUI()
{
super();
setSize(350, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLocationRelativeTo(null);
LoadUI();
}
public void LoadUI()
{
Area.setBounds(20, 50, 310, 310);
Area.setBorder(BorderFactory.createLineBorder(Color.BLUE));
Area.setEditable(false);
Field.setBounds(20, 10, 200, 35);
Button.setBounds(230, 10,100, 35);
Button.setText("Scan");
Button.addActionListener(this);
Panel.add(Field);
Panel.add(Area); …Run Code Online (Sandbox Code Playgroud) 我认为这是一个很常见的问题,但我仍然找不到满意的答案,所以我会问自己.
这是一段代码:
// this is insine OnClickView
TextView status = (TextView) findViewById(R.id.status);
status.setText("Trying to connect to the server...");
try {
// this opens a socket and send a login request to the server.
int result = CommunicationManager.login(String email, String password);
switch (result) {
case CommunicationManager.SUCCESS:
// login ok, go on with next screen
break;
case CommunicationManager.WRONG_EMAIL:
status.setTextColor(Color.RED);
status.setText("Wrong Email!");
break;
case CommunicationManager.WRONG_PASSWORD:
status.setTextColor(Color.RED);
status.setText("Wrong Password!");
break;
}
} catch (CommunicationException e) {
status.setTextColor(Color.RED);
status.setText("Unable to estabilish a connection!");
} catch …Run Code Online (Sandbox Code Playgroud) 我遇到了一个基于Java Swing的应用程序的问题,我是为专业目的而创建的.
一个新类PanelUserInput,我为这个类创建了一个单独的监听器,称为PanelUserInputListenerIS-A ItemListener和ActionListener.
类PanelUserInput类至少有5个Labels和ComboBox,它们现在具有虚拟值.还有一个用于提交所选值的按钮.我正在使用GridBadLayout来定位组件.
现在的问题是ComboBox中的项目选择是通过鼠标阻止的.虽然我可以使用键盘箭头键(向上和向下)访问元素.此外,我无法使用鼠标单击按钮,但我只能按下我的键盘Space
有没有人在Java Swing中看到过这种行为,什么是最佳解决方案?
由于保密原因,我无法共享代码.
http://i.stack.imgur.com/XvHm5.png
当我单击"开"按钮时,它应该开始在JTextField上发送1个垃圾邮件.而是整个GUI冻结,包括关闭按钮.我正在使用while循环,当你再次单击jButton时它会停止.因为jButton冻结了我无法阻止这个程序.我已经读过Threads和Multithreading会有所帮助,但是所有的教程都太复杂了,我无法理解.他们说把循环和GUI放在不同的线程上.我想理解这个,以修复我正在处理的另一个更复杂的程序.这就是为什么我做一个简单的例子来展示我的问题.
http://i.stack.imgur.com/tbNAF.png
这是程序显示的方式.(jTextField甚至没有更新.我要问的是,如果有人知道解决这个问题的方法,无论是否使用线程.我的代码在下面.
public class whileLoop extends javax.swing.JFrame {
public whileLoop() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jToggleButton1 = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jToggleButton1.setText("On");
jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jToggleButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jToggleButton1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) …Run Code Online (Sandbox Code Playgroud) 我在这里有这个代码,开始一个进程等待8秒,然后再次杀死它.
for (int i = 0; i < 20; i++)
{
Process pro = new Process();
pro.StartInfo.FileName = @"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
pro.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
pro.Start();
Thread.Sleep(8000);
try
{
pro.Kill();
Thread.Sleep(1000);
}
catch
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)
当我在调试模式或直接从.exe运行应用程序时,它成功启动并终止进程,但它被冻结.我无法移动窗口或点击其他按钮..
我的sort()函数存在瓶颈,例如:
list.sort(function (a,b) {return (a.value - b.value);});
Run Code Online (Sandbox Code Playgroud)
冻结浏览器几秒钟.
对于循环的相同情况,建议使用"超时"策略,例如此处描述的策略:
那么,问题是,这可以用排序方法实现吗?
*在评论讨论后编辑
// main_div is a div defined before
for (let i=0; i<list.length; i++) {
main_div.appendChild(document.getElementById(list[i].id));
}
Run Code Online (Sandbox Code Playgroud) 如果TortoiseHg 不再反应,我该怎么办?
在控制台窗口(如果打开...)中有一条消息(只要搁置窗口未打开):
Warte auf Sperre von Arbeitsverzeichnis von C:\... gehalten von ''
Run Code Online (Sandbox Code Playgroud)
意思是
Wait for lock of working directory C:\... held by ''
Run Code Online (Sandbox Code Playgroud) 我已经用 C# 为我正在开发的一个小型 Forms 程序编写了以下方法,但每当我尝试运行此方法时,整个程序都会冻结。
没有错误,我尝试for (int n = 0; n<8; n++)这样做while(true),但这似乎没有改变任何事情......
有任何想法吗?提前致谢!
public bool legalMove(int y, int x)
{
// Check if the cell is occupied
if (grid[x, y] != 0)
return false;
// Check if there's an opponents circle somewhere around it
for (int i = -1; i<=1; i++)
for (int j = -1; i<=1; j++)
{
if (i == 0 && j == 0)
continue;
int row = x + i;
int col …Run Code Online (Sandbox Code Playgroud) 我在我的程序中实现了 3 个辅助线程。
GET第一个通过每 2 分钟向每个外部设备发送一个 HTTP 请求来检查外部设备的在线状态。
第二个用于“实时查看”功耗,每 2 秒检查一次外部设备的值,并在主线程中使用一些进度条/标签显示它们。
第三个等待并每隔一小时从同一设备获取其他值并将它们写入文件。
在线程 #3 中没有Synchronize()必要(我认为),因为主线程获取文件并根据用户命令从中构建图表。
实际上,所有线程都像魅力一样运行几个小时,直到午夜。第一个线程 24/7 持续运行,没有任何问题,但其他两个线程似乎在午夜锁定。
主窗口始终保持功能状态。
如果我关闭程序并重新启动它,一切都会再次正常工作,但在 00:00h ... finito 丢失了线程。
难道是程序中的等待循环有问题Execute()?到目前为止我还没有找到。
我是第一次来,如有遗漏或不清楚的地方,请多多包涵!
这是线程 #1 的声明和实现:
type
TpingThread = class(TThread)
private
pingHTTP: TidHTTP;
public
constructor Create;
procedure updateOnlineStatus;
protected
procedure Execute; override;
end;
constructor TPingThread.Create;
begin
Self.Suspended := False;
Self.FreeOnTerminate := False;
inherited Create(False);
end;
//"pings" all networkdevices, runs until program ends
procedure TPingThread.Execute;
Var delayTime: TDateTime;
n: Byte;
s: String;
begin …Run Code Online (Sandbox Code Playgroud)