【发布时间】:2023-03-14 01:17:01
【问题描述】:
当用户输入密码并点击确定按钮时,密码将被加密并存储在JTextArea。这工作正常。但我想在showConfirmDialog 和showMessageDialog 弹出窗口中添加自定义徽标。我尝试使用以下代码,但自定义图像(徽标)未显示在消息弹出窗口中
public static void main(String[] args) {
Box box = Box.createHorizontalBox();
JLabel label = new JLabel("Enter your password : ");
box.add(label);
JPasswordField passwordField = new JPasswordField(24);
box.add(passwordField);
final ImageIcon icon = new ImageIcon("C:\\Users\\Test\\Internet.png");
int button = JOptionPane.showConfirmDialog(null, box, "Enter your password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.NO_OPTION, icon);
if (button == JOptionPane.OK_OPTION) {
String password = new String(passwordField.getPassword());
String encryptedPassword;
if (password != null && !password.equals("")) {
byte[] bytesEncoded = Base64.encodeBase64(password.getBytes());
JTextArea richTextField = new JTextArea(10, 10);
encryptedPassword = new String(bytesEncoded);
richTextField.setText(encryptedPassword);
richTextField.setOpaque(false);
richTextField.setEditable(false);
JOptionPane.showMessageDialog(null, richTextField);
} else {
JOptionPane.showMessageDialog(null,
"Password cannot be null. Please enter password to encrypt.");
}
}
}<br>
我将ImageIcon 对象作为参数传递给JoptionPane.showConfirmDialog。但是当我运行它时,我没有看到弹出窗口中显示任何图像。我不确定我在这里做错了什么。
注意:我需要在两个弹出窗口中都显示一个自定义图像。 showConfirmDialog 和 showMessageDialog
任何帮助将不胜感激
【问题讨论】: