1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package test.dialog;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(final String... args) {
SwingUtilities.invokeLater(Main::launchAtEDT);
}
private static void launchAtEDT() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
final var button = new JButton("Click me!");
final var toolBar = new JToolBar();
toolBar.setFloatable(false);
toolBar.add(button);
final var frame = new JFrame("Test");
button.addActionListener(event -> {
final var dialog = createDialog(frame);
dialog.setVisible(true);
});
frame.setLayout(new BorderLayout());
frame.add(toolBar, BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(800, 600));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static JDialog createDialog(JFrame parent) {
final var closeButton = new JButton("Exit");
final var toolBar = new JToolBar();
toolBar.setFloatable(false);
toolBar.add(Box.createHorizontalGlue());
toolBar.add(closeButton);
final var label = new JLabel("This is a dialog box");
final var dialog = new JDialog(parent, "Dialog");
closeButton.addActionListener(event -> dialog.setVisible(false));
dialog.setLayout(new BorderLayout());
dialog.setModal(true);
dialog.setResizable(false);
dialog.add(label, BorderLayout.CENTER);
dialog.add(toolBar, BorderLayout.SOUTH);
dialog.setSize(300, 200);
dialog.setLocationRelativeTo(parent.getContentPane());
return dialog;
}
} |
Partager