我尝试使用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);
}
}
答案 0 :(得分:1)
我在这里看到两种可能性:
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
的整个表面。
然后,当您向内容窗格添加(默认情况下为BorderLayout.CENTER)menus
时也是如此,因为内容窗格也有BorderLayout。
答案 1 :(得分:0)
如果您想手动设置元素的Location
和Size
而没有任何LayoutManager,则需要进行后续处理。
将menus extends JPanel
课程的布局设置为null
。并设置Size
的{{1}}和Location
。
JPanel p
我在代码中看到的另一个问题是,您将Button和Cookie设置在相同大小的相同位置。所以你不会看到他们两个
与您要添加到此面板而没有布局的任何其他元素一样,您需要设置p.setSize(800,300);
p.setLocation(0,0);
setLayout(null);
的{{1}}和Location
个
但我真的需要提到的是,不使用布局管理器就是BAD设计!