简体   繁体   English

使用内部框架时,主面板的重新粉刷无法正常工作

[英]repaint of main panel not working properly when using an internal frame

I'm having the following bug/problem and I havent been able to found solution yet on web or example of someone with similar issue.Basicaly I have a main frame that contains a panel of the same size(acting as the main panel) and When you press "Enter" an internal frame pop up(on top of where the playersprite is) acting as the inventory and then the control is passed to it and if you press "Enter" again the inventory is destroyed and control is passed back to the main panel. 我遇到以下错误/问题,但仍无法在网络上找到解决方案或存在类似问题的人的示例。基本而言,我有一个包含尺寸相同的面板(充当面板)的主框架,当您按“ Enter”键时,将弹出一个内部框架(在PlayersPrite所在的顶部)作为清单,然后将控件传递给它,如果您按“ Enter”键,则再次销毁清单并将控件传递回主面板。

the repaint function is called and the character and the map is then redrawn and this work about 90% of the time.The other 10% or less of time whenever the inventory is destroyed it seems the repaint is called(and work) except nothing is drawn its as if it draw on the destroyed panel because if I add a debug keypress that call repaint on the mainpanel(thescreen) everything is back to normal. 调用repaint函数,然后重新绘制角色和地图,这大约90%的时间有效。每当销毁库存时,其他10%或更短的时间似乎都调用了repaint(并且有效),但什么都没有就像在被破坏的面板上绘制一样绘制它,因为如果我添加了一个调试按键,该调用在主面板(屏幕)上调用了重新绘制,一切都会恢复正常。

of course I could just repaint the character every loop in the run() method but thats terrible since I will only repaint if something changed(ie I moved) 当然我可以在run()方法的每个循环中重新绘制字符,但是那太糟糕了,因为我只会在发生某些更改(例如,我移动了)时才重新绘制

I removed all the move and other code since they arent useful and still get the problem with the below code.You can think of the Character class as a plain drawn square.Anyone as any insight on why this is happening? 我删除了所有移动代码和其他代码,因为它们没有用,但仍然会出现下面的代码问题。您可以将Character类视为一个简单的正方形。任何人都可以理解为什么会发生这种情况?

public class main extends JFrame implements Runnable{
    private boolean gameRunning=true;
    private Character Link;
    private MainScreen theScreen;
    public final int ScreenHeight=500;
    public final int ScreenWidth=500;
    public boolean inMenu=false;
    Block ablock=new Block(200,200);
    public class Inventory extends JInternalFrame{
        public Inventory(){
            setBounds(25,25,300,300);
            setDefaultCloseOperation(HIDE_ON_CLOSE);
            setVisible(true);
            addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e){
                    int key=e.getKeyCode();
                    if(key==KeyEvent.VK_ENTER){
                    try{                                    
                        setClosed(true);
                        theScreen.requestFocusInWindow();
                        theScreen.repaint();
                        inMenu=false;
                    }                               
                    catch(Exception ex){}
                    }
            }});
        }
    }
    class MainScreen extends JPanel{
        MainScreen(){
            super();
            setIgnoreRepaint(true);
            setFocusable(true);
            setBounds(0,0,ScreenWidth,ScreenHeight);
            setVisible(true);
            setBackground(Color.white);
        }
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            Link.draw(g);
            g.drawImage(ablock.getImg(),ablock.getX(), ablock.getY(),null);
        }
    }
    main(){
        super();
        final JDesktopPane desk = new JDesktopPane();
        theScreen=new MainScreen();
        add(theScreen);
        theScreen.addKeyListener(new KeyAdapter() {
            public  void keyPressed(KeyEvent e){
                int key=e.getKeyCode();
                if(key==KeyEvent.VK_ENTER){
                    inMenu=true;
                    Inventory myInventory=new Inventory();
                    desk.add(myInventory);
                    myInventory.requestFocusInWindow();
                }
            }   
    });
        add(desk);
        try {
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());

            }
        catch (Exception e) {}
        setTitle("Project X");
        setResizable(false);
        Link=new Character();
        setSize(500,500);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        main Game=new main();
        new Thread(Game).start();
    }
    public void run(){
            //omitted/irrelevant only contains  a FPS count
          }
    }

}

Don't use KeyListeners. 不要使用KeyListeners。 Use Key Bindings . 使用键绑定

Don't use a Thread. 不要使用线程。 Use a Swing Timer for animation so updates will be done on the EDT. 使用Swing Timer进行动画处理,以便在EDT上进行更新。

Don't use an Internal Frame for a popup window. 不要将内部框架用于弹出窗口。 Use a JDialog. 使用一个JDialog。

Do custom painting on a JPanel, not a JDesktopPane. 在JPanel而不是JDesktopPane上进行自定义绘制。

Don't use setIgnoreRepaints(). 不要使用setIgnoreRepaints()。 That is used for active rendering. 这用于主动渲染。

Don't use empty catch clauses. 不要使用空的catch子句。

Use standard Java naming conventions. 使用标准的Java命名约定。 Classes start with upper cases characters, variable names do not. 类以大写字母开头,变量名则不然。

Don't use setBounds(). 不要使用setBounds()。 Use a Layout Manager. 使用布局管理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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