[编辑]好的由于马尔特Kölle,它被删除之后的“c”的计算停止。还有的“C”是不是按的问题,但我已经注意到了另一个问题。如果产卵按钮被按下多次,我不知道如何避免速度变量“SW”和“SH”叠加。我不能设置,因为它的内部类中的速度变量等于零。我已经更新了代码。
我决定使用AWT,其中有一个按钮,“B”在产生另一个按钮“C”在屏幕上移动中心,在Java中创建一个简单的按钮游戏。当“C”被点击将被删除。 “C”绕着不错,但我有两个问题。
额外。之后,这些问题都是固定的,下一步就是要能够生成多个按钮具有相同的属性。如果任何人有建议,他们将不胜感激,但不是必需的。
我有好几个系统的输出来检测这样的问题,所以这就是我怎么知道它仍然计算。这是我的第一个代码项目,所以,如果有任何其他的方式也可以改进,请让我知道。谢谢!
public class Main {
Frame f = new Frame("Button Game");
Button b = new Button("Spawn Button");
Button c = new Button("Click Me!");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
int fh = f.getHeight();
int fw = f.getWidth();
boolean cExists;
Main () {
f.setSize(screenWidth/2,screenHeight/2); //Sets frame size
f.setLocation(screenWidth/4,screenHeight/4); //Sets frame location
f.setLayout(null); //Sets layout manager
f.setVisible(true); //Sets frame to be visible
System.out.println("Frame height:" + f.getHeight());
System.out.println("Frame width:" + f.getWidth());
fh = f.getHeight() + 20;
fw = f.getWidth();
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.out.println("Window closed by window listener");
f.dispose();
System.exit(1);
}
});
f.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
fw = f.getWidth();
fh = f.getHeight();
System.out.println("Frame size changed to " + f.getSize());
b.setBounds(fw/2 - 60,fh/2 - 15, 120, 30);
System.out.println("Button 'b' re-centered to " + b.getLocation());
if (fw <= 320 || fh <= 180) {
f.setSize(320,180);
}
}
});
f.add(b);
b.setBounds(fw/2 - 60,fh/2 - 15, 120, 30);
System.out.println("Button 'f' spawned at " + b.getLocation());
b.setVisible(true);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int rn1 = ThreadLocalRandom.current().nextInt(30, fw - 130);
int rn2 = ThreadLocalRandom.current().nextInt(50, fh - 60);
f.add(c);
cExists = true;
c.setBounds(rn1, rn2, 100, 30);
System.out.println("Button 'c' created at " + c.getLocation());
int sh = ThreadLocalRandom.current().nextInt(-5, 10);
int sw = ThreadLocalRandom.current().nextInt(-5, 10);
System.out.println("Speed vertical: " + sh);
System.out.println("Speed horizontal: " + sw);
Timer timer = new Timer();
class remindTask extends TimerTask {
public void run() {
if (cExists) {
c.setBounds(c.getX() + sw, c.getY() + sh, 100, 30);
System.out.println("Button 'c' moved to " + c.getLocation());
if (c.getX() >= fw-130) {
c.setBounds(30 + sw, c.getY() + sh, 100, 30);
} else if (c.getY() >= fh-60) {
c.setBounds(c.getX() + sw, 30 + sh, 100, 30);
} else if (c.getX() <= 30) {
c.setBounds(fw - 130 + sw, c.getY() + sh, 100, 30);
} else if (c.getY() <= 30) {
c.setBounds(c.getX() + sw, fh - 15 + sh, 100, 30);
}
} else {
timer.cancel();
//sw = 0; cant because it is inside an internal class. Maybe outsource to a method?
//sh = 0; Error:(94, 33) java: local variables referenced from an inner class must be final or effectively final
}
}
}
timer.schedule(new remindTask(), 0, 100);
}
});
c.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.remove(c);
cExists = false;
System.out.println("'cExists' set to false");
System.out.println("Removed button 'c' at " + c.getLocation());
}
});
}
public static void main(String[] args) { //Calls code
System.out.println("Code beginning"); //Logs beginning of runtime
try { //Runs Main through run
Main f = new Main();
f.run(args);
}
catch (Exception e){ //NOT SURE
e.printStackTrace();
}
}
public void run (String[] args) throws Exception{ //New method to call non-static things
System.out.println("Frame created");
}
你试图牛逼检查,如果cExists为True。但要做到这一点,你将需要两个“==”,而不是唯一的一个。
77号线:
public void run() {
if (cExists == true) {
c.setBounds(c.getX() + sw, c.getY() + sh, 100, 30);
检查是否布尔是真正的清洁方法可能是这样的:
if(cExists){
我试图在我的项目,它为我工作。
编辑:一个等号之间“=”和两个等号“==”的区别:
一个等号是赋值操作。你用它来赋值给一个变量。 例:
int number = 7
在这里,你是在价值7赋给变量“数”
两个等号是用来检查一些条件,例如:
if(number == 7){
// do something
}
这是你在找什么,因为这意味着“如果数量等于7”。