不调用绘画方法 [英] Paint Method is not Called

查看:103
本文介绍了不调用绘画方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上一帧中创建实例后,我尝试在下一帧中使用背景图像,但结果是,我只看到调试的结果,发现未调用paint方法.据我所知,paint方法是由JFrame类继承的,按照这种逻辑,我已将其重写.如我所料,发生逻辑错误的原因是由于我使用了事件处理程序并在EventHandlerClass中创建了实例.

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");
                    }               
                    }           
                }

这是一个代码,如果执行了一个动作,它将创建一个实例(gs).完成此操作后,我注意到该实例已被用作制作新的控制台框架.

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");
    }

现在,通过制作一个框架和一些按钮,我希望显示指示结果的所有数字(1、2、3),但数字1才显示.

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. 您正在扩展 JFrame ,但没有添加任何其他功能,请参见:

  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.

您正在打破绘画链:完成上述要点后,在 paintComponent()中,调用 super.paintComponent(...)

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.

这篇关于不调用绘画方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆