LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2009, 04:24 AM   #1
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285
Blog Entries: 1

Rep: Reputation: 37
creating a window in java


hello friends,

i want to create a window in java using java.awt.Window.

i tried out the following code:
Code:
import java.awt.*;

public class TestWindow
{
       public static void main(String []args)
       {
	      new CreateWindow();
       }
}

public class CreateWindow extends Window
{
       CreateWindow()
       {
	      //create an instance of Window
	      //the following statement creates an invisible window 
	      Window win = new Window(Window owner);
	      //next we make the window visible.
	      win.setVisible(1);
       }
}

and i got the following error during compilation:

Code:
$ javac TestWindow.java
TestWindow.java:20: ')' expected
              Window win = new Window(Window owner);
                                            ^
TestWindow.java:20: illegal start of expression
              Window win = new Window(Window owner);
                                                  ^
2 errors
can anybody please correct me...........
 
Old 10-01-2009, 04:38 AM   #2
gr33d
Member
 
Registered: Dec 2008
Posts: 41

Rep: Reputation: 15
are you trying to cast the owner object to type Window?

i'm not intimately familiar with awt (i use swing) but the line:
Window win = new Window(Window owner);

should be
Window win = new Window(owner);
 
Old 10-01-2009, 04:41 AM   #3
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285

Original Poster
Blog Entries: 1

Rep: Reputation: 37
actually i got that code from the java docs itself...so i just gave it a shot.
 
Old 10-01-2009, 04:47 AM   #4
gzunk
Member
 
Registered: Sep 2006
Posts: 89

Rep: Reputation: 20
A few things wrong with your code. Here's a fixed version

Code:
import java.awt.*;

public class TestWindow
{
       public static void main(String []args)
       {
	      CreateWindow();
       }
       
       public static void CreateWindow()
       {
	      //create an instance of Window
	      //the following statement creates an invisible window 
	      Window win = new Window(null);
	      
	      win.setSize(300,200);

	      //next we make the window visible.
	      win.setVisible(true);
       }
}
First of all, you wouldn't (normally) create two classes in the same file. You can, but it's not normally done. I've changed your second class to be a method on your first class.

Secondly, the constructor to Window should be passed null if you don't want it to have an owner. And if you want to pass in a variable you don't need to specify the type beforehand (i.e. you would have used just owner, rather than Window owner). And remember you must declare your variables before you use them.

Thirdly, you need to set the size of a window before you will be able to see it.

Fourthly, you don't really want to be using AWT, you should use Swing, and you really don't want to be using a window, a Frame would provide window decorations like scrollbars, a titlebar and a close button at the top.
 
Old 10-01-2009, 05:04 AM   #5
lemon09
Member
 
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285

Original Poster
Blog Entries: 1

Rep: Reputation: 37
@gzunk

but what was wrong if i had made another class.
 
Old 10-01-2009, 05:26 AM   #6
gzunk
Member
 
Registered: Sep 2006
Posts: 89

Rep: Reputation: 20
Nothing inherently wrong, but there can only be one public class per file, and it must have the same name as the file. Your code had two public classes.

Here's another example of how to create a window:

Code:
import javax.swing.*;

public class BetterWindow {

	public static void main(String args[]) {
		
		JFrame jf = new JFrame();
		jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		jf.setSize(300,200);
		jf.setVisible(true);
		
	}	
}
Or this, which does the same thing:

Code:
import javax.swing.*;

public class BetterWindow extends JFrame {

	public BetterWindow() {
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setSize(300,200);
		setVisible(true);
	}
	
	public static void main(String args[]) {
		BetterWindow bw = new BetterWindow();
	}
	
}
In this second example, I create a class called BetterWindow, which has a constructor that sets the default close operation (what happens when you click on the X button) sets the size of the window and sets the window to be visible.

Then during the main method, this constructor is invoked. In the first example, everything is done in the main method.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a launcher for java ap in gnome fabre Linux - General 1 05-13-2006 10:36 PM
Creating a countdown timer window in Fedora Core statman Linux - Software 0 06-18-2005 04:42 PM
creating a window in linux with a 'window' variable??! (openCV) vmok82 Programming 1 02-28-2005 03:38 PM
Java creating a deck of cards djgerbavore Programming 4 10-02-2004 03:57 PM
Creating drivers with Java Jose Muņiz Programming 2 10-03-2003 06:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration