Showing posts with label GUI. Show all posts
Showing posts with label GUI. Show all posts

Tuesday, May 22, 2012

Simple GUI in Java

with a frame we will take a window or form, from application in desktop

with a simple example will output string " Hello World"
here's the code..


DemoFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class DemoFrame {
  private JLabel label;
  
  public DemoFrame() {  
    label = new JLabel("Hallo Dunia...");
  }  
  
  public void createAndShowGUI() {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Demo Frame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(label, BorderLayout.CENTER);
    //frame.pack();
    frame.setBounds(0, 0, 300, 200); //ukuran frame
    frame.setLocationRelativeTo(null); //frame di tengah layar
    frame.setVisible(true);
  }
  
  public static void main(String[] args) {      
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        DemoFrame app = new DemoFrame();
        app.createAndShowGUI();
      }
    });      
  }  
}