2016-04-18 3 views
0

Ich habe versucht, etwas mit dem JMenu einzurichten, wo Sie zwischen "Seiten" (JPanels) wechseln können, aber ich stieß auf ein Problem, wo man nicht verschwinden würde, nachdem ich einige Methoden online versucht hatte. Kann mir jemand sagen, was ist der Fehler, den ich mache?Warum wird die Java-Komponente JPanel nicht verschwinden?

Code:

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

das ist eine Menge Code für jemanden durch zu gehen. Wenn Sie nur die relevanten Abschnitte veröffentlichen, erhalten Sie möglicherweise Antworten – e4c5

+1

[Wie CardLayout verwenden] (http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)? – MadProgrammer

+1

Fragen zur Fehlersuche ("Warum funktioniert dieser Code nicht?") Müssen das gewünschte Verhalten, ein bestimmtes Problem oder einen Fehler und den kürzesten Code enthalten, der für die Reproduktion in der Frage erforderlich ist. Fragen ohne eine klare Problemstellung sind für andere Leser nicht nützlich. Siehe: Erstellen eines minimalen, vollständigen und überprüfbaren Beispiels. –

Antwort

1
frame.add(MainPage()); 

Diese Linie baut eine brandneue "Hauptseite" JPanel, und es fügt die die JFrame.

frame.remove(MainPage()); 

Diese Codezeile erstellt eine brandneue „Hauptseite“ JPanel, die die JFrame hinzugefügt nie wurde, und versucht, es zu entfernen.

Dieses neue Panel kann nicht entfernt werden, da es nie hinzugefügt wurde. Sie müssen einen Verweis auf das ursprüngliche Bedienfeld beibehalten und dieses entfernen.

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

//... 

frame.remove(main_page); 

Hinweis: Diese main_page erneut hinzugefügt, ohne dass zu einem späteren Zeitpunkt könnte es neu zu erstellen. Rufen Sie einfach erneut frame.add(main_page); an. Aber wirklich, Sie wollen die card layout manager verwenden.

+0

Vielen Dank, ich bin ein Neuling in Java, also wusste ich das nie – furProgrammer

Verwandte Themen