1

https://i.stack.imgur.com/nyLMG.jpg This is what I am trying to create essentially to start my menu based game. I am wondering if my approach is good. I have created a frame class "SonomaRoller" shown below. In that class it adds "frame" my "panel1" as shown in the diagram. As of now I also have my images drawn in that class that appear as "panel2". I want the user to be able to switch between panels with the buttons in panel1. Some of the panels where panel 2 is will have buttons of their own as well. What is the best approach for this? Should i make seperate classes for the panels and add them in JFrame? Should I switch between panels using JFrame since it adds the first panel? I have included my code below for my two classes. I also have a Jtext pane below where the panels are. Thanks in advance

___________________MY FRAME___________________

package sonomaroller;

import javax.swing.*;
import java.awt.*;
import static javax.swing.JFrame.*;

public class SonomaRoller extends JFrame {

    public static Dimension size = new Dimension(550,550); //Dimension of Frame
    public static String title = "Sonoma Roller v0.00" ;
    //Creates new object f to create the window

    //boolean
    public boolean addShop=false;

    public SonomaRoller(){

      setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null); // null centers window on screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        init(addShop);
    }
    public void init(boolean addShop){
       frame panel=new frame();

       panel.setLayout(null);
       add(panel);
       setVisible(true);

    }
   public static void main(String[] args) {

       SonomaRoller object1=new SonomaRoller();  

    }
}

___________________MY PANEL w/buttons___________________

package sonomaroller;
import javax.swing.*;
import java.awt.*; 
import java.awt.event.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;

import javax.swing.text.StyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

public class frame extends JPanel implements Runnable {


    public void run(){

    }

    public frame(){

        loadpics();
        attackButton();
        magicButton();
        travelButton();
        shopButton();
        textField(); 

    }
    public void paintComponent(Graphics g){


    }

   }
    public void textField(){


}
    public void attackButton(){

    }
    public void magicButton(){

    }
    public void travelButton(){

    }
    public void shopButton(){

    }

     public void loadpics(){
        Sonoma = new ImageIcon("C:\\Users\\Camtronius\\Documents\\NetBeansProjects\\SonomaRoller\\src\\sonomaroller\\testImage.jpg").getImage();
        System.out.println("image loaded");  
        loaded = true;
        repaint();
    }

}
Cameron Roberson
  • 111
  • 4
  • 20
  • 1
    Just __STOP__ now before proceeding, restrain yourself from the use of [Absolute Positioning](http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html), just read the first paragraph, in that, related to the pitfalls of using such an approach. Consider [CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for such a task. One related [example](http://stackoverflow.com/a/9349137/1057230) :-) – nIcE cOw Aug 16 '13 at 03:31
  • Moreover the way you handling images in your project is questionable. What if you give this project of yours to a friend of yours to test, then the person has to first provide images in the exact same location as written in your `loadpics()` method, what if the person doesn't have `Drive C` ? Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), check the `NetBeans` title, for information related to this thingy :-) – nIcE cOw Aug 17 '13 at 16:55

1 Answers1

1

Can you not use ActionListener to set the panel visible or invisible by pressing a button. For example:

panel_1Button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            panel1.setVisible(false)
            panel2.setVisible(true);
        }
    });
wilty
  • 175
  • 1
  • 1
  • 10