简体   繁体   English

简单的Java图形程序不显示

[英]Simple Java Graphic Program Not Displaying

Basically i'm starting to learn graphics in java so I made a simple program to display two rectangles and a string on the screen. 基本上,我开始学习Java语言中的图形,所以我做了一个简单的程序来在屏幕上显示两个矩形和一个字符串。 The program compiles fine but does not display the two rectangles or the string. 该程序可以正常编译,但是不显示两个矩形或字符串。 Any input on my problem would be greatly appreciated. 对于我的问题的任何投入,将不胜感激。

//ClassOne.java
import javax.swing.*;

public class ClassOne {
    public static void main(String[] args)
    {
        JFrame f = new JFrame("Title");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ClassTwo object = new ClassTwo();

        f.add(object); //add object to frame
        f.setSize(400,250);
        f.setVisible(true);

    }
}


//ClassTwo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ClassTwo extends JPanel {

    public void paintComponet(Graphics g) //takes an object from a graphics class
    {
        super.paintComponent(g);
        this.setBackground(Color.BLACK);

        g.setColor(Color.WHITE);
        g.fillRect(25, 25, 100, 30); //x,y,width, height

        g.setColor(new Color(190,81,215));
        g.fillRect(25, 70, 100, 30);

        g.setColor(Color.RED);
        g.drawString("Text", 25, 120);
        System.out.print("hi");
    }

}

It's 它的

public void paintComponent(Graphics g)

not

public void paintComponet(Graphics g) {

Add the @Override annotation to allow the compiler to check for the method 添加@Override批注以允许编译器检查方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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