第一类:
public class Bar extends JComponent {
// instance variables - replace the example below with your own
private static boolean litUp = false;
private static boolean vertical = true;
private static int positionX;
private static int positionY;
private static int sizeX;
private static int sizeY;
private static Color color;
public void paintComponent(Graphics g) {
System.out.println("I am being called");
positionX = 50;
positionY = 30;
vertical = true;
if(vertical == true) {
sizeX = 10;
sizeY = 30;
if(litUp == true)
{
color = Color.red;
}
else
{
color = Color.black;
}
} else{
sizeX = 30;
sizeY = 10;
if(litUp == true)
{
color = Color.red;
}
else
{
color = Color.black;
}
}
g.fillRect(positionX, positionY, sizeX, sizeY);
g.setColor(color);
super.paintComponent(g);
}
}
第二类:
public class TestingBar {
public static void main(String[] args) {
final int FRAME_WIDTH = 317;
final int FRAME_HEIGHT = 415;
//created frame and panel. Panel layout is taken away from the flow layout.
final JFrame myFrame = new JFrame();
myFrame.setTitle("Tester Window (v2)");
myFrame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
final JPanel myPanel = new JPanel();
myPanel.setLayout(null);
final Bar testBar = new Bar();
//myPanel.add(testBar);
myFrame.getContentPane().add(testBar);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myPanel.add(testBar);
}
}
这只是给了我一个空的JFrame
。并且 paintComponent
方法中从未使用 print 语句。我真的被难住了。
PS:他们不会让我在不输入更多内容的情况下发布此内容,所以我将谈论海龟。我不明白它们是如何在野外生存的。它们跑不了,壳也确实没那么硬,只能从正面攻击,一旦翻倒就会无限期瘫痪。你认为自然选择现在已经把它们从泳池中耗尽了。
也许他们因为太可爱而活了下来。我的意思是,只要观看 YouTube 上一只小海龟试图吃樱桃番茄的视频即可。它是可爱的缩影。
最佳答案
我认为你应该调用 super.paintComponent(g);在函数的开始处。它可能会绘制在您的矩形上。
关于java - 无法绘制这个该死的矩形! (java Swing ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26553436/