jlabel加载以前的QRCode不是最新的

时间:2018-01-23 13:51:06

标签: java jlabel qr-code

我目前正在加载当前的QRCode。当我生成qrcode并移动到下一个卡片布局时它会加载前一个qrcode而不是我刚创建的那个。如果没有生成qrcode或我删除它,它什么都不加载。

QRCode生成完美。

我按Actionstener

生成代码

Pconfirm.java

btnConfirm.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        BackEnd.purchase(Double.parseDouble(stotal));
        parentForm.showPanel(MainPage.PPROCESS);
    }
});

Pprocess.java

private JLabel lblQRCode;

private void createUIComponents() {
    ImageIcon imageIcon = new ImageIcon("MyQRCode.png");
    Image image = imageIcon.getImage();
    Image newimg = image.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
    imageIcon = new ImageIcon(newimg);
    lblQRCode = new JLabel(imageIcon);
}

BackEnd.java

public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);    
    Path path = FileSystems.getDefault().getPath(filePath);
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
}
public static String purchase(double amount){
        String text;
        try {
            generateQRCodeImage("bitcoin:"+kit.wallet().currentReceiveAddress().toString()+"?amount="+String.format("%.7f",amount), 20, 20, QR_CODE_IMAGE_PATH);
            text = "QR Code generated. - "+kit.wallet().currentReceiveAddress().toString();
        } catch (WriterException e) {
            text = "Could not generate QR Code, WriterException :: " + e.getMessage();
        } catch (IOException e) {
            text = "Could not generate QR Code, IOException :: " + e.getMessage();
        }
        return text;
    }

1 个答案:

答案 0 :(得分:0)

已解决且已更新代码......

BackEnd.java

public static BufferedImage getQRCodeImage(String amount) throws WriterException{
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode("bitcoin:"+kit.wallet().currentReceiveAddress().toString()+"?amount="+String.format("%.7f",Double.parseDouble(amount)), BarcodeFormat.QR_CODE, 200, 200);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }

Pconfirm.java

btnConfirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                proc2.setvar(lblfinal.getText());
                parentForm.showPanel(MainPage.PPROCESS);
            }
        });

Pprocess.java

public void setvar(String varsent) {
        cost = varsent;
        try {
            imageIcon.setImage(BackEnd.getQRCodeImage(cost));
            lblQRCode.setIcon(imageIcon);
            lblstatus.setText("Amount for " + cost);
            System.out.println(BackEnd.gaddress());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

我做了很多或重组代码的运行方式。这很有效。