我已经编写了这个java代码,我希望蓝色矩形像一个边界球一样四处移动.但是当我试图运行代码时,它似乎停留在一个位置并且在一行中移动.以下是代码.
import javax.swing.*;
import java.awt.*;
public class MoveBody extends JFrame {
BallPanel ballpanel;
JFrame frame;
int X=15;
int Y=15;
boolean up=false;
boolean down=true;
boolean left=false;
boolean right=true;
public static void main(String[]args)
{
new MoveBody().go();
}
private void go()
{
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ballpanel = new BallPanel();
frame.getContentPane().add(BorderLayout.CENTER, ballpanel);
frame.setVisible(true);
frame.setResizable(false);
frame.setSize(500,500);
frame.setLocation(375, 55);
moveIt();
}
class BallPanel extends JPanel
{
public void paintComponent(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.WHITE);
g.fillRect(10, 10, this.getWidth()-10,this.getHeight()-10);
g.setColor(Color.BLUE);
g.fillRect(X,Y, 50, 50); …Run Code Online (Sandbox Code Playgroud) 根据相邻兄弟选择器的定义,以下代码应该有效.但事实并非如此.我似乎没有发现任何错误.
<!DOCTYPE html>
<html>
<head>
<style>
#p2+h4{
color:red;
}
</style>
</head>
<body>
<p>
<p id="p2">This is the sibling of the selected para</p>
<p>
<h4>this should not be colored</h4>
</p>
<h4>this should be colored</h4>
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)