jlabel 加载以前的 QRCode 不是当前的
jlabel loading previous QRCode not current
我目前正在加载最新的 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;
}
已解决并更新了代码...
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();
}
}
我为代码的方式做了很多或重组运行。这很好用。
我目前正在加载最新的 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;
}
已解决并更新了代码...
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();
}
}
我为代码的方式做了很多或重组运行。这很好用。