Status
Not open for further replies.

ganesh_68

Distinguished
Feb 3, 2010
1
0
18,510
Hello,
Respected sir I am ganesh Patekar.
when i type java GUI code in notepad and it compiles in command prompt and alos runs but after running it hangs and i cant close GUi frame and cant type anything in command prompt please tell me what to do
example of program:-

import java.awt.*;

class simple1 extends Frame
{

Panel p1;
List l1,l2,l3;

public simple1()
{

p1= new Panel();

l1= new List();
l2= new List(3);
l3= new List(3,true);
add(p1);
p1.add(l1);
p1.add(l2);
p1.add(l3);

l1.add("Om");
l1.add("Om");
l1.add("Om");

l2.add("Om");
l2.add("Om");
l2.add("Om");

l3.add("Om");
l3.add("Om");
l3.add("Om");


setSize(400,400);
setVisible(true);

}
public static void main(String args[ ])
{
simple1 s1= new simple1();
}
}
 
Solution
You need to put it in a JFrame, and then tell the JFrame what action should be taken when the close button is clicked

Code:
package test;

import java.awt.*;

import javax.swing.JFrame;

public class test extends Frame {

	Panel p1;
	List l1, l2, l3;

	public test() {
		//create the JFrame
		JFrame testFrame = new JFrame();

		//create the panel, and add everything you want to the panel
		p1 = new Panel();

		l1 = new List();
		l2 = new List(3);
		l3 = new List(3, true);
		add(p1);
		p1.add(l1);
		p1.add(l2);
		p1.add(l3);

		l1.add("Om");
		l1.add("Om");
		l1.add("Om");

		l2.add("Om");
		l2.add("Om");
		l2.add("Om");

		l3.add("Om");
		l3.add("Om");
		l3.add("Om");

		//add the panel to the JFrame
		testFrame.add(p1);

		//set the...

kyeana

Distinguished
May 21, 2008
230
0
18,860
You need to put it in a JFrame, and then tell the JFrame what action should be taken when the close button is clicked

Code:
package test;

import java.awt.*;

import javax.swing.JFrame;

public class test extends Frame {

	Panel p1;
	List l1, l2, l3;

	public test() {
		//create the JFrame
		JFrame testFrame = new JFrame();

		//create the panel, and add everything you want to the panel
		p1 = new Panel();

		l1 = new List();
		l2 = new List(3);
		l3 = new List(3, true);
		add(p1);
		p1.add(l1);
		p1.add(l2);
		p1.add(l3);

		l1.add("Om");
		l1.add("Om");
		l1.add("Om");

		l2.add("Om");
		l2.add("Om");
		l2.add("Om");

		l3.add("Om");
		l3.add("Om");
		l3.add("Om");

		//add the panel to the JFrame
		testFrame.add(p1);

		//set the JFrame visable and set the default size
		testFrame.setVisible(true);
		testFrame.setSize(400, 400);

		//set the what happens when you click the close button (in 
		//this case, exits the program)
		testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public static void main(String args[]) {
		test s1 = new test();
	}
}

I would highly recommend going over Sun's Swing tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/

It outlines everything for build GUI's in java, and also shows you how to use NetBeans to build a GUI graphically instead of all by code (much much much easier i promise).
 
Solution
Status
Not open for further replies.