简体   繁体   中英

Java GUI does not appear

This is for my university project and I am busting my brain around why my Java GUI does not work. This is the situation: the code compiles and executes without an issue.

This code should create 300 X 300 frame center the desktop and create circle and it print my name underneath.

I got it working until the frame, but no circle

package gui;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;

public class GUI  extends JFrame{



     public void Paint (Graphics g){
      super.paintComponents(g);
      g.setColor(Color.yellow);
      g.fillOval(50, 50, 200, 200);
      g.setColor(Color.BLACK);
       g.drawArc(75, 60, 150, 150, -25, -125);
  g.fillOval(100, 100, 25, 25);
  g.fillOval(175, 100, 25, 25);
  g.setColor(Color.BLUE);   
  g.setFont(new Font("Serif", Font.BOLD,18));
  g.drawString("My Nanme is BOB", 33, 275);


}



/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    GUI GUI = new GUI() ;
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUI.setSize(300,300);
    GUI.setTitle("BOB's GUI App");
    GUI.setVisible(true);
    GUI.setLocationRelativeTo(null);

在此处输入图片说明

I really appreciate your output. also please give me a hint why it does not work

Java is case-sensitive :

public void Paint (Graphics g)

Will never override

public void paint (Graphics g)

Your paint() method should have a lowercase 'P'. As you currently have it, you're not overriding the existing JFrame method.

To avoid such issues in the future, I would investigate the @Overrides annotation . If you use this on the above, it will tell you you're not overriding anything!

In your main, you have written

GUI GUI = new GUI() ;

You should make the second GUI something else. For example,

GUI gui = new GUI();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM