Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单打印一个圆形GUI问题_Java_Swing_User Interface - Fatal编程技术网

Java 简单打印一个圆形GUI问题

Java 简单打印一个圆形GUI问题,java,swing,user-interface,Java,Swing,User Interface,所以我是gui新手,我想做一个简单的程序来打印一个圆圈来代表太阳,然后在它附近我想打印另一个圆圈来代表行星。我的问题是,当我添加paintPlanet方法时,gui窗口中返回的所有内容现在都是空白屏幕。即使当我把paintPlanet注释掉时,太阳的圆圈也不会打印出来,我只剩下一个空白窗口。有人能帮我找出哪里出了问题,如何修复它,让两个圆圈都打印出来吗?我是GUI新手,所以请对我放松:) 主要内容: 你的paintSun和paintPlanet方法永远不会被神奇地调用。相反,JPanel需要重写

所以我是gui新手,我想做一个简单的程序来打印一个圆圈来代表太阳,然后在它附近我想打印另一个圆圈来代表行星。我的问题是,当我添加paintPlanet方法时,gui窗口中返回的所有内容现在都是空白屏幕。即使当我把paintPlanet注释掉时,太阳的圆圈也不会打印出来,我只剩下一个空白窗口。有人能帮我找出哪里出了问题,如何修复它,让两个圆圈都打印出来吗?我是GUI新手,所以请对我放松:)

主要内容:


你的paintSun和paintPlanet方法永远不会被神奇地调用。相反,JPanel需要重写paintComponent方法,因为所有绘图都在那里完成。您甚至可以从paintComponent中调用paintSun和paintPlanet方法,但我建议只调用一次
super.paintComponent(g)
,并且只能从paintComponent方法重写自身中调用

e、 g


你的paintSun和paintPlanet方法永远不会被神奇地调用。相反,JPanel需要重写paintComponent方法,因为所有绘图都在那里完成。您甚至可以从paintComponent中调用paintSun和paintPlanet方法,但我建议只调用一次
super.paintComponent(g)
,并且只能从paintComponent方法重写自身中调用

e、 g


哇,我觉得自己太傻了,没听清楚。非常感谢。哇,我觉得自己太傻了,没听清楚。非常感谢。
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;


public class PlanetsLogic extends JPanel
{   
private static final long serialVersionUID = 1L;

public void paintSun(Graphics g)
{
    super.paintComponent(g);

    //create circle and fill it as yellow to represent the sun
    g.setColor(Color.YELLOW);
    g.drawOval(100, 75, 75, 75);
    g.fillOval(100, 75, 75, 75);
} //end paintSun


public void paintPlanet(Graphics g)
{
    super.paintComponent(g);
    //create circle and fill it as yellow to represent the orbiting planet
    g.setColor(Color.BLUE);
    g.drawOval(75, 75, 75, 75);
    g.fillOval(75, 75, 75, 75);


}//end paintPlanet

}//end class PlanetsLogic
import javax.swing.JFrame;

public class OrbitingPlants_main 
{

public static void main(String[] args) 
{

    PlanetsLogic planet = new PlanetsLogic();
    JFrame frame = new JFrame();

    frame.setTitle("Orbiting Planets");
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(planet); //add panel onto frame
    frame.setVisible(true); 
}

}
// use @Override to ask the compiler to check if this method is a true override
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g); // HERE!
    paintSun(g);
    paintPlanet(g);
}

public void paintSun(Graphics g) {
    // super.paintComponent(g); // nope, not here!

    //create circle and fill it as yellow to represent the sun
    g.setColor(Color.YELLOW);
    g.drawOval(100, 75, 75, 75);
    g.fillOval(100, 75, 75, 75);
} //end paintSun


public void paintPlanet(Graphics g) {
    // super.paintComponent(g);  // NO don't call this here
    //create circle and fill it as yellow to represent the orbiting planet
    g.setColor(Color.BLUE);
    g.drawOval(75, 75, 75, 75);
    g.fillOval(75, 75, 75, 75);
}//end paintPlanet