Takisp22
Takisp22

Reputation: 41

Why doesn't my setLocation(x,y) work?

I tried to use the cookie.setLocation(x,y). I set my panel's layout to null but it doesn't seem to work. What am I doing wrong? Nothing appears on the window when I run it. I set sizes so it can't be that.

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.Border;
import java.util.*;
public class menus extends JPanel implements ActionListener{
    JPanel p=new JPanel();
    JPanel p2=new JPanel();
    JLabel lbl=new JLabel();
    JLabel lblpr=new JLabel();
    Boolean fin=true;
    Boolean fin2=true;
    Boolean fin3=true;
    Boolean fin4=true;
    int g=5;
    int x=0;
    JLabel lbl2=new JLabel();
    int seconds=0;
    Timer t=new Timer(1000,this);
    int counter=0;
    ImageIcon image = new ImageIcon();
    public menus(){
        lblpr=new JLabel("");
        p.setLayout(null);
        image = new ImageIcon(getClass().getResource(""));
        JButton cookie = new JButton(new ImageIcon(getClass().getResource("cookie-1.gif")));
        Border emptyBorder = BorderFactory.createEmptyBorder();
        p.add(cookie);
        JButton b=new JButton("Buy Grandma(40 cookies)");
        p.add(b);
        cookie.setSize(200,200);
        cookie.setLocation(100,100);
        b.setSize(200,200);
        b.setLocation(100,100);
        p.add(lblpr);
        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(counter>=40){
                    if(x<5){
                        t.start();
                        counter-=40;
                        x++;
                    }else{
                        JOptionPane.showMessageDialog(null, "You bought a maximum of 5 grandmas. To win the game get 200 cookies!");
                    }
                }else{
                    JOptionPane.showMessageDialog(null, "Error you do not have enought cookies to buy a grandma");
                }
            }
        });
        cookie.setBorder(emptyBorder);
        cookie.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                counter++;
                if(counter>=200 && fin==true){
                    fin=false;
                    JOptionPane.showMessageDialog(null, "Woo.. now... em... idk... exit? "
                            + "I mean you must keep on going....."
                            + "\nNext goal 10000 can u do it? i dont think u have the balls to do it");
                    g=1000;
                }
                if(counter>=10000 && fin2==true){
                    fin2=false;
                    JOptionPane.showMessageDialog(null, "Wow... you do got the balls afterall... Well next one is 100000. Ok i dont think you will actually do this one.. "
                            + "\ni mean 5 grandmas by ur side only.. well let me give u a little help since we are getting along. I present u the ULTIMATE GRANDMA. This grandma will give you 300 cookies/s so yeah.. \nGood luck and see you at 100000");
                    g=305;
                }
                if(counter>=100000 && fin3==true){
                    fin3=false;
                    JOptionPane.showMessageDialog(null, "Really? Still playing? jeez... ok i guess the LAST goal is 1 Million.. See you there for ur price. oh yeah heres the BADASS ULTIMATE GRANDMA. 1000 cookies/s.");
                    g=1305;
                }
                if(counter>=1000000 && fin4==true){
                    fin4=false;
                    JOptionPane.showMessageDialog(null, "Well wow.. you really really did it didnt u? Ok here is ur prize.. press ok to claim");
                    ImageIcon i=new ImageIcon(getClass().getResource("Screenshot_1.png"));
                    lblpr.setIcon(i);

                }
                lbl.setText("You have "+counter+" Cookies!");
            }
        });
        lbl = new JLabel("");
        p.add(lbl);
        add(p);
    }
    public void actionPerformed(ActionEvent e){

        if(x==1){
        counter++;
        lbl.setText("You have "+counter+" Cookies!");
        }else if(x==2){
            counter+=2;
            lbl.setText("You have "+counter+" Cookies!");
        }else if(x==3){
            counter+=3;
            lbl.setText("You have "+counter+" Cookies!");
        }else if(x==4){
            counter+=4;
            lbl.setText("You have "+counter+" Cookies!");
        }else if(x==5){
            counter+=g;
            lbl.setText("You have "+counter+" Cookies!");
        }
    }
    public static void main(String Args[]){
        menus gui = new menus();
        JFrame jf = new JFrame();
        jf.setTitle("Yo");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(1280,720);
        jf.setVisible(true);
        jf.setResizable(false);
        jf.getContentPane().add(gui);
    }
}

Upvotes: 0

Views: 1168

Answers (2)

ArcticLord
ArcticLord

Reputation: 4039

If you want to set Location and Size of your elements manually and without any LayoutManager, you need to be consequent.
Set the Layout of your menus extends JPanel class to null. And set Size and Location of your JPanel p.

p.setSize(800,300);
p.setLocation(0,0);
setLayout(null);

Another problem I could see in your code is that you have set your Button and your Cookie at the same position with same size. So you won't see both of them.
And like any other Elements you want to add to this Panel without Layout your need to set Location and Size of your JLabels
But I really need to mention here that using no Layout manager is BAD design!

Upvotes: 0

Arnaud
Arnaud

Reputation: 17534

I see two possibilities here :

  1. Remove p

Get rid of your intermediate p JPanel.

You are adding to the menus JPanel with a FlowLayout (default), a p JPanel. Nothing tells menus what size is your p JPanel, so it comes with size 0/0.

Then you add menus to the content pane, which is okay, since the content pane usually has a default BorderLayout.

Your menus panel will fill all the surface of the content pane, unfortunately, there is nothing visible inside it (due to p being of preferred size 0).

So, remove p, and do all your components adding and stuff, directly on menus panel.

  1. Change menus layout

You may also just change the default Layout of menus :

 setLayout(new BorderLayout());

Here, when you will be adding p (by default, BorderLayout.CENTER), it will fill the whole surface of menus.

Then, the same goes when you add (by default, BorderLayout.CENTER) menus to the content pane, since the content pane also has a BorderLayout.

Upvotes: 1

Related Questions