Conversation with Merlin [email protected] · Sat Dec 09 2023

how to create multimple fenetre in java swing

To create multiple windows (fenetres) in Java Swing, you can follow these steps:

  1. Create a new class for each window you want to display.

  2. In each class, extend the JFrame class to create a new JFrame window.

  3. Add components, such as buttons, labels, etc., to the JFrame using its content pane.

  4. Set the properties of the JFrame, such as size, location, and visibility.

  5. Instantiate and display the windows as needed in your main application or where required.

Here's a basic example to illustrate how you can create multiple windows in Java Swing:

import javax.swing.*; public class FirstWindow extends JFrame { public FirstWindow() { setTitle("First Window"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { new FirstWindow(); new SecondWindow(); }); } } class SecondWindow extends JFrame { public SecondWindow() { setTitle("Second Window"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocation(400, 0); setVisible(true); } }

In this example, we created two windows (FirstWindow and SecondWindow) by extending the JFrame class. We then set properties and displayed these windows in the main method.

You can create as many windows as needed by following similar steps for each window. I hope this helps! Let me know if you need further assistance.