Java GUI
Local
External
Dialog Boxes, Premade
# Information dialog box with 'ok' button
JOptionPane.showMessageDialog(frame, "Dialogs Text Content", "Dialogs Title");
# Warning dialog box with 'ok' button
JOptionPane.showMessageDialog(frame, "Dialogs Text Content", "Dialogs Title", JOptionPane.WARNING_MESSAGE);
# Error dialog box with 'ok' button
JOptionPane.showMessageDialog(frame, "Dialogs Text Content", "Dialogs Title", JOptionPane.ERROR_MESSAGE);
# No decorator symbol dialog box with 'ok' button
JOptionPane.showMessageDialog(frame, "Dialogs Text Content", "Dialogs Title", JOptionPane.PLAIN_MESSAGE);
# Custom symbol dialog box with 'ok' button
JOptionPane.showMessageDialog(frame, "Dialogs Text Content", "Dialogs Title", JOptionPane.INFORMATION_MESSAGE, icon);
# Confirm dialog box with 'yes' 'no' 'cancel'
JOptionPane.showConfirmDialog(frame, "Dialogs Text Content");
Menubar to JFrame
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
menuBar = new JMenuBar();
menu = new JMenu("File");
menuBar.add(menu);
menuItem = new JMenuItem("exit");
menu.add(menuItem);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doExit();
}
});
Exit, run a handler as exiting
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
handleExit();
}
});
Exit on JFrame close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Exit Programmatically
// exit a JFrame program by doing this:
private void doExit() {
processEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
Application Example
- Use JFrame for a standalone app.