Advertisement
Guest User

Java swing example

a guest
Jan 26th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import javax.swing.JButton;
  2. import javax.swing.JFrame;
  3. import javax.swing.JPanel;
  4.  
  5.  
  6. public class Main {
  7.  
  8.    
  9.     public static void main(String... args){
  10.         initComponents();
  11.     }
  12.    
  13.     static void initComponents(){
  14.         JFrame frame = new JFrame("Frame title in here duh");//frame object holds all the stuff
  15.        
  16.         JPanel panel = new JPanel();//jpanel is the basic component which u can put buttons, textfields in etc
  17.        
  18.         panel.setLayout(null);//layout null is absolute layout, you use X,Y for positioning
  19.        
  20.         JButton button = new JButton("text for button");// a simple button
  21.         button.setBounds(100,100,200,50);//position x position y,width and height
  22.        
  23.         panel.add(button);//must add all components we have to the panel
  24.        
  25.         frame.add(panel);//after everything add the panel
  26.        
  27.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//in Java by default if u press the X button of the window
  28.         //it would not stop your program but just minimize the gui and still run the thread in background
  29.         //useful for minimizing, services etc, and this line sets it to close when you press X
  30.        
  31.         frame.setBounds(600,600,600,600);//spawning the frame at x,y with size something,something
  32.        
  33.         frame.setResizable(false);//cannot resize it :D
  34.         frame.setVisible(true);//makes it visible
  35.     }
  36.    
  37.    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement