Wednesday, June 30, 2010

Very simple graphics user inteface program in java

Hi.
In this post I will write very small program with user interface in java.




import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class VeryBasicUI {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            
            @Override
            public void run() {
                BasicFrame frame = new BasicFrame();
                frame.showUI();
            }
        });
    }
}

class BasicFrame extends JFrame {
    BasicPanel panel;
    
    public BasicFrame() {
        panel = new BasicPanel();
        getContentPane().add(panel);
    }
    
    public void showUI() {
        setTitle("Basic user interface program");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700,700);
        setVisible(true);
    }
}

class BasicPanel extends JPanel {
    JLabel label;
    add(label);
    
    public BasicPanel() {
        label = new JLabel("Hello world");

    }
}


Program we'll be called from the event dispatch thread:

  SwingUtilities.invokeLater(new Runnable() {
           
            @Override
            public void run() {
                BasicFrame frame = new BasicFrame();
                frame.showUI();
            }
        });
          
In function showUI() I have writte window name, default close operation, size of the window, and we set visible to true.

It is very important to set visible to true. Graphics user interface program with no set visible to true will not show Window. Omission of setting setVisible to true cause many mistakes!!!

public void showUI() {
        setTitle("Basic user interface program"); // set name of window
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,300); // setting the width and height of window
        setVisible(true);
    }

1 comment:

  1. Classic Series S-T-Nite-Tron #37 Ironing with Nylon Countertops
    Classic Series S-T-Nite-Tron #37 Ironing with infiniti pro rainbow titanium flat iron Nylon Countertops. Features titanium color premium-grade aluminum finish. Signature titanium earring posts aluminum handle. ion chrome vs titanium Includes - No aftershokz trekz titanium telescoping steering column.

    ReplyDelete