How to use gui in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to use gui in java

27th Feb 2019, 2:16 PM
VedG
4 Answers
+ 5
import javax.swing.*; public class FirstGUI { public static void main(String args[]){ JFrame f = new JFrame("My First GUI"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(300,300); JButton button1 = new JButton("Press"); f.getContentPane().add(button1); f.setVisible(true); } }
27th Feb 2019, 2:48 PM
Андрей Чачин, Andrew Chachin
Андрей Чачин, Andrew Chachin - avatar
+ 2
// There are 2 ways to create a frame // 1 By creating the object of frame class // 2 By extending frame class ( public class MyFrame extends JFrame ) // Dont forget to import import javax.swing.JFrame; // This is a example by creating the object of frame class: public class MyFrame { public static void main(String[] args){ // Creating instance of JFrame JFrame frame = new JFrame(); // Now use the instance variable (frame) to call some methods frame.setSize(400,400); // Set the window size frame.setLocationRelativeTo(null); // This center your window frame.setVisible(true); // This makes your window visible frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // So you can close your window } } // You cant create gui with sololearns playground..
27th Feb 2019, 3:03 PM
JavaBobbo
JavaBobbo - avatar
0
Thank you for advice....can i know how to use gui in sololearn i am not having pc
27th Feb 2019, 2:51 PM
VedG