gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:49 25 4
gpt4 key购买 nike

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

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

除了执行打印的按钮外,一切都按预期工作;当我单击打印按钮时,什么也没有发生,如果我多次单击它(通常是 3 次),打印机将开始打印 3 次,之后什么也不会发生,直到我使用 X 按钮关闭应用程序 (GUI)。 (如果直接用代码打印就不存在这个问题)

代码相当简单,请看一下:

GUI screenshot

按钮代码:

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);
}
}


}

打印类:

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);


}
}




}

非常感谢。

最佳答案

导致您的问题的原因可能有很多,最明显的是您的数据库操作需要一段时间才能连接、运行并返回结果,因此很难给您一个单一的答案。

从您的问题来看,不清楚单击按钮是否会导致打印任何内容?

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

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

关于java - 按钮操作延迟或退出后执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34592551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com