Paint Method is not Called

烂漫一生 提交于 2021-01-29 09:30:56

问题


After making an instance in a previous frame, I'm trying to the background image on the next frame but as a result, I just saw the debugged result and found out that the paint method was not called. From what I know, the paint method is inherited by the JFrame class and with this logic, I've made it overrided. As I guess, the reason happen the logical error is from what I used the event handler and made the instance in the EventHandlerClass.

    if(e.getActionCommand().equals(ButtonTo))       
        if(idString.equals("USER"))
                {                       
                    {
                        if("1234".equals(pwSt))     
                        {
                            System.out.println("Wellcome");
                            if(gs==null)
                            {
                                gs=new GameStart();
                            }
                        }
                    else
                    {
                         System.out.println("Confirm your password");
                    }               
                    }           
                }

This is a code that If an action is performed it will make an instance(gs). After doing this, I noticed that the instance has been used as to make a new console frame.

class GameStart extends JFrame {
    private Image screenImage;
    private Graphics screenGraphic;
    private Image introBackgroundImage;
    private ImageIcon img;

    GameStart()
    {
        JFrame jf=new JFrame("Game Set");
        jf.setBounds(300, 300, 400, 200);
        jf.setLayout(new BorderLayout());

        JButton bt1=new JButton("Start");
        JButton bt2=new JButton("Exit");    
        JPanel panel1=new JPanel();
        panel1.add(bt1);panel1.add(bt2);

        setContentPane(panel1);

        jf.add(panel1, BorderLayout.SOUTH);
        bt1.addActionListener(new Choice());
        bt2.addActionListener(new Choice());
        jf.setVisible(true);    
        img=new ImageIcon("./Images/backGroundImage.jpg");
        System.out.println("1");
    }

    public void paint(Graphics g) {
        screenImage=createImage(200, 200);  
        screenGraphic=screenImage.getGraphics();
        screenDraw(screenGraphic);
        g.drawImage(screenImage, 0, 0, null);
        System.out.println("2");
    }

    public void screenDraw(Graphics g) 
    {   
        this.repaint();
        System.out.println("3");
    }

Now, With making a frame and some buttons, I expect to show all the numbers(1, 2, 3) that indicate the result but Just did number 1.


回答1:


There are some errors in your code that I can see at first glance:

  1. You're extending JFrame, but you're not adding any extra functionality to it, see: Extends JFrame vs. creating it inside the program. Instead, build your GUI towards the use of JPanels and override their paintComponent(...) method and not the paint(...) one.

  2. You're breaking the paint-chain: After doing the above point, in paintComponent(), call super.paintComponent(...)

Maybe there are others but I'm currently busy and can't test your code, but the ones above should help with your issue.



来源:https://stackoverflow.com/questions/55618570/paint-method-is-not-called

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!