gpt4 book ai didi

Java线程正在重复 Swing 形式

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

我正在使用 java 线程同时管理多个 (3) 程序。1 用于 Java swing 表单(绘制 UI 以进行输入),1 用于在系统托盘上设置图标(从 UI 获取输入后立即启动),1 用于处理输入并将其反射(reflect)到 SystemTray 图标(以弹出窗口的形式)向该托盘图标发送消息)。

之前,我尝试过不使用单独的线程,但我面临的问题是“如果执行控制一次前进(从 UI 到下一个进程),其中使用 thread.sleep() 方法(持有控制权)执行一些重复任务实际上是整个时间)。因此,早期的表单(UI)仍然没有响应(我们当时甚至无法关闭表单)。

因此,我想为三个线程中的每一个实现单独的线程。但当我尝试启动表单(UI)时,它实际上会无限循环地重复。

这里是代码:

 public class ImprovedImplementation {

public static void main(String[] args) {
System.out.println("Main thread is started...");
new LauncherUI();
}

}

class LauncherUI implements Runnable {

private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;

String textFieldValue = null;

Thread launcher ;
LauncherUI()
{
prepareGUI();
launcher = new Thread(this, "Launcher Thread");
System.out.println("launcher thread created" + launcher);
launcher.start();
}
public void run()
{
try
{
LauncherUI swingControlDemo = new LauncherUI();
swingControlDemo.showEventDemo();
}
catch(Exception e)
{
System.out.println("launcher thread interrupted");
}
System.out.println("launcher run is over" );
}

private void prepareGUI() {

mainFrame.setSize(450, 400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.setLocationRelativeTo(null);

headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);

statusLabel.setSize(350, 100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());

mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
mainFrame.setResizable(false);
}

private void showEventDemo() {
headerLabel.setText("Welcome to the Web Server Tracking System !!");
headerLabel.setFont(new Font("serif", Font.BOLD, 20));

JLabel l = new JLabel();
l.setText("Enter All (Servers) URLs separated by a comma ( , ) :");
l.setFont(new Font("Serif", Font.BOLD, 16));
JLabel l2 = new JLabel();
l2.setText("[for example: google.com,wikipedia.org,sjsu.edu]");
l2.setFont(new Font("Serif", Font.BOLD, 15));
JTextField txt = new JTextField(30);

JButton submitButton = new JButton("Start Tracking");
JButton cancelButton = new JButton("Close");

submitButton.setActionCommand("Start");
cancelButton.setActionCommand("Cancel");

submitButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
textFieldValue = txt.getText();
// textFieldValue="Hello";
// .... do some operation on value ...
}
});
cancelButton.addActionListener(new ButtonClickListener());

controlPanel.add(l);
controlPanel.add(l2);
controlPanel.add(txt);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);

mainFrame.setVisible(true);

}

private class ButtonClickListener implements ActionListener {

public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Start")) {
if (textFieldValue.equals("") || textFieldValue.indexOf(".") == -1) {
statusLabel.setText("Please Enter Some Server Urls, Before Start Tracking.");
if (textFieldValue.indexOf(".") == -1 && !textFieldValue.equals("")) {
statusLabel.setText("Please Enter Valid Urls.");
}
statusLabel.setForeground(Color.RED);
} else {
StringTokenizer urlValues = new StringTokenizer(textFieldValue, ",");
Vector v = new Vector();
while (urlValues.hasMoreTokens()) {
String token = urlValues.nextToken();
v.add(token);
}
String[] urlStrArray = (String[]) v.toArray(new String[v.size()]);
System.out.println("Took following URLS from the User:");
for(int m=0; m < urlStrArray.length;m++){
System.out.println(urlStrArray[m]);
}
statusLabel.setText("Tracking is started . . . it's pinned in your System Tray ( Task bar ).");
statusLabel.setForeground(Color.BLACK);
}
} else {
System.exit(0);
}
}
}
}

谁能帮帮我。我们将不胜感激。

最佳答案

你的main()执行了一个new LauncherUI()

您的 LauncherUI 构造函数在 Runnable 上调用 start()run() 方法调用 new LauncherUI()。该构造函数调用 start(),重复该过程;经典的无限循环。

关于Java线程正在重复 Swing 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28295746/

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