我遇到了一个让我发疯的问题。我通过这行代码调用一个方法:
private void btnGetFolderMouseClicked(MouseEvent e) {
MessageController.showAlert(AlertBox.AlertType.OK);
}
并且调用此方法:
public class MessageController {
public static void showAlert(AlertBox.AlertType alertType) {
AlertBox alertBox = new AlertBox("Test12345678910dfkjsdgmdgbu<xdfg<bdxgj ghfhftz rgdx", alertType);
alertBox.getRootPane().setOpaque(false);
//alertBox.getContentPane ().setBackground (new Color(0, 0, 0, 0));
//alertBox.setBackground (new Color(0, 0, 0, 0));
alertBox.setVisible(true);
// BDialog dialog = new BDialog();
// dialog.setVisible(true);
System.out.println("Nach Box");
//alertBox.invalidate();
}
}
这是我的JDialog课程:
public class AlertBox extends JDialog {
private ImageIcon downSide = new ImageIcon(getClass().getResource("/AlertBox/Down_Side.png"));
private ImageIcon leftDownCorner = new ImageIcon(getClass().getResource("/AlertBox/Left_Down_Corner.png"));
private ImageIcon leftSide = new ImageIcon(getClass().getResource("/AlertBox/Left_Side.png"));
private ImageIcon leftUpCorner = new ImageIcon(getClass().getResource("/AlertBox/Left_Up_Corner.png"));
private ImageIcon rightDownCorner = new ImageIcon(getClass().getResource("/AlertBox/Right_Down_Corner.png"));
private ImageIcon rightSide = new ImageIcon(getClass().getResource("/AlertBox/Right_Side.png"));
private ImageIcon rightUpCorner = new ImageIcon(getClass().getResource("/AlertBox/Right_Up_Corner.png"));
private ImageIcon upSide = new ImageIcon(getClass().getResource("/AlertBox/Up_Side.png"));
private String text;
private Rectangle2D fontSize;
private Font font = new Font("Arial", Font.BOLD, 13);
private ImagedButton btnOK;
public enum AlertType{
OK,
Warning,
Error
}
private AlertType alertType;
public AlertBox(String text, AlertType alertType){
// Set JDialog properties
super(new JFrame(), true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setUndecorated(true);
setLocationRelativeTo(null);
this.text = text;
this.alertType = alertType;
btnOK = new ImagedButton("OK");
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
exitAlertBox();
}
});
//this.add(btnOK);
}
@Override
public void paint(Graphics g) {
// Create Graphics2D Object
Graphics2D g2d = (Graphics2D) g.create();
// Get the Size
fontSize = StringMetrics.getBounds(g2d, font, text);
// set the fontSize
int width = (int) fontSize.getWidth() + 5 + leftSide.getIconWidth() + 5 + rightSide.getIconWidth();
int height = leftUpCorner.getIconHeight() + 5 + (int)fontSize.getHeight() + 5 + btnOK.getHeight() + 5 + leftDownCorner.getIconHeight();
this.setSize(width, height);
//this.replaceButton(width, height);
// Draw Left Upper Corner
System.out.println(leftDownCorner.getIconWidth() + " ; " + leftDownCorner.getIconHeight());
g.drawImage(leftUpCorner.getImage(), 0, 0, this);
g.drawImage(new ImageIcon(getClass().getResource("/Buttons/Button_Down.png")).getImage(), 21, 20, this);
// Draw Upper Side
//g.drawImage(upSide.getImage(), leftUpCorner.getIconWidth(), 0, 5 + (int) fontSize.getWidth() + 5, upSide.getIconHeight(), this);
// Draw Right Upper Corner
//g.drawImage(rightUpCorner.getImage(), this.getWidth() - rightUpCorner.getIconWidth(), 0, this);
// Draw Left Side
//g.drawImage(leftSide.getImage(), 0, leftUpCorner.getIconHeight(), leftSide.getIconWidth(), 5 + (int)fontSize.getHeight() + 5, this);
// Draw Right Side
//g.drawImage(rightSide.getImage(), width - rightSide.getIconWidth(), rightUpCorner.getIconHeight(), rightSide.getIconWidth(), 5 + upSide.getIconHeight() + 5, this);
// Draw Left Downer Corner
//g.drawImage(leftDownCorner.getImage(), 0, height - leftDownCorner.getIconHeight(), this);
// Draw Right Downer Corner
//g.drawImage(rightDownCorner.getImage(), this.getWidth() - rightUpCorner.getIconWidth(), height - rightDownCorner.getIconHeight(), this);
// Draw Downer Side
//g.drawImage(downSide.getImage(), leftDownCorner.getIconWidth(), height - downSide.getIconHeight(), 5 + (int) fontSize.getWidth() + 5, downSide.getIconHeight(), this);
// Draw the text
//g2d.drawString(text, 10, 10);
//g2d.drawString(text, leftUpCorner.getIconWidth() + 5, (int)fontSize.getHeight() + 8);
if (this.alertType == AlertType.OK){
}
}
private void replaceButton(int width, int height){
// paint OK-Button
int testWidth = width - (btnOK.getWidth() / 2);
int testHeight = height - btnOK.getHeight() - 5;
btnOK.setBounds(testWidth, testHeight, btnOK.getWidth(), btnOK.getHeight());
}
private void exitAlertBox(){
this.dispose();
}
}
问题是,窗口应该(在此版本的代码中)显示2个图像。嗯它确实..但有时..但大多数运行它没有。
当我调试我的代码时,看起来像'paint'方法被调用两次,但我找不到发生这种情况的行。
我做错了吗?
感谢您的帮助:)