线程在第一次运行时启动,然后在下次运行时停止

时间:2018-01-25 11:24:46

标签: java multithreading swing netbeans raspberry-pi3

大家好我有问题。我有2个JFrame,Login.javaMainForm.java。每当我开始第一次运行并成功登录时,线程就会启动,但是当我使用this.setVisible(false);注销JFrame然后再次登录时,线程将不再启动。这里似乎有什么问题?是否可以在每次用户登录时停止线程并再次启动它?

这是Login.java

的代码
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;


public class Login extends javax.swing.JFrame {

    public Login() {
        initComponents();
        jLabel_asterisk1.setVisible(false);
        jLabel_asterisk2.setVisible(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {...}                    


    //LOGIN FUNCTION
    private void jButton_loginActionPerformed(java.awt.event.ActionEvent evt) {                                              
        //asterisk required
        jLabel_asterisk1.setVisible(false);
        jLabel_asterisk2.setVisible(false);

        if(jTextField_username.getText().equals("")){
            jLabel_asterisk1.setVisible(true);
        }

        if(String.valueOf(jPasswordField.getPassword()).equals("")){
            jLabel_asterisk2.setVisible(true);
        } else {
            Connection con = MyConnection.getConnection();
            PreparedStatement ps;

            try {
                ps = con.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
                ps.setString(1, jTextField_username.getText());
                ps.setString(2, String.valueOf(jPasswordField.getPassword()));

                ResultSet rs = ps.executeQuery();

                if(rs.next()){

                    MainForm mf = new MainForm();
                    mf.setVisible(true);

             MainForm.jLabel_welcome.setText(jTextField_username.getText());

                    Thread mt = new Thread(new MainForm.bca());
                    mt.start();

                    this.setVisible(false); 
                } else {
                    JOptionPane.showMessageDialog(null, "Incorrect Username or Password!");
                }
            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalArgumentException e){
                JOptionPane.showMessageDialog(null, "You have already paid your latest balances, queue will proceed.");
            }
        }
    }                                             

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
}

这里是MainForm.java

的代码
import javax.swing.JLabel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.GpioInterrupt;
import com.pi4j.wiringpi.GpioInterruptListener;
import com.pi4j.wiringpi.GpioInterruptEvent;
import com.pi4j.wiringpi.GpioUtil;

public class MainForm extends javax.swing.JFrame {

    public static volatile int pulses = 0;
    public static boolean display = true;

    public static class bca implements Runnable{

        @Override
        public void run() {

            //create listener //bill coin pulses
            GpioInterrupt.addListener(new GpioInterruptListener() {
                @Override
                public void pinStateChange(GpioInterruptEvent event) {

                    if (event.getPin() == 0) {
                        if (event.getState() == false) {
                            pulses++;
                            display = true;
                        }
                    }

                    if (event.getPin() == 7) {
                        if (event.getState() == false) {
                            int bpulses = 0;
                            bpulses++;
                            pulses += bpulses * 10;
                            display = true;
                        }
                    }
                }
            });

            //setup wiring pi
            if (Gpio.wiringPiSetup() == -1) {
                System.out.println("Setup Failed!");
                return;
            }

            //pins that will be used
            GpioUtil.export(7, GpioUtil.DIRECTION_IN);
            GpioUtil.export(0, GpioUtil.DIRECTION_IN);

            //edge state whether rising, falling or change
            GpioUtil.setEdgeDetection(7, GpioUtil.EDGE_FALLING);
            GpioUtil.setEdgeDetection(0, GpioUtil.EDGE_FALLING);

            //enabling input as a callback
            Gpio.pinMode(7, Gpio.INPUT);
            Gpio.pullUpDnControl(7, Gpio.PUD_UP);
            GpioInterrupt.enablePinStateChangeCallback(7);

            //enabling input as a callback
            Gpio.pinMode(0, Gpio.INPUT);
            Gpio.pullUpDnControl(0, Gpio.PUD_UP);
            GpioInterrupt.enablePinStateChangeCallback(0);

            //void loop
            for (;;) {
                //Thread.sleep(5000);
                if (display == true) {
                    //System.out.println(pulses);
                    jTextField_pulse.setText(String.valueOf(pulses));
                    display = false;
                }
            }
        }
    }

    public MainForm() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {...}                      

    public static void main(String args[]) {

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainForm().setVisible(true);
            }
        });
    }

请帮帮我。我刚刚开始学习java。我目前正在使用rpi上的NetBeans IDE。

0 个答案:

没有答案