-1

So far all i have managed to get is the JButton to close the first JFrame (frame) but the other will not open.

Frame class code

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Frame {
public static void main(String[] args) {

    // Frame - Labelled BrickFall
    final JFrame frame = new JFrame();
    frame.setSize(1290, 730);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("BrickFall");
    frame.setLayout(null);
    frame.setLocationRelativeTo(null);

    // Start Button
    JButton Start = new JButton("Start");
    Start.setBounds(100, 300, 1080, 50);
    frame.add(Start);

    // Exit Button
    JButton Exit = new JButton("Exit");
    Exit.setBounds(369, 375, 540, 50);
    frame.add(Exit);

    // Closes when Exit Clicked
    Exit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
              System.exit(0);

              }
              });

    //New Frame opens when Start Clicked
    Start.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
             frame.setVisible(false);



        }

    });


    // Background Image
    JLabel background = new JLabel("");
    background.setBounds(0, 0, 1280, 720);
    background.setIcon(new ImageIcon(Frame.class.getResource("/resources/images/Title.png")));
    frame.getContentPane().setLayout(null);
    frame.getContentPane().add(background);

    frame.setVisible(true);

}

protected static void dispose() {
    // TODO Auto-generated method stub
}

}

On the (frame) action listener i have left out the second line to open the other JFrame as my attempts wont work

Game class code

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class game {
public static void main(String[] args) {

    // Frame - Labelled BrickFall
    JFrame game = new JFrame();
    game.setSize(1290, 730);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setTitle("BrickFall");
    game.setLayout(null);
    game.setLocationRelativeTo(null);

    // Background Image
    JLabel GameBackground = new JLabel("");
    GameBackground.setBounds(0, 0, 1280, 720);
    GameBackground.setIcon(new ImageIcon(Frame.class.getResource("/resources/images/Fill In.png"))); //Change Picture When Required
    game.getContentPane().setLayout(null);
    game.getContentPane().add(GameBackground);

    game.setVisible(true);


}
}

If anyone has any suggestions on how I can sort this I will be very thankful.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

3

You're disposing the JFrame and setting its default close operation to JFrame.EXIT_ON_CLOSE which closes the JVM, shutting down your entire program.

  • Solution 1: Make your JFrame invisible via setVisible(false)
  • or if you do use dispose(), make sure that you set its default close operation to anything but what you're currently doing: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); as this also closes the JVM.
  • Solution 2: better yet, make the closing window a JDialog.
  • Solution 3: Even better, don't spit different windows at the user, but rather swap views by swapping JPanels with a CardLayout. For details, please check out the CardLayout Tutorial.
  • Suggestion: don't use null layouts like you're doing, but rather learn to use the different layout managers.
  • Suggestion: get out of the main method. Don't make your classes nothing but large static main methods, basically 1970's procedural programming. Learn to use and then create OOP-compliant classes with constructors, non-static fields, non-static methods, with states and behaviors, and have these classes interact in a pleasing and useful fashion.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373