Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java - make window\s 1

Status
Not open for further replies.

ernieah

Programmer
Oct 27, 2002
42
0
0
US
(1) I am NEWBY to java. The following code works FINE, and
creates a "window" on the desktop, but
my attempt to make TWO windows, with a second program, by adding a section (a second "instantion"?) to this same
code (I thought) did NOT succeed: see the second sample of
code below this one.
1 // -----------------------------------------------
2 // java source program: "try_make_window_v2.java"
3 // page-
4 // java lesson by self-study student - ernie brown
5 // Using textbook: "Beginning JAVA 2", by Ivor Horton
6 // Page-783
7 // ------------------------------------------------
8 // "Test bed" script for this trial is:
9 //
10 // ./exec_java_program.ksh try_make_window_2 <enter>
11 //
12 // (Note: DON'T use the ".java" file-extension
13 // on the command-line. Enter (correctly) AS SHOWN)
14 // ---------------------------------------------------------
15 // java source program: "try_make_window_v2.java"
16 // (DON'T use the ".java" part on the command.line
17 // - when compiling with "javac")
18 // ---------------------------------------------------------
19
20 import javax.swing.JFrame;
21 import java.awt.Point;
22 import java.awt.GraphicsEnvironment;
23
24
25 public class try_make_window_v2
26 {
27 // the window object
28
29 static JFrame aWindow = new JFrame("This is ernies CENTERED - title");
30
31 public static void main(String[] args)
32 {
33
34 Point center =
35 GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
36 int windowWidth = 400;
37 int windowHeight = 150;
38
39 // set position and size
40
41 aWindow.setBounds(center.x-windowWidth/2, center.y-windowHeight/2,
42 windowWidth, windowHeight);
43
44
===========================================================
(2) My atttempt to create TWO 'windows" - which
does NOT succeed:

1 // -----------------------------------------------
2 // java source program: try_make_two_windows_v2.java"
3 // java lesson by self-study student - ernie brown
4 // Using textbook: "Beginning JAVA 2", by Ivor Horton
5 // ------------------------------------------------
6 // ./exec_java_program.ksh try_make_two_windows_v2 <enter>
7 // (Note: DON'T use the ".java" file-extension
8 // on the command-line.)
9 // -------------------------------------------------------
10
11 import javax.swing.JFrame;
12 import java.awt;
13
14 class sam {
15
16 public static void main(String[] args)
17 {
18
19 // --------------------------------------------------------------------------------------- 20 // instantiate the FIRST window object
21
22 static JFrame aWindow = new JFrame("This is ernies FIRST Window Title");
23 {
24
25 int windowWidth = 400;
26 int windowHeight = 150;
27 aWindow.setBounds(50, 100,
28 windowWidth, windowHeight);
29 aWindow.setVisible(true);
30 aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31
32
33 } //End_of_1st_window_instantiation
34 //----------------------------------------------------------------------------------------
35 // --------------------------------------------------------------------------------------- -
36 // instantiate the SECOND window object
37
38 static JFrame anotherWindow = new JFrame("This is ernies SECOND Window Title");
39 {
40
41 int windowWidth = 500;
42 int windowHeight = 250;
"try_make_two_windows_v2.java" 58L, 216
=========================================================
(3) I think I have some FUNDAMENTAL lack of
understanding about "instatiation", since all my
variations to create a code MYSELF, based on a
code which works (ONE window) "from a book", but
extended by me to give (I thought) one more
instantioation than the original (successful) program
did NOT work.

What am I mis-understanding, here? I DO think I am
having some trouble (at least) understanding some of
these java concepts.

(4) Thanx in advance for any help you can give me with this.
Please refer to my line-numbers, so I can follow your
explanatioon better.

End-of-memo: Best to you..from ernieah.
I don't sell books, or work for any
publisher or computer company.
 
Did you start that book from page 1 or from page sevenhundret and something?

Did you mention that your code was cut off in both cases?

If you use codetags, and complete code, that is much more helpful, because we can than cut-and-paste it and find out the linenumber ourself in our editor.

While the first 18, 9 lines aren't off much help.

The naming conventions aren't class try_make_window_v2, but class TryMakeWindowsV2. Is the samplecode using static JFrames? That's not your friend, if you try to build more than one JFrame.

Did you work through the chapters explaining the main-method, the static modifier, and do you know the difference between JFrame and JWindow?

don't visit my homepage:
 
Did you start that book from page 1 or from page sevenhundret and something?

I started the book from the START, and (it seemed, to me) to
be understandable from the START, and onward, since my first program (literally "from the book", to create and display ONE window) worked fine. Obviously, there's some things I don't yet understand, since my attempt to "test" my (alleged) understanding of the idea, by attempting to make TWO windows,
did NOT work.
===========================================================
"Did you mention that your code was cut off in both cases?"

I'm not sure what you mean by "cut off". Do you meant that
I omitted some of my code (acccidently or delibverately)?

So, I'm not sure how to reply to your comment here,
since I don't understand what your question meant.

As far as I know I did NOT omit, or chop-off, any of my code.
===========================================================
"If you use codetags, and complete code, that is much more helpful, because we can than cut-and-paste it and find out the linenumber ourself in our editor."

Great idea, I think. My original reason for using my original line numbers was to make it easy (I thought) for anyone to IDENTIFY part\s of my code, by my line-number, when commenting about particular line\s. Perhaps I made it more difficult for the comment-er.

"Code tags" is a good idea. I will try to do\use that in the future. Thanks for mentioning that..
===========================================================
"The naming conventions aren't class try_make_window_v2, but class TryMakeWindowsV2. Is the samplecode using static JFrames? That's not your friend, if you try to build more than one JFrame."
I'm not sure about "naming conventions" yet. I used underbars simply because I'm "used to them". I don't\didn't know if that was "bad form" - perhaps you aare telling me it is "bad form".

The "naming" I used for my FIRST code, worked OK, to write, and execute, successfully, the program. So, I supposed that the "naming" style I used for the second program was OK, too (whether or not it was "bad form").

It was my impression that the CLASS-name and the file name had to be identical - regardless of the "naming style" used.
And, of course, as I said, it worked for the first program.
===========================================================
"Did you work through the chapters explaining the main-method, the static modifier, and do you know the difference between JFrame and JWindow?"

I thought perhaps I understod what the "main-method" was, but I realize I don't yet know how to USE to EXECUTE the program - although I DO understand (I think) that that's what it's for!

I'm stilL learning how to USE the "main" method - to cause execution.
----------------------------------------------------------
No, I DON'T yet know the difference between JFrame and JWindow. I think each of these ("given, free) classes have
lots of methods, and I'm trying to learn WHICH (super)class
has whatever method I need to do for a particular programming task.

A lot to learn.
===========================================================
Thanks for your comments, and for the time, effort, patience and energy to make them.
Bye for now from ernieah
==========================================================


End-of-memo: Best to you..from ernieah.
I don't sell books, or work for any
publisher or computer company.
 
Well - this is a forum for professionals, and we can't lead you through the process of becoming a programmer.

We can just give a few hints.

CamelCase instead of Camel_case is not that much important, but there are some strong conventions about the first letter of a name.

Classnames and Interfaces shall start with an uppercase, so CamelCase is a class, an Interface - maybe a constructor.

Attributes and methods start with a lowercase letter, local variables too.

Packagenames are completly lowercase - not mixed, and finals (constants) completly uppercase.

java.awt.Color.BLUE is easily classified: java.awt is the package name, Color the class, BLUE something final.

Violating these conventions will not stop compiler and JVM from working, but makes your code less readable.

---
JFrame vs. JWindow - both exist, and you mix the names in your code. I assume you're not interested too much in the differences by now. The simplest differnce is, that a Window by itself is not decorated, which means, it has no Frame with Buttons for closing, minimizing, calling system-functions on it, while a Frame is a Frame (and has those controls).

Ok - let's go for an application with two JFrames:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JFrame1and2 extends JFrame implements ActionListener
{
	private static int offset = 0;

	private JTextArea jta;
	private JButton jb;

	public JFrame1and2 (String name)
	{
		super (name);
		JPanel mainpanel = new JPanel ();
		mainpanel.setLayout (new BorderLayout ());
		this.getContentPane ().add (mainpanel);

		jb = new JButton ("bye bye");
		jb.addActionListener (this);
		jta = new JTextArea ();
		mainpanel.add (jta, BorderLayout.CENTER);
		mainpanel.add (jb, BorderLayout.SOUTH);

		setSize (400, 400);
		setLocation (100 + offset, 100+offset);
		setVisible (true);
		offset += 400;
	}

	public void actionPerformed (ActionEvent e)
	{
		String cmd = e.getActionCommand ();
		if (cmd.equals ("bye bye"))
		{
			dispose ();
		}
	}

	public static void main (String args[])
	{
		new JFrame1and2 ("Frame 1");
		new JFrame1and2 ("Frame Two");
	}
}
You needn't extend JFrame but may build a Class which has a Frame which is used for the frame-operations, but it is common practice to do so and extend JFrame.

If both share a lot of attributes, they may be different objects of the same kind (class). Else you might inherit one from the other or build two differently specialized frames, and instantiate them.


don't visit my homepage:
 
Maybe you should consider using an online tutorial from Sun rather than that book.

Appart from that, I'd like to remark the idea of naming conventions. As Stefan says, it will compile, but the rest of the Java programmers won't have coffee with you.

Cheers,
Dian
 
This is one of the main reasons why I haven't been frequenting this site for a while.

I have always thought that this site functions best when people ask fairly specific questions; ones which have a tight scope. A lot of us who give our time to try to help people here do have jobs to attend to and can't find the time to answer a lengthy, broadly-scoped, question.

Ask one or two specific and narrow-scoped questions to get you moving. If they answers help, then you will often go for a short bit until you need more help. Then post another specific question in a new thread. You will get more help that way. Well, you will from me, at least.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top