How to add button on frame using swing in java
Swing - JButton Tutorials and ExamplesThe JButton class is used to add platform independent buttons in an independent application. In this tutorial, we will learn how to create a button in a swing application and how to tweak their appearance as needed. I have also shared some code snippets that may be useful for you when developing a swing application.
Swing JButton example
This example shows how to create a button in a swing application and how to add it to a frame. In addition, we will look at various methods of the JButton class.
Import javax.swing.JButton;
Import javax.swing.JFrame;
Public class JButtonExample {
JButtonExample () {
/ * JFrame is a top level container (window)
* Where we will be adding our button
* /
JFrame frame = new JFrame ();
// create button
JButton b = new JButton ("Click Me ..");
/ * This method specifies the location and size
* Button's. In method setBounds (x, y, width, height)
* x, y) are coordinates from top left
* Corner and the remaining two arguments are width
* And button height.
* /
b.setBounds (50,50,90, 50);
// add button to frame
frame.add (b);
// Setting the frame size. This is the size of the window
frame.setSize (300,200);
frame.setLayout (void);
frame.setVisible (true);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public static void main (String [] args) {
new JButtonExample ();
}
}
0 Comments