Results 1 to 10 of 10
  1. #1
    grizdog is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Can't have two JFrames in different classes?

    This seems like it must be a standard thing that the textbooks warn you about, but I can't find it.

    All of this occurred inside Eclipse, sometimes all in the same package, sometimes not.

    I wrote a very simple class which used JFrame, along with JPanel and JLabel, to open a window with some text in it. That's all. I used

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

    I can run the program several times and get the same result, it opens the window properly with the text in it every time.

    Then I copied everything to a new class. Other than renaming the class, and the name of the file, everything was exactly the same. But instead of opening the window, it sends some unreadable output to the console.

    The original program seems to be the only one that can open windows. Is this a feature? What do I do to get different programs to have the same functionality?

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by grizdog View Post
    This seems like it must be a standard thing that the textbooks warn you about, but I can't find it.
    No. You've introduced a bug somehow.

    But instead of opening the window, it sends some unreadable output to the console.
    The "unreadable" information is likely quite readable and informative, and without it and your code, we are probably not going to be able to help you.

    The original program seems to be the only one that can open windows. Is this a feature? What do I do to get different programs to have the same functionality?
    Hard to say. Again, you'll likely want to post your code, if it's a reasonable size, and the error message. If you do post code, please read the link below on how to use code tags.

    Much luck.

  3. #3
    grizdog is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    OK, here is the code, moused out of Eclipse. I couldn't find a special formatting option for code. It's supposed to exhibit a Vigenere square.

    Java Code:
    package vigSquare;
    
    /**
     * @author alex
     *
     */
    
    import javax.swing.*;
    import java.awt.*;
    
    public class Square extends JFrame {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private static final int FRAME_WIDTH	= 800;
    	private static final int FRAME_HEIGHT	= 800;
    	
     
    	private JLabel inputLine;
    	private String str;
    	
    	public static void main(String[] args) {
    		
    		Square myFrame = new Square();
    		myFrame.setVisible(true);
    		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new JPanel();
    		myFrame.getContentPane().add(panel);
    		myFrame.pack();
    	}
    	public Square(){
    		getContentPane().setLayout(new GridLayout(27, 0));
    
    		
    		str = new String("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z");
    		inputLine = new JLabel(str);
    		setSize(FRAME_WIDTH,FRAME_HEIGHT);
    		setTitle("Vigenere Square");
    		
    		for (int i = 0; i<26; i++){
    			inputLine = new JLabel(str.toString());
    			add(inputLine);
    			str = str.substring(2) + " " + str.charAt(0);
    			inputLine = new JLabel(str);
    		}
    	}
    	 
    }

    And here is a little bit of output. It goes up to 2047. There were no error messages.

    0
    1
    2
    3
    4
    5

    Moderator Edit: Code tags added
    Last edited by Fubarable; 12-13-2010 at 01:32 AM. Reason: Moderator Edit: Code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've added code tags to your post above. To see how to do this yourself, again, please see the link in my signature below labeled "how to use code tags".

    Your code looks OK. Now what happens when you try to copy it? Can you show the copied code and any error messages it produces?

  5. #5
    grizdog is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the code tags. Got it. The output also included non-ascii characters that didn't show up when I posted.

    This *is* the code that failed, and there were no error messages. It just didn't open a window, and printed (the wrong output, not a Vigenere square) to the console. It is the same as the code that worked, except for the names of the classes and the filename. Eclipse showed no errors and no warnings.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by grizdog View Post
    This *is* the code that failed, and there were no error messages. It just didn't open a window, and printed (the wrong output, not a Vigenere square) to the console. It is the same as the code that worked, except for the names of the classes and the filename. Eclipse showed no errors and no warnings.
    Hm, then I'm stumped, sorry. That being said, how are you copying the code? When I want to copy a file in Eclipse, I usually right-click on the file in the Package Explorer, then click "copy". Then I right click the package where I want the copy to go (or the same package if I want two copies in the same package), and then click "paste".

  7. #7
    grizdog is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Thanks. Yes, it is something very strange.

    I just copied the code the way I would do it in a text editor - highlight, Ctrl-C, open the new file, Ctrl-V. I'll probably use your method (no pun intended) from now on.

  8. #8
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Is the new file in the same package?

  9. #9
    grizdog is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    I tried it both ways, first in the same package and then in a new one. Exited Eclipse in between. Same result.

  10. #10
    grizdog is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default OK, it must be an Eclipse problem

    I built both files in Eclipse, but then copied them out of the workspace and deleted the "package" declaration, and byte-compiled and ran both with "javac" and "java" at the command line, and both worked fine.

    This is the Eclipse that came with the Ubuntu distribution. Anyone know if it is buggy this way?

Similar Threads

  1. help with jframes
    By pao09 in forum AWT / Swing
    Replies: 0
    Last Post: 04-15-2009, 03:10 PM
  2. jframes
    By ddj in forum AWT / Swing
    Replies: 0
    Last Post: 03-24-2009, 04:15 PM
  3. linking 2 Jframes
    By suhaib1thariq in forum New To Java
    Replies: 9
    Last Post: 02-13-2009, 02:30 AM
  4. two JFrames
    By kirtesh4u in forum New To Java
    Replies: 0
    Last Post: 11-17-2008, 09:26 PM
  5. Handling Two JFrames
    By hiranya in forum AWT / Swing
    Replies: 2
    Last Post: 11-05-2007, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •