[英]I'm Need Help Adding My Paint Component to the Frame in a Class
I need help making what I am trying to paint visible on the screen. 我需要帮助以使我尝试绘制的内容在屏幕上可见。 I was able to set it up properly in the main, however I feel like it would be more organized to keep everything in their own classes.
我基本上可以正确设置它,但是我觉得将所有内容都保留在自己的班级中会更有条理。 The window will show up, but nothing will be painted.
窗口将会显示,但是不会画任何东西。 Even the background I set does not show up.
即使设置的背景也不会显示。
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);
}
}
This is what the main looks like: 这是主要的样子:
public class GameProject {
public static void main(String [] args){
JPanel CC = new CharacterCreator();
}
}
You need to add CharacterCreator
to your JFrame
: 您需要将
CharacterCreator
添加到您的JFrame
:
application.add(this);
Aside: Consider using Initial Threads 撇开:考虑使用初始线程
Change the name of the Window Creator and implement this: 更改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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.