Swing Basic Program
Here's a basic program for swing in Java that creates a simple window with a button:
javaimport javax.swing.*;
public class SwingExample
{
public static void main(String[] args)
{
// Create a JFrame
JFrame frame = new JFrame("Swing Example");
// Create a JButton
JButton button = new JButton("Click Me");
// Add the button to the frame
frame.getContentPane().add(button);
// Set the size of the frame
frame.setSize(300, 200);
// Set the window to close when the //close button is clicked
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Display the frame
frame.setVisible(true);
}
}
This program imports the javax.swing
package, which contains the classes necessary for creating a GUI with Swing. It creates a JFrame
object with the title "Swing Example", and a JButton
object with the label "Click Me". It adds the button to the frame using the getContentPane()
method, sets the size of the frame to 300x200 pixels, sets the window to close when the close button is clicked using the setDefaultCloseOperation()
method, and makes the frame visible using the setVisible()
method. When you run this program, you should see a window with a button labeled "Click Me"
Comments
Post a Comment