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: Help removing JFrame inheritance

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Help removing JFrame inheritance

    Hi, I'm new-ish to java so please excuse any mistakes I make. Please Ignore stuff in brackets:[I've been making a dice game That when you press space, the dice "rolls" and a random number is generated. I've been trying to add the class to an html as an applet. I read somewhere that I had to Extend JApplet, but since I already extended JFrame I Can't extend Both. Can and I remove the JFrame inheritance and make my code work with JApplet? if so, How?]
    Code so far:
    EDIT: OK, I think I figured out what I'm supposed to do, but it doesn't display in my browser. Here's my new code:
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import javax.swing.*;
    import java.awt.event.*;
    import java.applet.*;
     
    public class DiceGameApplet extends JApplet
           implements KeyListener,
           ActionListener
    {
    	private JLabel label2 = new JLabel();
        JTextArea displayArea = new JTextArea();
        int width, height;
        JButton button = new JButton("Clear");
        JScrollPane scrollPane = new JScrollPane(displayArea);
        Icon DiceGame = new ImageIcon( getClass().getResource( "dice.gif" ) );
     
        static final String newline = System.getProperty("line.separator");
     
    	    public void init() {
            width = getSize().width;
            height = getSize().height;
    	    button.addActionListener(this);
    	    displayArea.setEditable(true);
    	    displayArea.addKeyListener(this);
    	    scrollPane.setPreferredSize(new Dimension(375, 125));
    	    getContentPane().add(scrollPane, BorderLayout.CENTER);
    	    getContentPane().add(button, BorderLayout.PAGE_END);
    		setLayout( new FlowLayout() );
    		label2.setText( "" );
    		label2.setIcon( DiceGame );
    		label2.setToolTipText( "DiceGame" );
    		label2.setHorizontalTextPosition( SwingConstants.LEFT );
    		label2.setVerticalTextPosition( SwingConstants.BOTTOM );
    		add( label2 );
        }
     
    	    public void keyTyped(KeyEvent e) {}
    	    public void keyPressed(KeyEvent e) {
    		    if(e.getKeyChar() == e.VK_SPACE) {
    		       int d = (int)(Math.random() * 10) % 6 + 1;
    		       displayInfo(d);
    	        }
    		    displayArea.requestFocusInWindow();
    	    }
    	    public void keyReleased(KeyEvent e) {}
     
    	    public void actionPerformed(ActionEvent e) {
    		     displayArea.setText("");
    		     displayArea.requestFocusInWindow();
            }
     
            private void displayInfo(int g){
    			displayArea.setText("");
    		    String keyString = Integer.toString(g);
                displayArea.append(keyString + newline);
                displayArea.setCaretPosition(displayArea.getDocument().getLength());
        }
    }
    What's Wrong with it?
    Thanks in advance!
    Last edited by SendBorg; January 28th, 2012 at 07:41 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help removing JFrame inheritance

    If you're putting it on the web, then you don't need a JFrame. Frames and applets cancel each other out.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    SendBorg (January 28th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help removing JFrame inheritance

    Quote Originally Posted by newbie View Post
    If you're putting it on the web, then you don't need a JFrame. Frames and applets cancel each other out.
    Really? *facepalm*. Thanks, I'll see if I can figure it out now.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,194
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Help removing JFrame inheritance

    See the tutorial about applets:
    Lesson: Java Applets (The Java™ Tutorials > Deployment)

Similar Threads

  1. Java Not Removing Panels?
    By Pantheon8 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 17th, 2011, 07:54 PM
  2. Help: Bug with removing data from a Hash Map
    By seabeach in forum Collections and Generics
    Replies: 4
    Last Post: August 11th, 2011, 11:54 AM
  3. Arraylist removing element
    By Stn in forum Loops & Control Statements
    Replies: 6
    Last Post: January 9th, 2011, 07:14 PM
  4. Removing objects from a hashSet
    By JohnTor in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 02:03 PM
  5. Removing the middle cell
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 7th, 2010, 12:35 PM

Tags for this Thread