user96913
user96913

Reputation: 9

displaying class to another class

Hi im doing my main page for my program and i want to happen is when i click the maze button, the Maze class would appear and same with the hexagonal button.

Can you tell me what is wrong with my program?everytime i run and click a button nothing happens.thanks for the huge help.

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Center extends JFrame {

Maze x = new Maze();
HEX h = new HEX();


JPanel jp = new JPanel();
JButton jb = new JButton("Maze");
JButton jb1 = new JButton("Hex");

JFrame frame = new JFrame();


public Center()
{
    frame.setTitle("Prims Maze Generation");
    //setVisible(true);
    //setSize(400,200);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(400, 200);
    jp.add(jb);
    jp.add(jb1);
    frame.add(jp);

    jb.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {

            x.setVisible(true);
            frame.pack();
        }
    });
    jb.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent e)
        {
            h.setVisible(true);

        }
    });

}

public static void main(String args[])
{
    Center p = new Center();
}

 }

Upvotes: 0

Views: 64

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 209012

Before anything, your class is already a JFrame, either use the class JFrame or use the instance JFrame (and remove the JFrame extension - just do this), don't do both!

Your problem and simple fix (but may not yield your expected results).

  • JFrame has a default BorderLayout, which when you add components, will be automatically placed at the BorderLayout.CENTER position. Each position may only have one component. You are trying to add three components to the CENTER without even knowing it. The fix if to specify a different position for each component, i.e. BorderLayout.NORTH, BorderLayout.SOUTH

Here is the much cleaner fix

  • Use a CardLayout. Assuming Maze and Hex are already JPanels (if they aren't then make then so. Do something like this

    CardLayout card = new CardLayout();
    JPanel mainPanel = new JPanel(card);
    JPanel firstVisiblePanel = new JPanel();
    Hex hex = new Hex();
    Maze maze = new Maze();
    
    public Center() {
        mainPanel.add(firstVisiblePanel, "initialPanel");
        mainPanel.add(hex, "hex");
        mainPanel.add(maze, "maze");
    
        final JButton jbtHex = new JButton("Hex");
        jbtHex.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                card.show(mainPanel, "hex");
            }
        });
        // do the same as above for the other panels.
        ....
        frame.add(mainPanel); <------- This is the ONLY panel you add to the frame.
        frame.setVisible(true); <----- Should be the LAST thing you do.
    }
    

See more at How to Use CardLayout and a running SO Example

Upvotes: 1

MrLolEthan
MrLolEthan

Reputation: 76

I had something like this happen to me once. Try resizing the frame AFTER using the setVisible() method. Try this:

public Center()
{
frame.setTitle("Prims Maze Generation");
//setVisible(true);
//setSize(400,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400, 200);
jp.add(jb);
jp.add(jb1);
frame.add(jp);

jb.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e)
    {

        x.setVisible(true);
        frame.setSize(400, 200);
        frame.pack();
    }
});
jb.addActionListener(new ActionListener(){
    public void actionPerformed (ActionEvent e)
    {
        h.setVisible(true);
        frame.setSize(400, 200);

    }
});
}

Upvotes: 0

Related Questions