我需要帮助制作我想在屏幕上显示的内容。我能够在主要部分正确设置它,但我觉得将所有内容保存在自己的类中会更有条理。窗口将显示,但不会绘制任何内容。即使我设置的背景也没有显示出来。
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();
}
}
答案 0 :(得分:2)
答案 1 :(得分: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);
}