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 7 of 7

Thread: Help Fixing Code and Painting(Java Applet)

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help Fixing Code and Painting(Java Applet)

    I've run into some sort of a conundrum in my code. I am a 1st year college student in Java, so im not really experience, but i am doing a applet for a test grade and am having trouble trying to get it run. What im trying to do is get some textfields, labels, and a jframe with a jpanel to run them on, but i am drawing up blanks. so far i've gotten:

     import javax.swing.JApplet;
    import java.awt.Graphics;
    import javax.swing.JTextField;
    import java.util.Timer;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class superclass extends JApplet
    {
    public JTextField one;
    public JTextField two;
    public JTextField three;
    public JTextField four;
    JPanel big = new JPanel();
    JFrame bigger = new JFrame();
    Timer timer1 = new Timer();
     
    public void init()
    {
    JTextField one = new JTextField(7);
    JTextField two = new JTextField(7);
    JTextField three = new JTextField(7);
    JTextField four = new JTextField(7);
     
    JLabel onelabel = new JLabel("Car 1:");
    JLabel twolabel = new JLabel("Car 2:");
    JLabel threelabel = new JLabel("Car 3:");
    JLabel fourlabel = new JLabel("Car 4:");
     
    bigger.add(big);
    big.add(one);
    big.add(two);
    big.add(three);
    big.add(four);
    }
     
    public void paint(Graphics g)
    {
     
     
    }
    }
    the biggest trouble for me is getting the jframe to show up with the panel and it's components, and also sizing the jframe and panel and getting lines to show up on the panel. i have scare knowledge on how paint works as my teachers zooms out of the class, so any help is greatly appreciated, like if the frame and panel are needed at all. ciao, and thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Fixing Code and Painting(Java Applet)

    Please edit your post and wrap your code withcode tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Be sure the code is properly formatted with indentations for nested statements.
    If there is no code in the paint() method, remove it so it doesn't do any harm.

    You should read the tutorial about how to create a GUI: http://docs.oracle.com/javase/tutori...ing/index.html


    Why are you writing an applet and using a JFrame. The two do NOT normally go together.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    LovellHoliday (March 26th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Fixing Code and Painting(Java Applet)

    so could i do all my coding and painting without a JFrame? i was under the impression that they had to be together

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Fixing Code and Painting(Java Applet)

    Applets are special classes that execute in a browser.
    JFrames are normally used in applications executed by the java program.

    They normally do NOT go together.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    LovellHoliday (March 26th, 2013)

  7. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Fixing Code and Painting(Java Applet)

    OK, i really appreciate that because i must've been getting the wrong info. Did you see my code? i was wondering if i could cut out the frame and the panel and paint the objects i have there on it, like the textfields and labels. im in the middle of making a racecar applet and this is just the beginning. also, any tips on how to do drawLine correctly? i have nothing show up when i do it.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help Fixing Code and Painting(Java Applet)

    You should read the tutorial at the link I posted above to see how to build a Swing GUI.
    Also see this tutorial about how to do custom painting:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    LovellHoliday (March 26th, 2013)

  10. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    25
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help Fixing Code and Painting(Java Applet)

    thank you! i feel like i go on and finish this soon, perhaps by 12 tonight for at least the textfield and the labels. much appreciated.

Similar Threads

  1. Help fixing my Java program! Reading files
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 27th, 2013, 05:51 PM
  2. LinkedStack - need help fixing code; should be quick fix
    By ash12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 28th, 2012, 07:12 AM
  3. help fixing the code of gui
    By aperson in forum AWT / Java Swing
    Replies: 1
    Last Post: January 21st, 2012, 07:20 AM
  4. Output in applet keeps painting over the top of itself
    By SashaThompson in forum Java Applets
    Replies: 1
    Last Post: October 9th, 2011, 11:21 AM
  5. Need help fixing Binary Search Tree code
    By fistpunch in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2010, 11:22 AM