为什么赢得Java组件JPanel不会失败?

时间:2016-04-18 02:49:39

标签: java jframe jpanel

我试图用JMenu设置一些东西,你可以在这里切换"页面" (JPanels)但是我遇到了一个问题,即在网上尝试一些方法之后不会消失。我犯的错误是什么?

代码:

public class Window {

public static boolean NewTerrainCamPos = false;

public static String textVal;
public static String textVal2;
public static String resiveTex;
public static String resiveTex2;

public static final int Width = 1000;
public static final int Height = 720;
public static final int FPS_CAP = 120;

private static long lastFrameTime;
private static float delta;

public void createDisplay(){

    ContextAttribs attribs = new ContextAttribs(3,2).withForwardCompatible(true).withProfileCore(true);

    try {
         Canvas openglSurface = new Canvas();
            JFrame frame = new JFrame();

            JPanel game = new JPanel();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(null);

            //..............Menu Bar...............
            JMenuBar menuBar = new JMenuBar();
            JMenu terrain  = new JMenu("Terrain");
            JMenu Home  = new JMenu("Home");
            menuBar.add(Home);
            menuBar.add(terrain);
            JMenuItem newTerrain = new JMenuItem("add Terrain");
            JMenuItem editTerrain = new JMenuItem("Edit Terrain");
            JMenuItem editTexture = new JMenuItem("Edit Texture");

            terrain.add(newTerrain);
            terrain.add(editTerrain);
            terrain.add(editTexture);
            frame.setJMenuBar(menuBar);
            //......................................................

            newTerrain.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    NewTerrainCamPos = true;
                    JFrame frame2 = new JFrame();
                    frame2.setVisible(true);
                    frame2.setSize(300, 300);
                    //...............................
                    GridLayout experimentLayout = new GridLayout(3,2);
                    frame2.setLayout(experimentLayout);
                    //.....................................
                    JLabel xCord = new JLabel("XCoords: ");
                    JLabel zCord = new JLabel("ZCoords: ");
                    JTextField text = new JTextField();
                    JTextField text2 = new JTextField();

                    resiveTex2 = text2.getText();

                    text.getDocument().addDocumentListener(new DocumentListener() {
                        @Override
                        public void insertUpdate(DocumentEvent de) {
                            resiveTex = text.getText();
                        }

                        @Override
                        public void removeUpdate(DocumentEvent de) {
                            resiveTex = text.getText();
                        }

                        @Override
                        public void changedUpdate(DocumentEvent de) {
                            //plain text components don't fire these events
                        }
                    });



                    text2.getDocument().addDocumentListener(new DocumentListener() {
                        @Override
                        public void insertUpdate(DocumentEvent de) {
                            resiveTex2 = text2.getText();
                        }

                        @Override
                        public void removeUpdate(DocumentEvent de) {
                            resiveTex2 = text2.getText();
                        }

                        @Override
                        public void changedUpdate(DocumentEvent de) {
                            //plain text components don't fire these events
                        }
                    });

                    JButton createTerrain = new JButton("CreateTerrain");

                    createTerrain.addActionListener(new ActionListener(){
                        TIDF terrainFileID;
                        public void actionPerformed(ActionEvent a){
                            NewTerrainCamPos = false;
                            textVal = text.getText();
                            textVal2 = text2.getText();
                            TIDF load = new TIDF();
                            load.terrainIDFile();
                        }
                    });

                    frame2.add(xCord);
                    frame2.add(text);
                    frame2.add(zCord);
                    frame2.add(text2);
                    frame2.add(createTerrain);
                }
            });

            editTerrain.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    JFrame frame3 = new JFrame();
                    frame3.setVisible(true);
                    frame3.setSize(300, 300);
                    //......................................
                    GridLayout experimentLayout = new GridLayout(3,2);
                    frame3.setLayout(experimentLayout);
                    //......................................
                    JButton select = new JButton("Select");
                    String  terrainLocList[] =
                        {
                            "Item 1",
                            "Item 2",
                            "Item 3",
                            "Item 4"
                        };

                    JList list = new JList(terrainLocList);
                    list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
                    list.setVisibleRowCount(-1);
                    frame3.add(list);
                    frame3.add(select);
                }
            });

            editTexture.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent arg0) {

                    frame.remove(MainPage());

                    frame.add(TextureEditor());

                    frame.revalidate();
                }
            });

            //.........................................

            frame.add(MainPage());
            frame.add(game);
            frame.setSize(1200, 1200);
            frame.setVisible(true);
            game.add(openglSurface);
            game.setBounds(0, 266, 1000, 720);
            openglSurface.setSize(1000, 720);
            Display.setParent(openglSurface);

        Display.setDisplayMode(new DisplayMode(Width, Height));
        Display.create(new PixelFormat(), attribs);
        frame.setTitle("Game editor 0.3");
    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    GL11.glViewport(0, 0, Width, Height);
    lastFrameTime = getCurrentTime();
}

public Component MainPage(){
    JPanel Main = new JPanel();
    Main.setBounds(400, 0, 500, 200);
    Label Welcome = new Label("Welcome to: Game Editor Version 0.3");
    Welcome.setFont(new Font("Monotype Corsiva",10,22));
    Main.add(Welcome);
    return Main;    
}

public Component TextureEditor(){
    JPanel TextureLayoutLook = new JPanel();
    TextureLayoutLook.setBounds(60, 0, 200, 200);
    Label editorVersion = new Label("Terrain Texture Editor: 0.1");
    TextureLayoutLook.add(editorVersion);
    return TextureLayoutLook;
}

public static boolean Returnboolean(){
    return NewTerrainCamPos;
}

public static String getTex1() {
    return textVal;
}

public static String getTex2(){
    return textVal2;
}

public static String getTexupdate(){
    return resiveTex;
}

public static String getTexupdate2(){
    return resiveTex2;
}

public static void updateDisplay(){
    Display.sync(FPS_CAP);
    Display.update();
    long currentFrameTime = getCurrentTime();
    delta = (currentFrameTime - lastFrameTime)/1000f;
    lastFrameTime = currentFrameTime;
}

public static float getFrameTimeSeconds(){
    return delta;
}

public static void closeDisplay(){
    Display.destroy();
}

private static long getCurrentTime(){
    return Sys.getTime()*1000/Sys.getTimerResolution();
}

}

1 个答案:

答案 0 :(得分:1)

frame.add(MainPage());

这一行构建了一个全新的“主页”JPanel,并将其添加到JFrame

frame.remove(MainPage());

这行代码会创建一个全新的“主页”JPanel,该主页从未添加到JFrame,并尝试将其删除。

无法删除此新面板,因为它从未添加过。您需要保留对原始面板的引用,然后将其删除。

JPanel main_page = MainPage();
frame.add(main_page);

//...

frame.remove(main_page);

注意:此main_page可以在将来重新添加,而无需重新创建它。只需再次致电frame.add(main_page);即可。但实际上,您想使用card layout manager