How to set the location of “JOptionPane.showMessageDialog”

前端 未结 3 1533
闹比i
闹比i 2020-12-06 18:41

I want to make JOptionPane.showMessageDialog message appear

  • Any place in the screen.
  • Relative to JFrame. (not at the centre of the JFram
3条回答
  •  青春惊慌失措
    2020-12-06 19:13

    import javax.swing.JDialog;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    
    public class CustomDialog extends JDialog {
        private JPanel myPanel = null;
        private JButton yesButton = null;
        private JButton noButton = null;
    
        public CustomDialog(JFrame frame, boolean modal, String myMessage) {
        super(frame, modal);
        myPanel = new JPanel();
        getContentPane().add(myPanel);
        myPanel.add(new JLabel(myMessage));
        yesButton = new JButton("Yes");
        myPanel.add(yesButton);
        noButton = new JButton("No");
        myPanel.add(noButton);
        pack();
        //setLocationRelativeTo(frame);
        setLocation(200, 200); // <--
        setVisible(true);
        }
    }
    

提交回复
热议问题