簡體   English   中英

使用Swing的paintComponent方法上未顯示形狀

[英]Shape is not showing on paintComponent method using Swing

運行我的程序時,不會顯示為fillOval的形狀。 我擴展了JPanel,但似乎出現了問題,因為我看不到fillOval。 這是我的代碼:

public class MovingBall extends JPanel{

public MovingBall(){

}
public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,700);
    panel.setBackground(Color.BLACK);
    frame.setVisible(true);
    MovingBall movingBall = new MovingBall();
    frame.add(movingBall);
}
@Override
public void paintComponent(Graphics canvas){
    super.paintComponent(canvas);

    canvas.setColor(Color.BLUE);
    canvas.fillOval(50, 90, 20, 20);

}

}

將這些行frame.setSize(500,700);之前frame.setSize(500,700); 呼叫

MovingBall movingBall = new MovingBall();
frame.add(movingBall);

或致電

frame.revaliedate();
frame.repaint(); 

添加后

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM