我尝试使用cookie.setLocation(x,y)。我将面板的布局设置为null,但似乎不起作用。我究竟做错了什么?当我运行它时,窗口上什么都没有出现。我设置了尺寸,所以不能那样。

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);
    }
}

最佳答案

我在这里看到两种可能性:


删除p


摆脱您的中间p JPanel。

您将使用menus(默认),FlowLayout JPanel添加到p JPanel。
什么都没有告诉menus您的p JPanel的大小是多少,因此它的大小为0/0。

然后将menus添加到内容窗格中,这是可以的,因为内容窗格通常具有默认的BorderLayout

您的menus面板将填满内容窗格的所有表面,不幸的是,其中没有可见的内容(由于p的首选大小为0)。

因此,删除p,然后直接在menus面板上添加和填充所有组件。


更改menus布局


您也可以只更改默认布局menus

 setLayout(new BorderLayout());


在这里,当您添加p(默认情况下为BorderLayout.CENTER)时,它将填充menus的整个表面。

然后,将menus添加到内容窗格中(默认情况下为BorderLayout.CENTER)也是如此,因为内容窗格也具有BorderLayout。

10-08 16:15