2013-01-17 19 views
0

我目前正在制作一个程序,使用RFID来输入并使用Java Swing Interface显示其信息。当Swing应用程序启动时运行一个函数(私有)

这里我的问题是,我创建了一个函数,不断检查RFID中是否有输入,并使用Swing显示它,但我需要在应用程序启动时运行该函数。我试过使用公共函数,但它不能使用jlabel.setText和其他函数,因为它正在静态运行。

我认为添加一个开始按钮只是为了让轮子转动,但我不喜欢使用任何按钮,因此界面很干净。

package com.domain.test; 

import java.io.IOException; 
import java.io.InputStream; 

public class SerialReader implements Runnable { 

    InputStream in; 

    public SerialReader(InputStream in) { 
     this.in = in; 
    } 

    public void run() { 
     byte[] buffer = new byte[1024]; 
     String dataHolder; 
     String IDnumber = new String(); 
     int incomingData = -1; 
     int ctr = 0; 
     try { 

      while ((incomingData = this.in.read(buffer)) > -1) { 
       dataHolder = new String(buffer, 0, incomingData); 

       if (incomingData >= 1) { 


        ctr++; 

        IDnumber = IDnumber + dataHolder; 
        if (IDnumber.length()==14) { 

         System.out.println(IDnumber); 
         log display = new log(); 
         display.setLabel(IDnumber.substring(1, IDnumber.length() - 1)); 
         IDnumber = new String(); 
         ctr = 0; 

        } 

       } 

      } 

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

我需要做的是传递提交给这个函数的数据,然后传递给这个Swing函数(setText)。

public void setLabel(String studNum){ 
    sNum = studNum; 
    jName.setText(sNum); 
} 

问题是我可以传递数据,但我不能用它来显示在我的界面上。这就是为什么我要问如何创建一个可以在应用程序启动时运行的函数(可以与Swing通信)。

此外,如果你可以给我另一种方式,我可以将数据传递给我的Swing。

+0

没有实际的代码,这个问题不能回答...请通过使用编辑按钮把它放在你的问题上。 – ppeterka

+0

code added sir ... –

回答

2

您可以实现轮询来寻找输入

http://download.oracle.com/javase/tutorial/uiswing/misc/timer.html

摆动定时器(的 javax.swing.Timer一个实例)触发一个指定的延迟后的一个或多个 操作事件。 请勿将Swing定时器与 通用定时器工具混淆, 已添加到 版本1.3中的java.util包中。此页面只描述 摆动定时器。

一般情况下,我们建议 使用Swing的计时器,而不是 通用定时器用于GUI相关 任务,因为摇摆定时器都共享 一样,预先存在的计时器线程 和GUI相关任务自动 执行在事件派发线程上。 但是,如果您不想从 定时器触摸GUI或需要执行冗长的 处理,您可能会使用通用定时器 。

+0

试图找出摆动定时器...谢谢先生 –

+0

它的习惯在SO接受答案,如果你喜欢它 – TheWhiteRabbit

2

您需要线程,定时器或执行程序服务来启动并检查该RFID源。

为什么不能在创建应用程序时实例化并启动线程?我看不需要按钮。

我不会对Swing线程执行此操作。结果将在UI中呈现,但该操作并非真正的Swing问题。

0

首先你需要渲染GUI一个单独的线程来显示 IDNumber中,这样,当GUI是 渲染的IDNumber您的阅读器线程没有被阻塞。


其次,你需要的IDNumber的并发显示,并从输入流中读取 消息。


可以同时实现上面说的通过使用Swing.

最后提供SwingWorker API,以确保阅读器开始运行的GUI (显示)开始后才要求,就需要volatile关键字,以便JVM不会 以优化代码为借口更改代码的执行顺序,而执行 。


在这里,我发布了一个代码片段来实现所有这些。我希望这可能是你的帮助。

import javax.swing.*; 
import java.awt.*; 
import java.util.List; 
class SerialReader extends SwingWorker<Void,String> 
{ 
    Log display ; 
    String messages[] =  { 
        "Java supports OOPS!!", 
        "JRE is the bundle of JVM and java Class liberary", 
        "JVM executes the java bytecode.", 
        "JIT converts code at runtime prior to executing it natively", 
        "Stay here I'll be back after reading more about java!!!! :)" 
       }; 
    private SerialReader(){}//To avoid construction of object without parameter 
    public SerialReader(Log display) 
    { 
     this.display = display; 
    } 
    @Override 
    protected Void doInBackground() 
    { 
     try 
     { 
      //Reading Messages one by one till the end 
      for(int i=0;i<messages.length;i++) 
      { 
       String message = messages[i]; 
       publish(message); 
       Thread.sleep(600);//I have added this much delay in compensation to the time 
         //that would be taken when u are formating data from inputstream. 
      }   //You must sleep the Thread for at least 1 ms so that change in 
         //display could be visualized. 
     }catch(Exception ex){ex.printStackTrace();} 
     finally 
     { 
      return null; 
     } 
    } 

    @Override 
    protected void process(List<String> chunks) 
    { 
     for(String msg: chunks) 
     { 
      display.setLabel(msg); 
     } 
    } 

    @Override 
    protected void done() 
    {} 
} 
class Log extends JFrame 
{ 
    private JLabel label = new JLabel(""); 
    public Log() 
    { 
     super("Display"); 
    } 
    public void prepareAndDisplay() 
    { 
     getContentPane().setLayout(new FlowLayout()); 
     getContentPane().add(label);  
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(400,100); 
     setVisible(true); 
    } 
    public void setLabel(final String value) 
    { 
     label.setText(value); 
    } 
} 
public class MainClass 
{ 
    private static volatile Log log; 
    private static volatile SerialReader reader; 
    public static void main(String st[]) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       log = new Log(); 
       log.prepareAndDisplay(); 
       log.setLabel("Display is ready Please Wait for incoming message..."); 
       try 
       { 
        Thread.sleep(100);//Delayed the start of Reader by 100 ms 
       }catch(Exception ex){} 
       reader = new SerialReader(log); 
       reader.execute(); 
      }  
     });         
    } 
} 


为了更多地了解SwingWorker API点击here。要了解如何使用SwingWorker请点击here

相关问题