Skip to content

Instantly share code, notes, and snippets.

@figengungor
Created March 9, 2013 21:27
Show Gist options
  • Save figengungor/5125815 to your computer and use it in GitHub Desktop.
Save figengungor/5125815 to your computer and use it in GitHub Desktop.
Java GUI >> JLabel, JButton, JPanel
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MyGUI extends JFrame
{
JLabel jl; //Made global so all methods can use this
//GUI representation of System.out.println();
JButton jb;
JPanel jp,jp2; // panels are containers for objects like buttons, labels etc...
public MyGUI()
{
setTitle("Tutorial");
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp = new JPanel();
jp2 = new JPanel();
jl = new JLabel("This text is written with JLabel");
jp.add(jl); //adding label to first panel
add(jp); //adding panel(container) to frame
//that one will be displayed on top of the frame as default
jb = new JButton("Press the JButton");
jp2.add(jb); //adding button to second panel
add(jp2, BorderLayout.SOUTH); //changing position of our panel to south of our frame.
//If we don't do that, two panels will overlap at top
}
public static void main(String[] args)
{
MyGUI frame = new MyGUI();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment