【问题标题】:paintComponent(); is not working/ being called油漆组件();不工作/被调用
【发布时间】:2015-08-17 15:39:58
【问题描述】:

这是我的代码。我对编码有点陌生,所以请多多解释。

public class main extends JPanel{


    public static Window f = new Window();
        public static int Width = 600;
        public static int Hight = 400;
        public static void main(String args[]){ 

            f.setSize(Width, Hight);
            f.setVisible(true);
            f.setResizable(false);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setTitle("FightForYourLife");
            f.setLocationRelativeTo(null);
        }

        public void main(){
            JPanel panel = this;
        }

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillRect(100, 100, 50, 30);
            System.out.println("IT IS PAINTING");
        }



}

【问题讨论】:

    标签: java swing paintcomponent


    【解决方案1】:

    一堆问题,但第一个:

    • 您永远不会将主对象添加到您的顶级窗口、GUI 中,因此由于它从未添加到 GUI 中,因此它永远不会显示或呈现,因此永远不会调用 paintComponent。在您的公共静态 void main 方法中创建一个新的 Main 对象并将其添加到您的 Window(为什么是 Window 而不是 JFrame?)。

    其他问题:

    • 您的类作为“伪”构造函数,public void main(){。请记住,构造函数没有返回类型,没有 void,没有任何东西。所以应该是public main() {
    • 您的伪构造函数没有任何用处。 JPanel panel = this;???
    • 您需要遵循 Java 命名规则:类名以大写字母开头,字段和方法以小写字母开头。所以public class main { 应该是public class Main {
    • 再一次,为什么选择 Window 而不是 JFrame?

    例如:

    import java.awt.*;
    import javax.swing.*;
    
    // rename main to Main
    public class Main extends JPanel{
    
            public static int WIDTH = 600;
            public static int HEIGHT = 400;
    
            public static void main(String args[]){ 
                Main main = new Main(); // *** create your main object ***
    
                JFrame f = new JFrame("FightForYourLife");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(main);  // *** add your main object to the top-level window
                f.pack();  
                f.setResizable(false);
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
    
            public Main(){
                // JPanel panel = this; // this is not helpful, delete it
            }
    
            @Override // let the JPanel size itself.
            public Dimension getPreferredSize() {
                return new Dimension(WIDTH, HEIGHT);
            }
    
            @Override  // paintComponent should be protected, not public
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.setColor(Color.RED);
                g.fillRect(100, 100, 50, 30);
                System.out.println("IT IS PAINTING");
            }
    }
    

    【讨论】:

    • 你好,我只想说声谢谢,我被困了几天,JPanel 面板 = 这是我在网上寻找答案,有人说要补充。
    • @ExstreamTurtle:给你这个建议的人,把他们的名字写下来,这样你以后就可以忽略他们告诉你的一切。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 2013-03-10
    相关资源
    最近更新 更多