Olá bom dia a todos,
Para implementar uma caixa de diálogo, você deriva uma classe de [i]JDialog[/i]. É essencialmente o mesmo processo que derivar a janela para um aplicativo a partir de [i]JFrame[/i].
mais precisamente:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogTeste {
public static void main(String[] args) {
DialogFrame frame = new DialogFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible( true );
}
}
class DialogFrame extends JFrame{
public DialogFrame(){
setTitle("DialogFrame");
setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
JMenuBar menuBar = new JMenuBar();
setJMenuBar( menuBar );
JMenu fileMenu = new JMenu("File");
menuBar.add( fileMenu );
JMenuItem aboutItem = new JMenuItem("About");
aboutItem.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent event )
{
if( dialog == null ) // primeira vez
dialog = new AboutDialog( DialogFrame.this );
dialog.setVisible( true );
}
});
fileMenu.add( aboutItem );
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent event )
{
System.exit(0);
}
});
fileMenu.add( exitItem );
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
private AboutDialog dialog;
}
class AboutDialog extends JDialog{
public AboutDialog( JFrame owner ){
super( owner, "About DialogTeste", true );
add( new JLabel( "<html><h1><i>Core Java</i></h1><hr>www.javafree.org</html>" ) , BorderLayout.CENTER );
JButton ok = new JButton("Ok");
ok.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent event ){
setVisible( false );
}
});
JPanel panel = new JPanel();
panel.add( ok );
add( panel, BorderLayout.SOUTH );
setSize( 250, 150 );
}
}
é um exemplo completo, e pode auxiliar no entendimento da classe JDialog. Espero ter ajudado.
atenciosamente