閃爍的代碼並不重要,而不是如何捕捉點擊對話框外部(如果有的話)時觸發的事件。
在Windows上,如果您嘗試在對話框外單擊,則聽起來是'ding'
聲音,我只是想知道是否有可能跳過該事件的流行。這可能嗎?如何讓用戶在外部點擊時模態JDialog閃爍
謝謝。
SSCCE:
import com.sun.awt.AWTUtilities;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ModalDialogSSCCE {
private JFrame frame = new JFrame("Modal Dialog SSCCE - click to display dialog");
private JPanel dialogContent = new JPanel();
private ModalDialog dialog;
public static void main(String[] args) {
new ModalDialogSSCCE();
}
public ModalDialogSSCCE() {
Action closeAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
};
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 500));
JButton closeButton = new JButton("Close");
closeButton.addActionListener(closeAction);
dialogContent.setSize(200, 200);
dialogContent.setBackground(Color.DARK_GRAY);
dialogContent.add(closeButton, BorderLayout.CENTER);
dialog = new ModalDialog(dialogContent, closeAction);
frame.getContentPane().addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
dialog.setVisible(true);
}
});
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public class ModalDialog extends JDialog {
private JComponent content;
private final int shadowSize = 5;
public ModalDialog(JComponent content, Action closeAction) {
this.setLayout(null);
this.content = content;
this.setUndecorated(true);
AWTUtilities.setWindowOpaque(this, false);
this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
this.getContentPane().add(content);
content.setBounds(shadowSize, shadowSize, content.getWidth(), content.getHeight());
content.getInputMap(JComponent.WHEN_FOCUSED)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
content.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
content.getActionMap().put("close", closeAction);
}
@Override
public void paint(Graphics graphics) {
super.paint(graphics);
Graphics2D g = (Graphics2D) graphics.create();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(new Color(50, 50, 50, 50));
g.setStroke(new BasicStroke(shadowSize));
g.drawRoundRect(shadowSize/2 + 1, shadowSize/2 + 1, getWidth() - shadowSize - 2, getHeight() - shadowSize - 2, 1, 1);
content.repaint();
}
public void toggle() {
if (isVisible()) this.setVisible(false);
else this.setVisible(true);
}
@Override
public void setVisible(boolean visible) {
if (visible) {
this.setBounds(frame.getX() + (frame.getWidth() - content.getWidth())/2,
frame.getY() + (frame.getHeight() - content.getHeight())/2,
content.getWidth() + 2 * shadowSize,
content.getHeight() + 2 * shadowSize);
}
super.setVisible(visible);
}
public JComponent getContent() {
return content;
}
}
}
*「在Windows上,如果您確實嘗試在對話框外點擊,它聽起來像是'叮''*這是不正確的。 '丁'只有在對話框是模態的時候纔會發生,用戶點擊擁有對話框的***應用程序的另一部分。*** –
一旦你看到事件,你想做什麼? – trashgod
@AndrewThompson這是正確的。我暗示點擊是在應用程序內,但不在對話框本身。感謝您的澄清。 – rtheunissen