我没有太多的编码经验,我只创建了一个非常小的(非常小的)应用程序,之前用于将文件从位置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);
}
}
}
印刷类:
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);
}
}
}
非常赞赏。
答案 0 :(得分:0)
您的问题可能有多种原因,最明显的是,您的数据库操作只需要一段时间来连接,运行和返回结果,因此很难给您一个答案。
根据您的问题,单击按钮是否会导致打印任何内容尚不清楚?
我建议您调试应用程序以帮助找出问题所在,或者至少添加一些日志记录来帮助您。
此外,您应该始终关闭并释放您的流,数据库连接,语句和结果集。