退出后延迟或执行按钮操作

Button action delayed or performed after exiting

我没有太多编码经验,我只做过一个非常小(真的很小)的应用程序,之前用于将文件从位置A复制到位置B然后刷新A目录我没有遇到过这种问题。

现在我正在开发一个应用程序,该应用程序在数据库中搜索数据并将其发送到标签打印机进行打印。

除打印按钮外,一切正常;当我单击打印按钮时,没有任何反应,如果我多次单击它(通常是 3 次),打印机将开始打印 3 次,之后没有任何反应,直到我使用 X 按钮关闭应用程序 (GUI)。 (直接代码打印不存在这个问题)

代码比较简单,请看一下:

按钮代码:

private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {                                      
    // TODO add your handling code here:
    PrintLabel p = new PrintLabel(jTextField1.getText());

    if (ONtoggle.isSelected()) {
        try {
            p.labelPrint(jTextField2.getText(), jTextField3.getText(), "MMC");
        } catch (FileNotFoundException | SQLException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 
    else {
        try {
            p.labelPrint(jTextField2.getText(), jTextField3.getText(), "MMT");
        } catch (FileNotFoundException | SQLException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}                                     

正在打印 Class:

public class PrintLabel {

  private final String port;
        public PrintLabel(String port) {
                this.port = port;
        }


        public void labelPrint (String number, String copies, String cut) throws FileNotFoundException, SQLException {   


        String DBurl = "jdbc:oracle:thin:@[DB hostname]/[DB Service Name]"; //DB login info.
        String DBuser = "user";
        String DBpass = "pass";

        Connection DBcon = DriverManager.getConnection(DBurl, DBuser, DBpass); // DB connection



        Statement statement = DBcon.createStatement(); //class used to make statements.


        ResultSet rs = statement.executeQuery([SELECT STATEMENT]);


       if (rs.next()) {

            String name = rs.getString("name");
            String nationality = rs.getString("nationality");
            String sex = rs.getString("sex");
            String birthDate = rs.getString("birthdate");

            FileOutputStream os = new FileOutputStream(port);

             PrintStream ps = new PrintStream (os);


             String commands = 
                     "^XA" + //begin ZPL command.
                     "^" + cut + "" + //kiosk cut mode, needed for cutting. MMC for cutting and MMT for tear-off.
                     "^PQ" + copies + ",0,0,n,y" + //first number indicates number of copies.
                     //Line 1
                     "^FO100,20" +  //X and Y axis alignment.
                     "^A0N,20,20" + //font width and hight in dots.
                     "^FB350,2,5,L,0" + //max lines set to 2 for long names.
                     "^FDName: " + name + "^FS" +  //print text command start and end.
                     //Line 2
                     "^FO100,70" + 
                     "^A0N,20,20" + 
                     "^FD File: " + number + "^FS" + 

                     "^FO420,70" + 
                     "^A0N,20,20" + 
                     "^FD" + sex + "^FS" + 
                    //Line 3
                     "^FO100,100" + 
                     "^A0N,20,20" + 
                     "^FD Date of Birth: " + birthDate + "^FS" + 
                     //Line 4
                     "^FO100,130" + 
                     "^A0N,20,20" + 
                     "^FD Nationality: " + nationality + "^FS" + 
                     //Line 5
                     "^FO100,150 ^BY2,1.0,50" + //Barcode Field Width, ratio and height.
                     "^B3N,50,n,n" + //Barcode code-39.
                     "^FD" + number + "^FS" + 
                     "^XZ";  //end ZPL command.

             ps.println(commands);


        }
        }




}

非常感谢。

您的问题可能有多种原因,最明显的是您的数据库操作连接需要一段时间,运行 和 return 结果很难给你一个单一的答案。

根据你的问题,不清楚单击按钮是否会导致打印任何内容?

我建议调试您的应用程序以帮助找出问题所在,或者至少添加一些日志记录来帮助您。

此外,您应该始终关闭和释放流、数据库连接、语句和结果集。