0

I am creating a program, and I have my main class that works with all the JButtons, but I can't get my second class that calls the first class launch with the buttons. The JFrame will launch, but the buttons won't.

I am using eclipse just for the information.

Here is the code of my main class:

    public class Unescapable extends JPanel implements ActionListener
{

private static final long serialVersionUID = 1L;
private static final Font font1 = new Font("FONT", Font.BOLD, 75);
protected JButton b1;
protected JButton b2;
protected JButton b3;
protected JButton b5;
protected JTextField t1;

    public Unescapable() 
    {
        t1 = new JTextField("Unescapable");
        t1.setText("Unescapable");
        t1.setBounds(225, 50, 750, 100);
        t1.setForeground(Color.LIGHT_GRAY);
        t1.setOpaque(true);
        t1.setVisible(true);
        t1.setEditable(false);
        t1.setBackground(Color.BLACK);
        t1.setFont(font1);

        b1 = new JButton("Open Window");
        b1.setActionCommand("open");
        b1.setBackground(Color.GRAY);
        b1.setForeground(Color.white);
        b1.setOpaque(true);
        b1.setBorderPainted(false);
        b1.setBounds(280, 200, 390, 40);
        b1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                b1.setBackground(Color.BLUE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                b1.setBackground(Color.GRAY);
            }
        });

        b2 = new JButton("Delete File");
        b2.setActionCommand("delete");
        b2.setBackground(Color.GRAY);
        b2.setForeground(Color.white);
        b2.setOpaque(true);
        b2.setBorderPainted(false);
        b2.setBounds(280, 255, 390, 40);
        b2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                b2.setBackground(Color.BLUE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                b2.setBackground(Color.GRAY);
            }
        });

        b3 = new JButton("Info...");
        b3.setActionCommand("info");
        b3.setBackground(Color.GRAY);
        b3.setForeground(Color.white);
        b3.setOpaque(true);
        b3.setBorderPainted(false);
        b3.setBounds(278, 330, 185, 40);
        b3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                b3.setBackground(Color.BLUE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                b3.setBackground(Color.GRAY);
            }
        });

        b5 = new JButton("Quit Game");
        b5.setActionCommand("close");
        b5.setBackground(Color.GRAY);
        b5.setForeground(Color.white);
        b5.setOpaque(true);
        b5.setBorderPainted(false);
        b5.setBounds(485, 330, 185, 40);
        b5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                b5.setBackground(Color.BLUE);
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                b5.setBackground(Color.GRAY);
            }
        });

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b5.addActionListener(this);

        b1.setToolTipText("Opens Another JWindow");
        b2.setToolTipText("Deletes \"text.txt\"");
        b3.setToolTipText("Give's some information.");
        add(b1);
        add(b2);
        add(b3);
        add(b5);
        add(t1);
        System.out.println("Main Window Is Running");
    }

    public void actionPerformed(ActionEvent e) 
    {
        if ("open".equals(e.getActionCommand()))
        {
            File f = new File("text.txt");
            try
            {
                PrintWriter out = new PrintWriter(f);
                out.println("TheBestMacTutorials");
                out.close();
            }
            catch (Exception e2)
            {

            }
        } 
        else if ("delete".equals(e.getActionCommand()))
        {
            File f = new File("text.txt");
            f.delete();
        }
        else if ("info".equals(e.getActionCommand()))
        {
            InfoBook add = new InfoBook();
            add.call();
        }
        else
        {
            System.out.println("Window Is Now Closing");
            System.exit(0);
        }
    }

    private static void createAndShowGUI() 
    {
        JFrame program = new JFrame("My Program");
        program.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Unescapable newContentPane = new Unescapable();
        program.setContentPane(newContentPane);

        program.setLayout(null);
        program.setVisible(true);
        program.setLocation(850, 445);
        program.setSize(900, 580);
        program.setTitle("Unescapable 1.0");
        program.setBackground(Color.GREEN);
        program.isOpaque();
        program.isForegroundSet();
        program.getContentPane().setBackground(Color.BLACK);

    }

    public static void main(String[] args) 
    {   

        javax.swing.SwingUtilities.invokeLater(new Runnable() 
        {
                public void run() 
                {
                    createAndShowGUI(); 
            }
      });
 }
    }

The code for the "Info Book":

    public class InfoBook extends JFrame
{
private static final long serialVersionUID = 1L;
private static final Font font1 = new Font("FONT", Font.BOLD, 75);
protected JButton b1;
protected JButton b2;
protected JTextField t1;

public InfoBook()
{
    b1 = new JButton("Cancel");
    b1.setActionCommand("cancel");
    b1.setBackground(Color.GRAY);
    b1.setForeground(Color.white);
    b1.setOpaque(true);
    b1.setBorderPainted(false);
    b1.setBounds(280, 200, 390, 40);
    b1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            b1.setBackground(Color.BLUE);
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            b1.setBackground(Color.GRAY);
        }
    });

    b2 = new JButton("More Info");
    b2.setBackground(Color.GRAY);
    b2.setForeground(Color.WHITE);
    b2.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            b2.setBackground(Color.BLUE);
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            b2.setBackground(Color.GRAY);
        }
    });

    t1 = new JTextField("Info");
    t1.setText("Info...");
    t1.setBounds(225, 50, 750, 100);
    t1.setForeground(Color.LIGHT_GRAY);
    t1.setOpaque(true);
    t1.setVisible(true);
    t1.setEditable(false);
    t1.setBackground(Color.BLACK);
    t1.setFont(font1);

    b1.setToolTipText("Opens Another JWindow");
    b2.setToolTipText("Gives More Infomation");

    add(b1);
    add(b2);
    add(t1);
    System.out.println("Info is now running");
}

public static void creatAndShowGUI()
{
    JFrame frame = new JFrame("Info Panel");
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    InfoBook newContentPane = new InfoBook();

    frame.setLayout(null);
    frame.setVisible(true);
    frame.setLocation(850, 445);
    frame.setSize(900, 580);
    frame.setTitle("Unescapable 1.0");
    frame.setBackground(Color.GREEN);
    frame.isOpaque();
    frame.isForegroundSet();
    frame.getContentPane().setBackground(Color.BLACK);

}
public static void call()
{

    javax.swing.SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            creatAndShowGUI();
        }
    });
}

}

I believe thats all the code, and I am not very used to how to format the code in stack overflow, so I might of messed something up. I am probably just getting something messed up, so i am sorry for that but thanks in advance.

  • 1
    Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Nov 26 '15 at 04:02
  • 1
    [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – MadProgrammer Nov 26 '15 at 04:02

1 Answers1

3

The main problem is in your createAndShowGUI in your InfoBook class...

public static void creatAndShowGUI() {
    JFrame frame = new JFrame("Info Panel");
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    InfoBook newContentPane = new InfoBook();

    frame.setLayout(null);
    frame.setVisible(true);
    frame.setLocation(850, 445);
    frame.setSize(900, 580);
    frame.setTitle("Unescapable 1.0");
    frame.setBackground(Color.GREEN);
    frame.isOpaque();
    frame.isForegroundSet();
    frame.getContentPane().setBackground(Color.BLACK);

}

Essentially, you create an instance of newContentPane, but you never use it.

But, you're about to run into a second problem...

} else if ("info".equals(e.getActionCommand())) {
    InfoBook add = new InfoBook();
    add.call();

Here you create an instance of InfoBook, but this instance will have no relationship to the one that is created in your creatAndShowGUI and which I assume you want to show on the screen, so getting information from the class will be impossible

I suspect, you actually want to use a JDialog of some kind instead, which will allow you to present a window to the user, BUT which will block the execution of the code until the user closes the window, at which time you could then interrogate the object for it's information.

See How to Make Dialogs for more details

  • As a general rule of thumb, you don't want to extend from top level containers like JFrame or JDialog, instead, you want to use a JPanel as the base component and add these to what ever container you need.
  • Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
  • You might also like to have a look at The Use of Multiple JFrames, Good/Bad Practice?
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I am trying to get the "Info" Frame to open when the JButton is clicked, and for the Info panel to be seen and the Main window to be invisible, like in the program/game mine craft. When you click "Single Player" it opens a frame inside that frame, and it all is in one frame, so you don't have any pop up windows. I hope you get what I am trying to say. I personally like how the program mine craft did their Main JFrame, so I would like a layout that is something like theirs. – Cader1114 Nov 26 '15 at 14:09