2016-04-18 21 views
0

「ページ」(JPanels)を切り替えることができるJMenuで何かをセットアップしようとしていましたが、オンラインでいくつかのメソッドを試しても消えることのない問題が発生しました。誰かが私が作っているエラーが何であるか教えてもらえますか?なぜJavaコンポーネントJPanelが消えないのですか?

コード:

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

これは、誰かが通過するためのコードです。関連セクションのみを投稿すると、回答が得られる可能性があります。 – e4c5

+1

[CardLayoutの使用方法](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)? – MadProgrammer

+1

デバッグのヘルプ(「なぜこのコードは動作しませんか?」)には、目的の動作、特定の問題またはエラー、および問題そのものを再現するために必要な最短コードが含まれている必要があります。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:最小、完全、および検証可能な例を作成する方法。 –

答えて

1
frame.add(MainPage()); 

この行は、ブランドの新しい "メインページ" JPanelを構築し、JFrameを追加します。

frame.remove(MainPage()); 

このコード行は、JFrameに追加されたことのない新しい「メインページ」JPanelを作成し、それを削除しようとします。

この新しいパネルは追加されていないため、削除できません。元のパネルへの参照を保持し、それを削除する必要があります。

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

//... 

frame.remove(main_page); 

注:このmain_pageはそれを再作成することなく、将来の時点で再度追加することができます。もう一度frame.add(main_page);に電話してください。しかし、実際には、card layout managerを使用します。

+0

本当にありがとう、私はJavaでnoobだから私はそれを知らなかった – furProgrammer

関連する問題