Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I'm supposed to see text in this window

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I'm supposed to see text in this window

    import javax.swing.*;
    import java.awt.*;
     
    public class HelloJava {
            public static void main( String[] args ) {
                        JFrame frame = new JFrame( "Hello, Java!" );
                        frame.setSize( 300, 300 );
                        frame.setVisible( true );
                        class HelloComponent extends JComponent {
                            public void paintComponent( Graphics g ) {
                                                            g.drawString( "Hello, Java!", 125, 95 );                                        
                        HelloComponent hello = new 
                        HelloComponent();
                        frame.add( "hello" );
                                }
                            }
            }
    }

    I'm following this E-book i got from the android market (O'Reilly), and so far i got a lot of minor errors, and the book doesn't show how to fix it, so i was hoping you could help me out. (also, this is my first serious attempt to learn programming).
    I'm using Eclipse


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm supposed to see text in this window

    got a lot of minor errors,
    Copy and paste the error messages here.

    Have you written any classes before? You should probably start with writing a simple class instead of copy some code that is not well formed and trying to make that work.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I'm supposed to see text in this window

    Actually, i get only 2 errors with this class. In the sideline, at the last line (frame.add( "hello" ) i see this:



    If i understood it correctly, i should a text like "Hello Java!" in the middle of the screen, and when i would resize the window, but the text would not move with it.
    This is the first step i've attempted to serious programming, but it kinda blows. I read alot of pages where i learn the basics, but when i write the code, i know very little of what i'm actually writing. I have been told that this code is a GUI component, and it's not clever to learn Java with that. (i've ordered another book which works with BlueJ)
    Last edited by Ypmaha; March 9th, 2012 at 12:32 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm supposed to see text in this window

    You should go through a java tutorial or text book before trying to code a program. There are several things wrong with the code you are posting.

    One very basic thing that will make the code easier to read and understand is proper formatting.
    The posted code is a mess. I don't have a tutorial on the proper coding style but you definitely need to find one if you are going to post code and ask others for help.

    To fix your code: move the class definition out of the main method.

Similar Threads

  1. Can anybody help me fix this, what I am supposed to do is at the top
    By peplo1214 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2012, 07:05 AM
  2. Not able to read from a textfiel the way it's supposed to.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 27th, 2012, 04:11 AM
  3. Replies: 1
    Last Post: December 17th, 2011, 03:32 AM
  4. Not sure what I'm supposed to do...
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2011, 11:13 PM
  5. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM