jframe in java
JFrame
is a Swing component in Java that represents a top-level window with a border and a title. It is a basic container for building user interfaces in Java Swing, and provides the foundation for creating graphical user interfaces (GUIs) in Java.
Here is a simple example of how to create a JFrame
in Java:
import javax.swing.JFrame; public class MyFrame extends JFrame { public MyFrame() { super("My First JFrame"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { MyFrame frame = new MyFrame(); } }
In this example, we create a new class that extends the JFrame
class. We override the default constructor of the JFrame
class to set the title, size, and default close operation of the window. We then create a new instance of this class in the main
method, which creates and displays the JFrame
.
When you run this code, a new window will appear with the title "My First JFrame". The window will be 400 pixels wide and 300 pixels tall. When the user clicks the close button on the window, the program will exit. You can add other Swing components to this window, such as JButton
, JLabel
, and JTextField
, to create a complete GUI.