我有一个代码,我应该在 JPanel 上返回矩形区域和周长。但是当我执行时,什么都没有发生。我怀疑我的方法中某处有错误,因为我相信其余的都可以。我会给予所有帮助。我只是要在 JPanel 中向您展示我的代码。
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rektanglar extends JPanel {
Rektanglar r1 = new Rektanglar ();
@Override
public void paintComponent (Graphics g) {
super.paintComponent (g);
g.drawString ("Rektanglar",10,20);
this.setBackground(Color.WHITE);
g.setColor(Color.BLUE);
g.fillRect(r1.getX(),r1.getY(), r1.getWidth(), r1.getHeight());
}
public int Y;
public int X;
public int width;
public int height;
public int Perimeter;
public int Area;
Rektanglar (){
width = 10;
height = 10;
X = 0;
Y = 0;
}
public void Rectangle(int x, int y, int w, int h) {
X = x;
Y = y;
width = w;
height = h;
}
public void setX(int X ){
this.X = X;
}
public int getX(int X){
return X;
}
public void setY(int Y){
this.Y = Y;
}
public int getY( int Y){
return Y;
}
public int getWidth( int width){
return width;
}
public int getHeight(int height){
return height;
}
public int getPerimeter(){
return (width + width + height + height );
}
public int getArea(){
return (height * width);}
}
}