加载所有组件后执行代码块

时间:2011-04-30 18:21:01

标签: java swing

我有一个与串口通信的程序 线程涉及此串口程序。

我正在显示一个摆动框架来显示串口的状态 问题是我打电话setVisible(true)窗口没有完全加载。
我尝试使用isValid()在窗口加载完成后启动串口脚本,但它从未起作用。

我希望在窗口加载完成后启动串口脚本 我如何克服这个问题?

//Code used to call setVisible()

form_objects.cardPayment.setVisible(true);  
if (form_objects.cardPayment.isValid()) {  
    openPort.sendMessage(global_variables.port, "@PL\r");  
    String readMessage = openPort.readMessage(global_variables.port);  
    System.out.println(readMessage);  
    String check_bit[] = readMessage.split(",");  
    System.out.println(check_bit[2]);  
    if (check_bit[0].equalsIgnoreCase("@PL") &&check_bit[2].trim().equals("0")) {  
        card_payment.card_text.setText("Swipe Card");  
        openPort.sendMessage(global_variables.port, "@PU," + amount +  ",,,,1\r");  
        boolean loop = true;  
        while (loop) {  
            openPort.sendMessage(global_variables.port, "@SR,1,131\r");   
            readMessage = openPort.readMessage(global_variables.port);  
            if (readMessage.equalsIgnoreCase("Timeout")) {   
                card_payment.card_text.setText("Enter Pin");  
            }  
            if (!readMessage.equals("") && !readMessage.equals("Timeout"))    {  
                loop = false;  
            }  
        }  
        String sr[] = readMessage.split(",");  
        if (sr[1].equals("1") && sr[5].equals("")) {  
            System.out.println("Cancelled");  
            card_payment.card_text.setText("Payment Cancelled");  
            form_objects.cardPayment.dispose();  
        } else if (sr[1].equals("1") && sr[5].equals("T")) {  
            System.out.println("Accepted");  
            card_payment.card_text.setText("Accepted");  
            long ptime = Calendar.getInstance().getTimeInMillis();  
            while(ptime+10000>=Calendar.getInstance().getTimeInMillis()){  
                //do nothing just stay thhere for 10 seconds  
            }  
            form_objects.cardPayment.dispose();  
        } else if (sr[1].equals("1") && sr[5].equals("F")) {  
            System.out.println("Declined");  
            card_payment.card_text.setText("Payment Declined");  
        }  
    } else {  
        System.out.println("terminal offline");  
    }  
}  

- 用于读写使用thread.sleep() ---

的端口的代码
public static void sendMessage(SerialPort port, String msg) {

    if (port != null) {

        System.out.println(msg);

        try {

            byte[] bytes = msg.getBytes("US-ASCII");

            try {

                global_variables.outputStream.write(bytes);

                System.out.println(bytes.length);

                global_variables.outputStream.flush();

            } catch (IOException ex) {

                Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex);

            }

        } catch (UnsupportedEncodingException ex) {

            Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex);

        }

        System.out.println("Opened successfully:" + msg.getBytes());

        try {
            Thread.sleep(2000);  // Be sure data is xferred before closing
            System.out.println("read called");
            //byte b[] = new byte[1024];
            //global_variables.inputStream.read(b);

            //System.out.println("available" + global_variables.inputStream.available());

            //SimpleRead read = new SimpleRead();
            //int read = global_variables.inputStream.read();
            //System.out.println("read call ended"+read);
        } catch (Exception e) {
        }

    }
}

public static String readMessage(SerialPort port) {
    byte[] buffer = new byte[1024];
    int count=0;                    
        try {
            Thread.sleep(1000);                
            if (global_variables.inputStream.available() > 0) {
                /*assigning it to count variable makes the read uniform. if we use available() each time the string are not processed fully.
                 * so assign it to count and use the count for rest of the buffer read operation
                 */

                count = global_variables.inputStream.available();
                System.out.println("Data Available:" + count);
                for (int i = 0; i < count; i++) {
                    buffer[i] = (byte) global_variables.inputStream.read();
                }
                String response = new String(buffer, 0, count);                    
                return response;
            } else {
                return "Timeout";
            }

        } catch (InterruptedException ex) {
            Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(openPort.class.getName()).log(Level.SEVERE, null, ex);
        }        
    return "timeout";
}

当我在form_objects.cardPayment.setVisible(true)的位置使用以下代码时,我无法获得调用的串口方法 你能解释一下为什么会这样吗?

java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                    form_objects.cardPayment.setVisible(true);
                }
            });

1 个答案:

答案 0 :(得分:4)

在您调用form_objects.cardPayment.setVisible(true);的同一个帖子中,您还调用了while (true) {,这将占用您的Swing应用程序,直到端口代码完成它为止。

您需要阅读使用后台线程,因为您似乎在Swing线程上执行了所有操作。查看使用SwingWorker对象。例如,Lesson: Concurrency in Swing

修改1:

@Deepak:如果你仍然陷入困境,可以考虑创建和发帖,或许甚至作为你的问题的答案或问题的附录SSCCE(请查看链接)。这可能需要比典型的SSCCE稍长一些,并且有点棘手,因为您必须模拟一些后台进程 - 足以重现您的问题 - 但可以帮助我们为您提供解决方案。