import javax.swing.*; import java.awt.*; import java.awt.event.*; /* * MyDialog.java is a simple Java application to demo display * of a dialog box. */ public class MyDialog extends JPanel implements ActionListener{ JFrame nsFrame; /** Creates the GUI shown inside the frame's content pane. */ public MyDialog(JFrame frame) { JButton DBut = new JButton ("Pop up a dialog box!"); DBut.setActionCommand("DB"); DBut.addActionListener(this); nsFrame=frame; add(DBut); } public void actionPerformed(ActionEvent e) { if ("DB".equals(e.getActionCommand())) //ok dialog JOptionPane.showMessageDialog(nsFrame, "Some approriate message."); } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("MyDialog"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create and set up the content pane. MyDialog newContentPane = new MyDialog(frame); newContentPane.setOpaque(true); //content panes must be opaque newContentPane.setBorder( BorderFactory.createEmptyBorder(20,30,20,30)); //T,L,B,R frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }