我的目标是创建一个包含矩形的类,然后使用它并在其他类中对其进行更改。
我试图编写此代码并创建对象Rect rect = new Rect();
,但是当我启动程序时,矩形框没有显示。
我也尝试用window.add(rect);
添加它,但是有相同的问题,我确定我做错了,但我真的不知道是什么。
我尝试的另一件事是从其他类Rect.drawRect(g);
调用方法,但随后它要求输入“ Argument”,并且是否像在drawRect方法中一样添加了Argument g,它说“ g无法解析为变量” “
我希望有人能解释并告诉我我做错了什么,也很高兴知道如何制作矩形或圆形,然后在其他课程中使用它,并可能更改其颜色和大小,我才发现如何在一堂课上做。
import javax.swing.JFrame;
public class Main extends Rect{
public static void main(String[] args ) {
JFrame window = new JFrame("test");
window.setSize(1000, 800);
window.setVisible(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rect rect = new Rect();
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rect extends JPanel{
public void drawRect(Graphics g){
g.setColor(Color.RED);
g.fillRect(100, 100, 200, 200);
}
}
答案 0 :(得分:0)
UDP:您需要覆盖void paintComponent(Graphics g)
而不是void drawRect(Graphics g)
并在方法内部调用super.paintComponent(g)
。然后,您可以使用window.add(rect);
。
感谢@FredK进行纠正
答案 1 :(得分:0)
最重要的是,您需要编写一些代码来绘画。这是通过重写Rect类中的paintComponent
方法来完成的,如下所示:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(100, 100, 200, 200);
}
第二个问题是您希望能够从其他类更改矩形的颜色和大小。举一个简单的例子,可以通过在Rect
类内添加一些静态值来轻松完成此操作:
public static Color myColor = Color.RED;
public static int myX = 100;
public static int myY = 100;
public static int myWidth = 200;
public static int myHeight = 200;
现在更新您的绘画方法以使用静态值:
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(myColor);
g.fillRect(myX, myY, myWidth, myHeight);
}
现在,无论何时何地使用Rect
面板,它都将根据静态值显示矩形。
例如,下面是一个简单且有效的程序,请注意它如何使用以下内容:
//create Rect
Rect rect = new Rect();
//set the size of the new panel
rect.setPreferredSize(new Dimension(800, 600));
//add the rect to your JFrame
window.add(rect);
//now you can change the color for all Rect instances
//Note how I use Rect instead of rect, however, both will work.
Rect.myColor = Color.BLUE;
//And dont forget to repaint it if you want to see the changes immediatly
rect.repaint();
完整的示例,主类:
import javax.swing.JFrame;
import java.awt.Color;
public class Main{
//Note how we don't need to extend the Rect class (It just adds confusion)
public static void main(String[] args ) {
JFrame window = new JFrame("test");
window.setSize(1000, 800);
window.setVisible(true);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create Rect
Rect rect = new Rect();
//set the size of the new panel
rect.setPreferredSize(new Dimension(800, 600));
//add the rect to your JFrame
window.add(rect);
//now you can change the color for all Rect instances
//Note how I use Rect instead of rect, however both will work.
Rect.myColor = Color.BLUE;
//And dont forget to update it
rect.repaint();
}
}
和Rect类:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Rect extends JPanel{
public static Color myColor = Color.RED;
public static int myX = 10;
public static int myY = 10;
public static int myWidth = 200;
public static int myHeight = 200;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(myColor);
g.fillRect(myX, myY, myWidth, myHeight);
}
}
请注意,如果您不想每次更改颜色/大小时都调用Rect.repaint()
,则只需创建一个新方法即可更改每个值并包含repaint()
,例如:>
public void changeWidth(int width){
myWidth = width;
repaint();
}