简体   繁体   中英

Paint Method is not Called

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.

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 JPanel s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM