我正在使用java线程同时管理多个(3)程序。 1表示Java swing表单(绘制用于输入的UI),1表示在systemtray上设置图标(从UI获取输入后立即启动),1表示处理输入并将其反映到SystemTray图标(以弹出窗口的形式)消息到托盘图标)。
早些时候,我试过没有单独的线程,但我面临的问题是"如果执行控制一次前进(从UI到下一个Process),其中使用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);
}
}
}
}
任何人都可以帮助我。 非常感谢。
答案 0 :(得分:4)
您的main()
执行new LauncherUI()
。
您的LauncherUI构造函数在start()
上调用Runnable
; run()
方法调用new LauncherUI()
。该构造函数调用start()
,重复该过程;经典的无限循环。