我正在尝试使用java.io.printstream
打印epl标签,但是当
我单击“打印”按钮,没有任何反应,打印不出来。
这是我的代码
String printerShareValue = printerTextField.getText().trim();
FileOutputStream printerStream = new FileOutputStream(printerShareValue);
PrintStream ps = new MyPrintStream(printerStream);
ps.print("\f");
ps.print(printTextArea.getText());
ps.flush();
ps.close();
printerTextField
包含要打印的打印机名称。
printTextArea
是我们要打印的文本标签。
class MyPrintStream extends PrintStream {
public MyPrintStream(OutputStream outp) { super(outp); }
@Override
public void print(String s) { super.print(s); }
}
public class TestPrint extends JFrame {
private Container contentPane;
private final int VERTICAL_RIGID_AREA_HEIGHT = 30;
private final int STANDARD_LABEL_HEIGHT = 20;
private final int STANDARD_LABEL_WIDTH = 100;
private JTextField printerTextField;
private JTextArea printTextArea;
public TestPrint() {
super();
contentPane = this.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
setTitle("PrintTest");
addRigidArea(contentPane, 0, VERTICAL_RIGID_AREA_HEIGHT);
setupPrintPanel();
setupPrinterTextArea();
setupButtons();
addRigidArea(contentPane, 0, 20);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsConfiguration gc = getGraphicsConfiguration();
Rectangle gcr = gc.getBounds();
Rectangle thisr = getBounds();
setLocation(new Point(gcr.x + (gcr.width / 2) - (thisr.width / 2), gcr.y
+ (gcr.height / 2) - (thisr.height / 2)));
setVisible(true);
}
private void setupPrintPanel() {
// setup the panel
JPanel panel = setupPanel();
exactly(panel, 500, 40);
addRigidArea(panel, 10, 0);
printerTextField = new JTextField();
// the JComboBox dropdown list element
JLabel databaseLabel = new JLabel("Eltron Label Printer Share");
databaseLabel.setHorizontalAlignment(SwingConstants.RIGHT);
exactly(databaseLabel, 150, STANDARD_LABEL_HEIGHT);
panel.add(databaseLabel);
addRigidArea(panel, 10, 0);
printerTextField = new JTextField();
exactly(printerTextField, 300, STANDARD_LABEL_HEIGHT);
panel.add(printerTextField);
contentPane.add(panel);
}
private void setupPrinterTextArea() {
JPanel panel = setupPanel();
addRigidArea(panel, 10, 0);
printTextArea = new JTextArea();
String s = "N\n"
+ "A50,1150,3,2,2,1,N,\"SHIP TO:\"\n"
+ "A50,950,3,3,2,1,N,\"test\"\n"
+ "B100,384,3,1,3,7,90,B,\"1046305\"\n"
+ "A90,950,3,3,2,1,N,\"tt\"\n"
+ "A130,950,3,3,2,1,N,\"tttt\"\n"
+ "A170,950,3,3,2,1,N,\"tttt\"\n"
+ "A210,950,3,3,2,1,N,\"ttt, CO ttt t\"\n"
+ "A270,1150,3,2,2,1,N,\"ORDER #\"\n"
+ "A270,880,3,2,2,1,N,\"PURCHASE ORDER #\"\n"
+ "A270,200,3,2,2,1,N,\"SHIP DATE\"\n"
+ "A300,1150,3,3,2,1,N,\"087462\"\n"
+ "A300,880,3,3,2,1,N,\"\"\n"
+ "A300,200,3,3,2,1,N,\"06-12-2018\"\n"
+ "A350,1150,3,2,2,1,N,\"TAG:\"\n"
+ "A350,1090,3,3,2,1,N,\"\"\n"
+ "A400,1150,3,2,2,1,N,\"ITEM #\"\n"
+ "A400,880,3,2,2,1,N,\"DESCRIPTION\"\n"
+ "A400,200,3,2,2,1,N,\"QUANTITY\"\n"
+ "A430,1150,3,3,2,1,N,\"2601400 01SI1T(H\"\n"
+ "A430,880,3,3,2,1,N,\"DOUBLE HUB MINI LARGE PLATINUM COATED \"\n"
+ "A430,200,3,3,2,1,N,\"1\"\n"
+ "A480,1150,3,3,2,1,N,\"B02MCL-PLT)[1000]\"\n"
+ "A530,1150,3,4,2,1,N,\"SAM123\"\n"
+ "A480,880,3W,3,2,1,N,\"WITH PAINTED OF HB02MCL-PLT\"\n"
+ "A530,880,3,3,2,1,N,\"FOR MONITORS BETWEEN 2-10LBS.\"\n"
+ "A580,1150,3,3,2,1,N,\"Test sales order note.\"\n"
+ "A610,1150,3,2,2,1,N,\"GROSS WEIGHT\"\n"
+ "A610,880,3,2,2,1,N,\"NET WEIGHT\"\n"
+ "A610,600,3,2,2,1,N,\"CARTON SIZE\"\n"
+ "A640,1150,3,3,2,1,N,\"2.0(Kg)\"\n"
+ "A640,880,3,3,2,1,N,\"3.0(Kg)\"\n"
+ "A640,600,3,3,2,1,N,\"4.0(mm)X0.0(mm)X0.0(mm)\"\n"
+ "P1";
exactly(printTextArea, 500, 550);
printTextArea.setText(s);
panel.add(printTextArea);
addRigidArea(panel, 10, 0);
contentPane.add(panel);
}
private void setupButtons() {
JPanel panel = setupPanel();
exactly(panel, 150, 50);
//The ok Button
JButton okButton = new JButton("Print");
okButton.setHorizontalAlignment(SwingConstants.RIGHT);
okButton.setEnabled(true);
okButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
printButtonAction();
}
});
getRootPane().setDefaultButton(okButton);
okButton.requestFocusInWindow();
panel.add(okButton);
addRigidArea(panel, 10, 0);
contentPane.add(panel);
}
private void printButtonAction() {
try {
if(printerTextField.getText().trim().length() == 0){
JOptionPane.showMessageDialog(null, "Please Enter Eltron Label Printer Share Path To Print Labels Same As In Spaceco ", "Enter Printer", JOptionPane.ERROR_MESSAGE);
return;
}
String printerShareValue = printerTextField.getText().trim();
FileOutputStream printerStream = new FileOutputStream(printerShareValue);
PrintStream ps = new MyPrintStream(printerStream);
ps.print("\f");
ps.print(printTextArea.getText());
ps.flush();
ps.close();
} catch (Exception ex) {
String message = ex.getMessage() + "" + ex.getLocalizedMessage();
JOptionPane.showMessageDialog(null, message, "No Printer", JOptionPane.ERROR_MESSAGE);
}
}
/**
* used to print the labels on the eltron label printer
*/
static class MyPrintStream extends PrintStream {
public MyPrintStream(OutputStream outp) {
super(outp);
}
@Override
public void print(String s) {
super.print(s);
}
}
/**
* Adds some rigid area with given widht and height
*/
protected void addRigidArea(Container c, int width, int height) {
c.add(Box.createRigidArea(new Dimension(width, height)));
}
/**
* Fixes the size to a component
*/
protected void exactly(JComponent jc, int width, int height) {
Dimension d = new Dimension(width, height);
jc.setMaximumSize(d);
jc.setMinimumSize(d);
jc.setPreferredSize(d);
}
/**
* Sets up a JPanel with Box Layout in X-axis
*/
protected JPanel setupPanel() {
JPanel jp = new JPanel();
jp.setLayout(new BoxLayout(jp, BoxLayout.X_AXIS));
return jp;
}
}
你能帮我吗?