0

我需要帮助使我尝试绘制的内容在屏幕上可见。我基本上能够正确设置它,但是我觉得将所有内容都放在自己的班级中会更有条理。窗口会出现,但不会画任何东西。甚至我设置的背景也没有出现。

public class CharacterCreator extends JPanel {

//Declare Variables

ImageIcon icon = new ImageIcon();

//PAINT
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);


    //Drawing Code
    g.setColor(Color.red);
    g.drawOval(10, 10, 10, 10);
}

//Window Creator
public CharacterCreator() {
    super();
    JFrame application = new JFrame();
    application.setTitle("Window");
    application.setBackground(Color.WHITE);
    application.setIconImage(null);
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.setSize(500, 400);
    application.setLocationRelativeTo(null);
    application.setVisible(true);


}
}

这是主要的样子:

public class GameProject {
    public static void main(String [] args){
        JPanel CC = new CharacterCreator();
    }
}
4

2 回答 2

2

您需要添加CharacterCreator到您的JFrame

application.add(this);

旁白:考虑使用初始线程

于 2013-03-28T19:07:04.873 回答
1

更改 Window Creator 的名称并实现:

 public CharacterCreator() {
    super();
    JFrame application = new JFrame();
    application.setTitle("Window");
    application.setBackground(Color.WHITE);
    application.setIconImage(null);
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    application.setSize(500, 400);
    application.setLocationRelativeTo(null);
    application.setVisible(true);

    CharacterCreator panel = CharacterCreator();
    application.add(panel);
    }
于 2013-03-28T19:14:25.353 回答