为什么我的 setLocation(x,y) 不起作用?
Why doesn't my setLocation(x,y) work?
我尝试使用 cookie.setLocation(x,y)
。我将面板的布局设置为 null
,但它似乎不起作用。我究竟做错了什么?当我 运行 时,window 上什么也没有出现。我设置了尺寸,所以不可能是那样。
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。
您正在使用 FlowLayout
(默认)、p
JPanel 添加到 menus
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。
如果您想手动设置元素的 Location
和 Size
并且没有任何 LayoutManager,则需要相应的。
将 menus extends JPanel
class 的布局设置为 null
。并设置 JPanel p
的 Size
和 Location
。
p.setSize(800,300);
p.setLocation(0,0);
setLayout(null);
我在您的代码中看到的另一个问题是您将 Button 和 Cookie 设置在相同位置且大小相同。所以你不会看到他们两个。
与您想要添加到此面板但没有布局的任何其他元素一样,您需要设置 JLabels
的 Location
和 Size
但我真的需要在这里提一下,不使用布局管理器是糟糕的设计!
我尝试使用 cookie.setLocation(x,y)
。我将面板的布局设置为 null
,但它似乎不起作用。我究竟做错了什么?当我 运行 时,window 上什么也没有出现。我设置了尺寸,所以不可能是那样。
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。
您正在使用 FlowLayout
(默认)、p
JPanel 添加到 menus
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。
如果您想手动设置元素的 Location
和 Size
并且没有任何 LayoutManager,则需要相应的。
将 menus extends JPanel
class 的布局设置为 null
。并设置 JPanel p
的 Size
和 Location
。
p.setSize(800,300);
p.setLocation(0,0);
setLayout(null);
我在您的代码中看到的另一个问题是您将 Button 和 Cookie 设置在相同位置且大小相同。所以你不会看到他们两个。
与您想要添加到此面板但没有布局的任何其他元素一样,您需要设置 JLabels
的 Location
和 Size
但我真的需要在这里提一下,不使用布局管理器是糟糕的设计!