title: Creating menu UI with Java
body: <p>Im attempting to make a standard menu UI in java and I'm having an issue. 
  <br>
  <br> So essentially <code>Code 1</code> sets up the Jframe and calls <code>code 2</code>.
  <br> The problem I'm having is that when the menu button is pressed I want it to load <code>code 3</code> and then stay there until the exit button is pressed. But what is happening at the moment is that the mouse pressed is detected, it runs through the entirety of <code>code 3</code> and returns to <code>code 2</code> without repainting the menu bar or exit button that <code>code 3</code> contains.
  <br><br>So what I'm looking to happen is that when <code>code 3</code> is called I want it to stay there and display the content (that being the menu) until a mouse click is detected in the area on the <code>exit_but</code>.
  <br><br> Any help would be great.
  <br><br> My code is show below;</p>
  
  <p><br> Code 1:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.Graphics;
  
  import javax.swing.*;
  
  public class demo_project 
  {
  public static void main(String[] args) 
  {
      JFrame frame = new JFrame("Funhaus Project");
  
      frame.setSize(720, 1280);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      frame.getContentPane().add(new demo_main_screen());
      frame.pack();
      frame.setVisible(true);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Code 2: <br></p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  
  import javax.swing.*;
  
  public class demo_main_screen extends JPanel
  {  
  private ImageIcon menu_button;
  
  private MouseListener listener = new MouseAdapter()
  {
      public void mouseClicked(MouseEvent e)
      {
          // Detects if the Video button was pressed
          if (e.getPoint().x &gt; 15 &amp;&amp; e.getPoint().x &lt; 183 &amp;&amp; e.getPoint().y &gt; 15 &amp;&amp; e.getPoint().y &lt; 85)
          {   
              System.out.println("Menu Button Pressed");
              new demo_menu();
          }
      }
  };
  
  // Paints the content to the screen
  public void paint(Graphics g)
  {
      super.paintComponent(g);
  
      menu_button.paintIcon(this, g, 15, 15);
  }
  
  // main screen constructor
  public demo_main_screen()
  {
      addMouseListener(listener);
  
      menu_button = new ImageIcon("res/menu_but.png");
  
      setBackground(Color.white);
      setPreferredSize(new Dimension(1280, 720));
      setFocusable(true);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Code 3: <br></p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  
  import javax.swing.*;
  
  public class demo_menu extends JPanel
  {
  
  private ImageIcon menu, exit_but;
  
  private MouseListener listener = new MouseAdapter()
  {
      public void mouseClicked(MouseEvent e)
      {
          if (e.getPoint().x &gt; 230 &amp;&amp; e.getPoint().x &lt; 255 &amp;&amp; e.getPoint().y &gt; 15 &amp;&amp; e.getPoint().y &lt; 48)
          {   
              return;
          }
      }   
  };
  
  // main screen constructor
  public demo_menu()
  {
      addMouseListener(listener);
  
      menu = new ImageIcon("res/menu.png");
      exit_but = new ImageIcon("res/exit_but.png");
  
      setBackground(Color.white);
      setPreferredSize(new Dimension(1280, 720));
      setFocusable(true);
  }
  
  // Paints the content to the screen
  public void paint(Graphics g)
  {
      super.paintComponent(g);
  
      menu.paintIcon(this, g, 0, 0);
      exit_but.paintIcon(this, g, 230, 15);
  
      System.out.println("repaint");
  }   
  </code></pre>
  
  <p>}</p>