0

I am trying to write code for an application I'm making, I have my main frame with 6 buttons on them. When I push one of my buttons, it is meant to bring up a frame that has a tabbed layout set up.

I have the tabbed frame coded correctly and when I set each frame as visible they will appear to screen but they don't appear when the button is pushed.

I have action listeners connected to the buttons and the frames I want to connect with them as well as constructors but for some reason I can't get my code to work correctly.

I have added my driver and my first two forms I'm trying to connect to each other.

public class SimpsonsDriver {

    //@param args the command line arguments
    
        public static void main(String[] args) {
            // TODO code application logic here
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame simpsonFrame = new JFrame("The Simpson");
            JFrame homerFrame = new JFrame ("Homer Simpson");
            JFrame margeFrame = new JFrame ("Marge Simpson");
            JFrame bartFrame = new JFrame ("Bart Simpson");
            JFrame lisaFrame = new JFrame("Lisa Simpson");
            JFrame maggieFrame = new JFrame("Maggie Simpson");
            SimpsonForm simpsonForm = new SimpsonForm(simpsonFrame, homerFrame, margeFrame, bartFrame, 
                    lisaFrame, maggieFrame);
            HomerForm homerForm = new HomerForm(homerFrame, simpsonFrame);
            MargeForm margeForm = new MargeForm(margeFrame, simpsonFrame);
            BartForm bartForm = new BartForm(bartFrame, simpsonFrame);
            LisaForm lisaForm = new LisaForm(lisaFrame, simpsonFrame);
            MaggieForm maggieForm = new MaggieForm(maggieFrame, simpsonFrame);
            
            
            
    }

}

public class SimpsonForm implements ActionListener{
    
    JFrame simpsonFrame, homerFrame, margeFrame, bartFrame, lisaFrame, maggieFrame;//instance variables
    JButton homerBtn, margeBtn, bartBtn, lisaBtn, maggieBtn, closeBtn; //instance variables

    public SimpsonForm(JFrame simpsonFrame, JFrame homerFrame, JFrame margeFrame, JFrame bartFrame, JFrame lisaFrame, JFrame maggieFrame) {
        
        this.simpsonFrame = simpsonFrame;
        this.homerFrame = homerFrame;
        this.margeFrame = margeFrame;
        this.bartFrame = bartFrame;
        this.lisaFrame = lisaFrame;
        this.maggieFrame = maggieFrame;
        simpsonFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    
        createPanel();
    }//end constructor method SimpsonForm

  
    private void createPanel(){
        
        homerBtn = new JButton ("Homer");
        margeBtn = new JButton ("Marge");
        bartBtn = new JButton ("Bart");
        lisaBtn = new JButton ("Lisa");
        maggieBtn = new JButton ("Maggie");
        closeBtn = new JButton ("Close");
        
        
        FlowLayout flow = new FlowLayout();
        JPanel p1 = new JPanel();
        p1.setLayout(flow);
        
        
        JPanel p2 = new JPanel();
        p2.setLayout(flow);
        
        ImageIcon img = new ImageIcon(this.getClass().getResource("simpsons.png"));
        
        JLabel imgLabel = new JLabel();
        
        imgLabel.setIcon(img);
        
        imgLabel.setBounds(10, 10, img.getIconWidth(), img.getIconHeight());
        p1.add(imgLabel);
        
        p2.add(homerBtn);
        homerBtn.addActionListener(this);
        
        p2.add(margeBtn);
        margeBtn.addActionListener(this);
       
        p2.add(bartBtn);
        bartBtn.addActionListener(this);
       
        p2.add(lisaBtn);
        lisaBtn.addActionListener(this);
        
        p2.add(maggieBtn);
        maggieBtn.addActionListener(this);
       
        p2.add(closeBtn);
        closeBtn.addActionListener(this);
        
        
        simpsonFrame.setLayout(new BorderLayout());
        simpsonFrame.setResizable(false);
        simpsonFrame.add(p1, BorderLayout.BEFORE_FIRST_LINE);
        simpsonFrame.add(p2, BorderLayout.CENTER);
        simpsonFrame.setSize(425, 425);
        simpsonFrame.setLocationRelativeTo(null);
        simpsonFrame.setVisible(true);
        
    }//end method createPanel

    @Override
    public void actionPerformed(ActionEvent e) {
         
        if(e.getSource() == homerBtn) {
            homerFrame.setVisible(true);
            simpsonFrame.setVisible(false);
        }
        else if(e.getSource() == margeBtn) {
            margeFrame.setVisible(true);
            simpsonFrame.setVisible(false);
        }
        else if(e.getSource() == bartBtn) {
            bartFrame.setVisible(true);
            simpsonFrame.setVisible(false);
        }
        else if(e.getSource() == lisaBtn) {
            lisaFrame.setVisible(true);
            simpsonFrame.setVisible(false);
        }
        else if(e.getSource() == maggieBtn) {
            maggieFrame.setVisible(true);
            simpsonFrame.setVisible(false);
        }
        else if(e.getSource() == closeBtn) {
                System.exit(0);
        }//end if
        
    }//end event handler
   
   } 

public class HomerForm implements ActionListener{
    
    JFrame simpsonFrame, homerFrame;
    JButton closeBtn;

    public HomerForm(JFrame simpsonFrame, JFrame homerFrame ) {
        this.simpsonFrame = simpsonFrame;
        this.homerFrame = homerFrame;
        createPanel();
    }

    
    private void createPanel() {
       
        homerFrame = new JFrame("Homer Simpson");
        closeBtn = new JButton("Close");
        closeBtn.setSize(200, 50);
        
        ImageIcon ogImage = new ImageIcon(this.getClass().getResource("/homer1.png"));
        JLabel ogLabel = new JLabel();
        ogLabel.setIcon(ogImage);  
        JPanel p1 = new JPanel();
        p1.add(ogLabel);
        ImageIcon hwImage = new ImageIcon(this.getClass().getResource("/homer2.png"));
        JLabel hwLabel = new JLabel();
        hwLabel.setIcon(hwImage);
        JPanel p2 = new JPanel();
        p2.add(hwLabel);
        ImageIcon cImage = new ImageIcon(this.getClass().getResource("/homer3.png"));
        JLabel cLabel = new JLabel();
        cLabel.setIcon(cImage);     
        JPanel p3 = new JPanel();
        p3.add(cLabel);
        JPanel p4 = new JPanel();  
        JTabbedPane tab = new JTabbedPane();
        tab.setBounds(100, 100, 300, 300);
        tab.add("Original", p1);
        tab.add("HalfWay", p2);
        tab.add("Current", p3);
        tab.add("Close", p4);
        p4.add(closeBtn);
        closeBtn.setLayout(new BorderLayout());
        closeBtn.addActionListener(this);
        homerFrame.add(tab);
        homerFrame.setResizable(false);
        homerFrame.setSize(500, 500);
        homerFrame.setLocationRelativeTo(null);
        homerFrame.setVisible(false);
       
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
          if(e.getSource() == closeBtn) {
                homerFrame.setVisible(false);
                simpsonFrame.setVisible(true);
               
        }//end if
        else
          {
              System.exit(0);
          }
    }
}

When i run it, i get this image. simpsonsFrame this is what happens when i click the homer button i then have to max it to see a blank screen.homerFrame. when i set the homerFrame to visible, it displays what i what to appear if the button was pushed.it shows homerFrame then it shows the simpsonFrame.homersetVisible true

  • 4
    1) Don't use multiple `JFrames`, it creates a terrible UX, the user will have several windows in their task bar making your app confusing, see [The use of multiple JFrames, Good / Bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (bad). 2) `setBounds` will be ignored when using Layout Managers, so you can safely remove those lines – Frakcool Apr 28 '21 at 14:32
  • 4
    Looks to me like you are trying to display a single form when a button is clicked and only one form will be visible at a time. For something like this I would suggest that on your main frame you have all the buttons. Then when you click a button, you create and display a child JDialog. This way the buttons are only defined in a single place, not in all of your child clases. – camickr Apr 28 '21 at 14:36
  • 2
    For better help sooner post a proper [mre] that demonstrates your issue. You can demonstrate the problem with a single button rather than 6, it should be in a single file, but a single public class so we can copy-paste, compile and run your code in one go – Frakcool Apr 28 '21 at 14:36
  • Thank you for your responses i am new to the website and still not to sure on how to post.correctly.. Am i ok to upload my full src for others to look over? – Alanna McKelvie Apr 28 '21 at 14:43
  • 4
    Instead of using multiple frames and swapping back and forth, a better option is to use a `CardLayout`. This will allow you do display different panels in the same frame when you click a button. Read the section from the Swing tutorial on [How to Use Card Layout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for a working example. This way you don't have dialogs hiding/showing every time you click a button. Or you could use a `JTabbedPane` to display each of the panels. – camickr Apr 28 '21 at 14:59
  • 2
    Instead of the full code, it's better to create a brand new project where you isolate the issue with the minimum code you can :) – Frakcool Apr 28 '21 at 15:52

0 Answers0