我的程序有问题。矩形在框架的某个区域消失了,我不知道为什么。我最近写了一个类似的程序并且运行得很好。不同之处在于我画的是圆形,而不是矩形。
你能帮我找出问题所在吗?
public class Runner {
public static void main(String[] args) {
AppFrame app = new AppFrame();
app.setVisible(true);
}
}
public class Drawing extends JPanel{
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
private int x;
private int y;
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fill(new Rectangle2D.Double(x, y, 40, 40));
}
public void repaintObject() {
repaint();
}}
public class AppFrame extends JFrame implements ActionListener {
private int height =600;
private int width = 600;
private JButton upButton;
private JButton downButton;
private JButton rightButton;
private JButton leftButton;
private JPanel drawingPanel;
private JPanel buttonsPanel;
private Drawing object;
private int x;
private int y;
public AppFrame() {
this.setLayout(new GridLayout(2,1));
drawingPanel= new JPanel();
buttonsPanel= new JPanel();
drawingPanel.setLayout(new GridLayout(1,1));
buttonsPanel.setLayout(new BorderLayout());
upButton = new JButton("Up");
downButton = new JButton("Down");
rightButton = new JButton("Right");
leftButton = new JButton("Left");
buttonsPanel.add(upButton, BorderLayout.CENTER);
buttonsPanel.add(downButton, BorderLayout.PAGE_END);
buttonsPanel.add(rightButton, BorderLayout.LINE_END);
buttonsPanel.add(leftButton, BorderLayout.LINE_START);
x=100;
y=100;
object = new Drawing();
object.setX(x);
object.setY(y);
drawingPanel.add(object);
this.add(drawingPanel);
this.add(buttonsPanel);
upButton.addActionListener(this);
downButton.addActionListener(this);
rightButton.addActionListener(this);
leftButton.addActionListener(this);
this.setSize(height, width);
setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
System.out.println(x+" "+ y);
if(source == upButton) {
if(y>=50) {
y = y - 50;
object.setY(y);
object.repaintObject();
}
} else if(source == downButton) {
if(y<350) {
y = y+50;
object.setY(y);
object.repaintObject();
}
} else if(source == rightButton) {
if(x<550) {
x = x+50;
object.setX(x);
object.repaintObject();
}
} else if(source == leftButton) {
if(x>=50) {
x=x-50;
object.setX(x);
object.repaintObject();
}
} }}
您可以使用
paint()
/JLabel
来绘制矩形(或任何您想要的形状,只要您覆盖 JPanel
),而不是通过覆盖 paintComponent()
在不同位置绘制矩形),然后使用该组件的 setLocation()
来更改其位置(根据您希望矩形移动的速度 和)。为了避免矩形超出主面板的边界,您可以使用矩形的当前位置、矩形的尺寸以及主面板的尺寸执行一些检查。因此,无论您最初为矩形或面板/框架指定什么大小,或者在程序运行时调整框架窗口的大小,矩形仍然能够在其父组件的边界内移动。下面是一个工作示例。如果您想使用鼠标移动矩形,请查看此处。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class App extends JFrame implements ActionListener {
JLabel label;
private JButton upButton;
private JButton downButton;
private JButton rightButton;
private JButton leftButton;
JPanel p; // main panel
int rectHeight = 40;
int rectWidth = 40;
int speed = 50;
public App() {
label = new JLabel();
label.setBounds(10, 10, rectWidth, rectHeight);
label.setBackground(Color.RED);
label.setOpaque(true);
p = new JPanel();
p.setLayout(null);
p.add(label);
upButton = new JButton("Up");
downButton = new JButton("Down");
rightButton = new JButton("Right");
leftButton = new JButton("Left");
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new BorderLayout());
buttonsPanel.add(upButton, BorderLayout.CENTER);
buttonsPanel.add(downButton, BorderLayout.SOUTH);
buttonsPanel.add(rightButton, BorderLayout.EAST);
buttonsPanel.add(leftButton, BorderLayout.WEST);
upButton.addActionListener(this);
downButton.addActionListener(this);
rightButton.addActionListener(this);
leftButton.addActionListener(this);
this.getContentPane().add(p, BorderLayout.CENTER);
this.getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(640, 480);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
int xPos = (int) label.getLocation().getX();
int yPos = (int) label.getLocation().getY();
int pHeight = (int) p.getSize().getHeight();
int pWidth = (int) p.getSize().getWidth();
if (source == upButton) {
if (yPos - speed > 0)
label.setLocation(xPos, yPos - speed);
else
label.setLocation(xPos, 0);
} else if (source == downButton) {
if (yPos + speed < pHeight - rectHeight)
label.setLocation(xPos, yPos + speed);
else
label.setLocation(xPos, pHeight - rectHeight);
} else if (source == leftButton) {
if (xPos - speed > 0)
label.setLocation(xPos - speed, yPos);
else
label.setLocation(0, yPos);
} else if (source == rightButton) {
if (xPos + speed < pWidth - rectWidth)
label.setLocation(xPos + speed, yPos);
else
label.setLocation(pWidth - rectWidth, yPos);
}
}
public static void main(String[] args) {
new App();
}
}