Java JInternalFrame未刷新并返回错误

Java JInternalFrame未刷新并返回错误,java,swing,jinternalframe,Java,Swing,Jinternalframe,亲爱的朋友们: 我正忙于一个小界面的编程,恳请您的帮助: 以下是我想做的事情: 我有一个带有几个内部框架的JDesktopPane。逻辑是: -DesktopPane从一个启动窗口(JInternalFrame)开始,该窗口具有级别1的按钮 -用户单击Level 1,一个新的内部窗口Level 1将打开(启动窗口关闭),其中有一个按钮 -用户单击按钮并将级别1设置为完成 -1级内部窗口关闭 -启动窗口重新打开,但由于级别1已设置为“完成”,现在启动窗口有一个访问级别2的新按钮 -用户单击该按钮并

亲爱的朋友们:

我正忙于一个小界面的编程,恳请您的帮助:

以下是我想做的事情: 我有一个带有几个内部框架的JDesktopPane。逻辑是: -DesktopPane从一个启动窗口(JInternalFrame)开始,该窗口具有级别1的按钮 -用户单击Level 1,一个新的内部窗口Level 1将打开(启动窗口关闭),其中有一个按钮 -用户单击按钮并将级别1设置为完成 -1级内部窗口关闭 -启动窗口重新打开,但由于级别1已设置为“完成”,现在启动窗口有一个访问级别2的新按钮 -用户单击该按钮并访问级别2,依此类推

以下是发生的情况 -我启动程序,我得到一级按钮启动窗口 -我点击按钮,返回启动窗口 但是 -2级没有按钮 -此外:如果我再次点击按钮级别1,我会得到一个错误

这里是我的代码:我希望有人能给我指出正确的方向 主类

package gioco;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.border.EmptyBorder;

public class AllInAWindow extends JFrame {
//create components outside constructor    
JDesktopPane MainDesktopPane = new JDesktopPane();
JInternalFrame IFrameLaunchWindow = new JInternalFrame(); 
JInternalFrame IFrameLevel1 = new JInternalFrame(); 
JInternalFrame IFrameLevel2 = new JInternalFrame();


//let's create the constructor    
public AllInAWindow() {
    initUI(); 
}
public void initUI(){

    VariablesSetEGet VariablesSetEGetObj = new VariablesSetEGet(); 
    //-------------------------IFrame Level1--------------------------//        
    //interface of IFrame Level1
    IFrameLevel1.setTitle("Level 1");
    IFrameLevel1.setSize(200, 200);
    IFrameLevel1.setLocation(200, 200);
    IFrameLevel1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
    JButton action1L1 = new JButton("action 1 Level1");
    action1L1.setBorder(new EmptyBorder(5 ,5, 5, 5));
    IFrameLevel1.add(action1L1);
    //button action
    roundLevelAction1 roundLevelAction1Obj = new roundLevelAction1(); //create object of the constructor                  
    action1L1.addActionListener(roundLevelAction1Obj);//add listener to JButton and pass it through the constructor

    //-------------------------IFrame Level2--------------------------//        
    //interface of IFrame Level2
    IFrameLevel2.setTitle("Level 2");
    IFrameLevel2.setSize(200, 200);
    IFrameLevel2.setLocation(300, 300);        
    IFrameLevel2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
    JButton action1L2 = new JButton("action 1 Level2");
    action1L2.setBorder(new EmptyBorder(5 ,5, 5, 5));
    IFrameLevel2.add(action1L2);
    //button action
    roundLevelAction2 roundLevelAction2Obj = new roundLevelAction2(); //create object of the constructor                  
    action1L2.addActionListener(roundLevelAction2Obj);//add listener to JButton and pass it through the constructor

    //-------------------------iFrame Launch window-----------------------------//
    IFrameLaunchWindow.setLayout(null);
    IFrameLaunchWindow.setTitle("INIZIO");
    IFrameLaunchWindow.setClosable(true);
    IFrameLaunchWindow.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
    IFrameLaunchWindow.setSize(400, 400);                
    //button level 1
    JButton ButtonLevel1 = new JButton("level1"); //create the button variable
    ButtonLevel1.setSize(200, 40);
    ButtonLevel1.setLocation(100, 120);
    ButtonLevel1.addActionListener//add the listener so that the button can launch the window with the board level
    (new ActionListener()
        {
            public void actionPerformed (ActionEvent lanciaLevel1) //create the action
            {                    
                MainDesktopPane.add(IFrameLevel1); //launch the frame with the level 1
                IFrameLevel1.setVisible(true); //set it to visible
                IFrameLaunchWindow.dispose();//close this launch window                     
            }
        }
    );
    IFrameLaunchWindow.add(ButtonLevel1); //add the button of the level 1
    ButtonLevel1.setVisible(true);
    //button level 2
    JButton ButtonLevel2 = new JButton("level2"); 
    ButtonLevel2.setSize(200, 40);
    ButtonLevel2.setLocation(100, 180);
    ButtonLevel2.addActionListener
    (new ActionListener()
        {
            public void actionPerformed (ActionEvent lanciaLevel2)
            {                                             
                MainDesktopPane.add(IFrameLevel2); //launch the frame with the level 2
                IFrameLevel1.setVisible(true); //set it to visible
                IFrameLaunchWindow.dispose();//close this launch window                     
            }
        }
    );
    //same as before but here the if: if level1 is not set to completed the button for level2 won't appear
    if (VariablesSetEGetObj.getL1()==1){
        IFrameLaunchWindow.add(ButtonLevel2);  
        ButtonLevel2.setVisible(true);
    }        
    //button level 3 (just for image, same logic)
    JButton ButtonLevel3 = new JButton("level3"); 
    ButtonLevel3.setSize(200, 40);
    ButtonLevel3.setLocation(100, 240);
    if (VariablesSetEGetObj.getL2()==1){
        IFrameLaunchWindow.add(ButtonLevel3);  
        ButtonLevel3.setVisible(true);
    }        

    MainDesktopPane.add(IFrameLaunchWindow); //start the main panel with the launch window
    IFrameLaunchWindow.setVisible(true);
    add(MainDesktopPane);//add the main desktopPane to the Frme                
}

class roundLevelAction1 implements ActionListener {//action of the button in IFrame 1
    public roundLevelAction1(){
            //there are graphical elements here in the original code but not relevant for this
    }         
        @Override
        public void actionPerformed(ActionEvent ActionLevel1) {//if button is clicked
            VariablesSetEGet VariablesSetEGetObj = new VariablesSetEGet(); 
            VariablesSetEGetObj.setL1(1);//set Level 1 to complete 
            IFrameLevel1.dispose(); //close L1 window
            //go back to launch window but now it should show button to access L2 as L1 has been set to completed
            MainDesktopPane.add(IFrameLaunchWindow); 
            IFrameLaunchWindow.setVisible(true);

        }
}

class roundLevelAction2 implements ActionListener {//action of button in IFrame2
    public roundLevelAction2(){
            //there are graphical elements here in the original code but not relevant for this
    }         
        @Override
        public void actionPerformed(ActionEvent ActionLevel2) {//if button is clicked
            VariablesSetEGet VariablesSetEGetObj = new VariablesSetEGet(); 
            VariablesSetEGetObj.setL2(1);//set level 2 to complete
            IFrameLevel2.dispose(); //close L2 window
            //go back to launch window but now it should show button to access L3 as L2 has been set to completed
            MainDesktopPane.add(IFrameLaunchWindow); 
            IFrameLaunchWindow.setVisible(true);                
        }
}


public static void main (String[]args) {
    //as this will launch the "fresh" new game, we set each level (and therefore button) 
    //to disabled except button 1. we do it here outside the runnable
    VariablesSetEGet VariablesSetEGetObj = new VariablesSetEGet(); 
    VariablesSetEGetObj.setL1(0);
    VariablesSetEGetObj.setL2(0);
    //TRIGGER LAUNCH WINDOW (see ZCode1)
    EventQueue.invokeLater
    (new Runnable() 
        { 
            @Override
            public void run() 
            {
                AllInAWindow AllInAWindowObj = new AllInAWindow(); 
                AllInAWindowObj.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                AllInAWindowObj.setVisible(true);
                AllInAWindowObj.setSize(800, 800);
            }
        }   
    );
}
}
public class VariablesSetEGet {
static int L1 = 0; 
public void setL1 (int L1){
    this.L1 = L1; 
}
public int getL1 (){
    return L1; 
}
static int L2 = 0; 
public void setL2 (int L2){
    this.L2 = L2; 
}
public int getL2 (){
    return L2; 
}    
}
单独上课

package gioco;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JInternalFrame; 
import javax.swing.border.EmptyBorder;

public class AllInAWindow extends JFrame {
//create components outside constructor    
JDesktopPane MainDesktopPane = new JDesktopPane();
JInternalFrame IFrameLaunchWindow = new JInternalFrame(); 
JInternalFrame IFrameLevel1 = new JInternalFrame(); 
JInternalFrame IFrameLevel2 = new JInternalFrame();


//let's create the constructor    
public AllInAWindow() {
    initUI(); 
}
public void initUI(){

    VariablesSetEGet VariablesSetEGetObj = new VariablesSetEGet(); 
    //-------------------------IFrame Level1--------------------------//        
    //interface of IFrame Level1
    IFrameLevel1.setTitle("Level 1");
    IFrameLevel1.setSize(200, 200);
    IFrameLevel1.setLocation(200, 200);
    IFrameLevel1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
    JButton action1L1 = new JButton("action 1 Level1");
    action1L1.setBorder(new EmptyBorder(5 ,5, 5, 5));
    IFrameLevel1.add(action1L1);
    //button action
    roundLevelAction1 roundLevelAction1Obj = new roundLevelAction1(); //create object of the constructor                  
    action1L1.addActionListener(roundLevelAction1Obj);//add listener to JButton and pass it through the constructor

    //-------------------------IFrame Level2--------------------------//        
    //interface of IFrame Level2
    IFrameLevel2.setTitle("Level 2");
    IFrameLevel2.setSize(200, 200);
    IFrameLevel2.setLocation(300, 300);        
    IFrameLevel2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
    JButton action1L2 = new JButton("action 1 Level2");
    action1L2.setBorder(new EmptyBorder(5 ,5, 5, 5));
    IFrameLevel2.add(action1L2);
    //button action
    roundLevelAction2 roundLevelAction2Obj = new roundLevelAction2(); //create object of the constructor                  
    action1L2.addActionListener(roundLevelAction2Obj);//add listener to JButton and pass it through the constructor

    //-------------------------iFrame Launch window-----------------------------//
    IFrameLaunchWindow.setLayout(null);
    IFrameLaunchWindow.setTitle("INIZIO");
    IFrameLaunchWindow.setClosable(true);
    IFrameLaunchWindow.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
    IFrameLaunchWindow.setSize(400, 400);                
    //button level 1
    JButton ButtonLevel1 = new JButton("level1"); //create the button variable
    ButtonLevel1.setSize(200, 40);
    ButtonLevel1.setLocation(100, 120);
    ButtonLevel1.addActionListener//add the listener so that the button can launch the window with the board level
    (new ActionListener()
        {
            public void actionPerformed (ActionEvent lanciaLevel1) //create the action
            {                    
                MainDesktopPane.add(IFrameLevel1); //launch the frame with the level 1
                IFrameLevel1.setVisible(true); //set it to visible
                IFrameLaunchWindow.dispose();//close this launch window                     
            }
        }
    );
    IFrameLaunchWindow.add(ButtonLevel1); //add the button of the level 1
    ButtonLevel1.setVisible(true);
    //button level 2
    JButton ButtonLevel2 = new JButton("level2"); 
    ButtonLevel2.setSize(200, 40);
    ButtonLevel2.setLocation(100, 180);
    ButtonLevel2.addActionListener
    (new ActionListener()
        {
            public void actionPerformed (ActionEvent lanciaLevel2)
            {                                             
                MainDesktopPane.add(IFrameLevel2); //launch the frame with the level 2
                IFrameLevel1.setVisible(true); //set it to visible
                IFrameLaunchWindow.dispose();//close this launch window                     
            }
        }
    );
    //same as before but here the if: if level1 is not set to completed the button for level2 won't appear
    if (VariablesSetEGetObj.getL1()==1){
        IFrameLaunchWindow.add(ButtonLevel2);  
        ButtonLevel2.setVisible(true);
    }        
    //button level 3 (just for image, same logic)
    JButton ButtonLevel3 = new JButton("level3"); 
    ButtonLevel3.setSize(200, 40);
    ButtonLevel3.setLocation(100, 240);
    if (VariablesSetEGetObj.getL2()==1){
        IFrameLaunchWindow.add(ButtonLevel3);  
        ButtonLevel3.setVisible(true);
    }        

    MainDesktopPane.add(IFrameLaunchWindow); //start the main panel with the launch window
    IFrameLaunchWindow.setVisible(true);
    add(MainDesktopPane);//add the main desktopPane to the Frme                
}

class roundLevelAction1 implements ActionListener {//action of the button in IFrame 1
    public roundLevelAction1(){
            //there are graphical elements here in the original code but not relevant for this
    }         
        @Override
        public void actionPerformed(ActionEvent ActionLevel1) {//if button is clicked
            VariablesSetEGet VariablesSetEGetObj = new VariablesSetEGet(); 
            VariablesSetEGetObj.setL1(1);//set Level 1 to complete 
            IFrameLevel1.dispose(); //close L1 window
            //go back to launch window but now it should show button to access L2 as L1 has been set to completed
            MainDesktopPane.add(IFrameLaunchWindow); 
            IFrameLaunchWindow.setVisible(true);

        }
}

class roundLevelAction2 implements ActionListener {//action of button in IFrame2
    public roundLevelAction2(){
            //there are graphical elements here in the original code but not relevant for this
    }         
        @Override
        public void actionPerformed(ActionEvent ActionLevel2) {//if button is clicked
            VariablesSetEGet VariablesSetEGetObj = new VariablesSetEGet(); 
            VariablesSetEGetObj.setL2(1);//set level 2 to complete
            IFrameLevel2.dispose(); //close L2 window
            //go back to launch window but now it should show button to access L3 as L2 has been set to completed
            MainDesktopPane.add(IFrameLaunchWindow); 
            IFrameLaunchWindow.setVisible(true);                
        }
}


public static void main (String[]args) {
    //as this will launch the "fresh" new game, we set each level (and therefore button) 
    //to disabled except button 1. we do it here outside the runnable
    VariablesSetEGet VariablesSetEGetObj = new VariablesSetEGet(); 
    VariablesSetEGetObj.setL1(0);
    VariablesSetEGetObj.setL2(0);
    //TRIGGER LAUNCH WINDOW (see ZCode1)
    EventQueue.invokeLater
    (new Runnable() 
        { 
            @Override
            public void run() 
            {
                AllInAWindow AllInAWindowObj = new AllInAWindow(); 
                AllInAWindowObj.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                AllInAWindowObj.setVisible(true);
                AllInAWindowObj.setSize(800, 800);
            }
        }   
    );
}
}
public class VariablesSetEGet {
static int L1 = 0; 
public void setL1 (int L1){
    this.L1 = L1; 
}
public int getL1 (){
    return L1; 
}
static int L2 = 0; 
public void setL2 (int L2){
    this.L2 = L2; 
}
public int getL2 (){
    return L2; 
}    
}

非常感谢您的帮助

这并不是最初问题的解决方案:这是根据MadProgrammer的建议进行的逻辑更改,即放弃JInternalFrame并使用CardLayout。我对CardLayout进行了研究,并设法做到了我最初想做的事情这里是代码

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CLayout {
    //1) create the Frame
    JFrame MainWindow = new JFrame("Finestra di Gioco"); 
    //2) create as many JPanels as messages + levels, but...
    JPanel panelCont = new JPanel(); //...the first one needs to contain them all
    JPanel FinestraLancio = new JPanel(); 
    JPanel Livello1 = new JPanel(); 
    JPanel Livello2 = new JPanel(); 
    //3) create the buttons to switch among panels
    JButton OpenLevel1 = new JButton("Open Level 1"); 
    JButton CompleteLevel1 = new JButton("Complete Level 1"); 
    JButton OpenLevel2 = new JButton("Open Level 2"); 
    JButton CompleteLevel2 = new JButton("Complete Level 2"); 

    //4) create an instance of the card Layout method
    CardLayout cl = new CardLayout(); 

    //5) create the constructor
    public CLayout(){
        //5.a) assign the instance of the card Layout to the container panel
        panelCont.setLayout(cl);

        //5.b) create the windows insert the buttons, assign the actions
        FinestraLancio.add(OpenLevel1); 
        FinestraLancio.add(OpenLevel2);
        OpenLevel2.setVisible(false);
        Livello1.add(CompleteLevel1); 
        Livello2.add(CompleteLevel2); 

        Livello1.setBackground(Color.yellow);
        Livello2.setBackground(Color.blue);

        OpenLevel1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent OpenLevel1){
                cl.show(panelCont, "2"); //this "2" is the position indicator, 
                                        //we'll discuss it at 5c
            }
        });

        OpenLevel2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent OpenLevel2){
                cl.show(panelCont, "3");
            }
        });

        CompleteLevel1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent CompleteLevel1){
                OpenLevel2.setVisible(true);
                cl.show(panelCont, "1");
            }
        });
        CompleteLevel2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent CompleteLevel2){
                OpenLevel2.setVisible(false);
                cl.show(panelCont, "1");
            }
        });        
        //5.c) now put the elements in the container panel, and add the name of 
        //the element and the position indicator you will need that position 
        //indicator when you want to call that specific window
        panelCont.add(FinestraLancio, "1"); //"1" is the position indicator
        panelCont.add(Livello1, "2"); 
        panelCont.add(Livello2, "3"); 

        //now indicate that the instance of the cardlayout has the container as 
        //its main panel and inside the container the first panel to be displayed 
        //is the one with the position indicator "1", i.e. the LaunchWindow
        cl.show(panelCont, "1");

        //create the main window
        MainWindow.add(panelCont); 
        MainWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        MainWindow.setSize(400, 400);
        MainWindow.setVisible(true);

    }

    public static void main (String[]args){
        SwingUtilities.invokeLater
        (new Runnable(){
            @Override
            public void run(){
                new CLayout(); 
            }
        }); 
    }

}

作为文章的一部分,我想指定我已经尝试在调用LaunchWindow repaint()和revalidate()的actionEvents中插入,但这并没有使用
CardLayout
而不是
JInternalFrame
s。使用某种控制器,它知道或可以确定当前状态,并控制用户可以移动到此-
IFrameLaunchWindow.setLayout(null)的位置-没有帮助您何时将“第二级”按钮添加到
IFrameLaunchWindow
?此外,您可能希望通读,这将使人们更容易阅读您的代码,您也更容易阅读其他人您确实意识到
variableSteget variableStegetobj=new variableSteget()
实际上并没有做任何事情,每次这样做时,都会创建
VariableSetGet
的本地副本,当该方法存在时,该副本会脱离上下文并丢失。。。