Java Tutorial
In java programming, we can also use JFrame to create a Graphical user interface (GUI) using JFrame class.
In Java JFrame, A container must be created to add some components to the JFrame.
Below code creates a container.
Container container = frame.getContentPane();
Need to create a container in Jframe and container is going to have various controls.
// Creates button with text 'Test Button' JButton button = new JButton("Test Button"); // Creates button with text 'Test Button' Container container = frame.getContentPane(); // Adding button to container of frame. container.add(button);
// importing swing package for JFrame, Jbutton controls import javax.swing.*; // importing to use Container class import java.awt.*; public class ButtonTest { public static void main(String[] args){ // Create JFrame for GUI frame and title as 'Add Button in JFrame Java Code' JFrame frame = new JFrame("Adds Button in JFrame Java Code"); // Creates button with text 'Test Button' JButton button = new JButton("Test Button"); //Creates a container which is going to have various controls like button.. Container container = frame.getContentPane(); // Adding button to container of frame. container.add(button); frame.setSize(500,200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }Output:
Once executing this java program, Frame with a button will be popped up as above and closes if we click close button in the frame title.
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page