|
First GUI Program
|
||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Home |
|
TThis code will
create a simple GUI. We will be using SWING, which is the newer and more capable
GUI framework |
||||||||||||||
| // Import the needed packages import javax.swing.*; import javax.swing.event.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; // The class we create uses and adds to - extends- JFrame. JFrame is where the GUI is displayed class firstgui extends JFrame { //define an ImageIcon called icon1. The image is specified by name ImageIcon icon1 = new ImageIcon("Globe1.gif"); // Create a JLabel called label1 using the ImageIcon icon1 JLabel label1 = new JLabel(icon1); // This is called the contructor: it initializes the class with some values before starting firstgui() { // We need to get the content pane and create a containter from it Container container1 = getContentPane(); // Add the label to the container container1.add(label1); // set sizes and visibility setSize(400,400); setVisible(true); } } // This is the main class: this is where it happens public class runfirstgui2 { public static void main(String[] bargs) { // create a new firstgui object firstgui firstguiobject = new firstgui(); } } |
||
| Result: |
||
|
||
|
Copyright 2005 Gehan Ameresekere |