Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Question [SOLVED]JFrame, isn't visible sometimes..

    Hi.
    When i start my application, sometimes i get a blank, Jframe(It's supposed to have black background) and when i press my start button, the game dosen't start, and sometimes it works perfectly. The console dosen't say any errors, so i'll post the code of all frames that's included.

    Starter class, the Frame for the game.
    PHP Code:
    public class Starter extends JFrame{
    
    public static void main(String[]args){
        new Starter();
    }
    
    public Starter(){
            super("The Cube");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            setLocation(750, 250);
            setResizable(false);
            setSize(600, 400);
            Cube cu = new Cube();
            addKeyListener(cu);
            add(cu);
        }
    Cube class, the class with the game.
    PHP Code:
    public class Cube extends JPanel implements ActionListener,KeyListener{
    
        public Cube(){
            setVisible(true);
            setSize(600, 400);
            setBackground(Color.BLACK);
            setLocation(750, 250);
            addKeyListener(this);
            player = new Starter(20, 330);
    ////////////////////////////////////////////////////////////////        
            lvl1part1 = new Starter(0, 200);
            lvl1part2 = new Starter(230, 200);
            lvl1win = new Starter(150, 0);
    ////////////////////////////////////////////////////////////////
            begin = new Timer(5, this);
            begin.start();
            new Menu();
        }
    The menu , it shows up next to the game frame. The Menu class is inside the Cube class.
    PHP Code:
    class Menu extends JFrame implements ActionListener{
    
        private JButton level1;
        private JButton level2;
        
    public Menu(){
        super("Menu");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setLayout(null);
        setResizable(false);
        setLocation(450, 250);
        level1 = new JButton("Level 1");
        level1.addActionListener(this);
        level1.setBounds(0, 80, 100 ,50);
        add(level1);
        
        level2 = new JButton("Level 2");
        level2.addActionListener(this);
        if(!lvl1done){
        level2.setEnabled(false);
        } else if(lvl1done){
            level2.setEnabled(true);
        }
        level2.setBounds(101, 80, 100 ,50);
        add(level2);
    }
    Last edited by AndroidAppNewbie; 03-06-2011 at 02:17 PM.

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

    Default

    It's the order of things that matter here: First add your components to the JFrame, then call setVisible(true). You'll also want to learn how to use layout managers and pack to full advantage.

  3. #3
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    It's the order of things that matter here: First add your components to the JFrame, then call setVisible(true). You'll also want to learn how to use layout managers and pack to full advantage.
    Hey thanks, it worked by changing in positions of setVisible and components, thanks. I'll change the thread to solved.

    EDIT: I can't seem to find the [SOLVED] switch..so i'll simply say it here. [SOLVED]..
    EDIT2: Nvm
    Last edited by AndroidAppNewbie; 03-06-2011 at 02:25 PM.

Similar Threads

  1. GUI is visible but content is not.
    By seemant.bisht in forum AWT / Swing
    Replies: 3
    Last Post: 10-07-2009, 06:28 PM
  2. JTextField not visible?
    By McChill in forum New To Java
    Replies: 2
    Last Post: 05-20-2009, 12:17 AM
  3. Tab or Table not visible
    By madhuvanthi2312 in forum SWT / JFace
    Replies: 1
    Last Post: 04-25-2009, 09:00 AM
  4. Address bar not visible
    By Akashchopra521 in forum New To Java
    Replies: 0
    Last Post: 12-03-2008, 07:13 AM
  5. constructor ... is not visible
    By emceenugget in forum New To Java
    Replies: 2
    Last Post: 09-27-2008, 05:12 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
  •