J'essaie d'obtenir mon cadre lorsque le bouton «Démarrer» est cliqué pour accéder au cadre de ma classe, mais quand je le fais, le cadre se ferme simplement au lieu de faire quoi que ce soit. Je fais cela dans Java Swing pour un projet scolaire, donc Swing fait partie des exigences. Si quelqu'un pouvait me dire pourquoi cela se produit, je l'apprécierais vraiment! (J'ai retiré les importations pour cela mais j'ai importé toutes les choses nécessaires) Voici mon code:

    package snake;

public class Start extends JFrame  implements ActionListener {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Start frame = new Start();
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


/**
 * Create the frame.
 */
public Start() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 500);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
    gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
    gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
    contentPane.setLayout(gbl_contentPane);

    JLabel lblSnake = new JLabel("Snake");
    GridBagConstraints gbc_lblSnake = new GridBagConstraints();
    gbc_lblSnake.insets = new Insets(0, 0, 5, 0);
    gbc_lblSnake.gridx = 7;
    gbc_lblSnake.gridy = 0;
    contentPane.add(lblSnake, gbc_lblSnake);

    JButton btnStart = new JButton("Start");
    GridBagConstraints gbc_btnStart = new GridBagConstraints();
    gbc_btnStart.insets = new Insets(0, 0, 5, 0);
    gbc_btnStart.gridx = 7;
    gbc_btnStart.gridy = 3;
    contentPane.add(btnStart, gbc_btnStart);

    // action listener for start btn
    btnStart.addActionListener(new ActionListener() {

        // once this is clicked on, it should call the GUI
        @Override
        public void actionPerformed(ActionEvent e) {
            new Frame();
            // closes the old form after start is clicked
            dispose();

        }
    });

    JButton btnBack = new JButton("Back");
    GridBagConstraints gbc_btnBack = new GridBagConstraints();
    gbc_btnBack.insets = new Insets(0, 0, 5, 0);
    gbc_btnBack.gridx = 7;
    gbc_btnBack.gridy = 5;
    contentPane.add(btnBack, gbc_btnBack);

    JTextArea textArea = new JTextArea("\t\t\t        SNAKE INSTRUCTIONS:\t\t\t\n\n\n1) Use the right, left, up, and down arrow keys to move the snake right, left, up, and down respectively.\n\n2) Each apple collected by the snake is a point\n\n3) If the snake collides with the wall or itself the game is over and you lose!\n\nWe hope you enjoy playing this old-school snake throwback!");
    GridBagConstraints gbc_textArea = new GridBagConstraints();
    gbc_textArea.fill = GridBagConstraints.BOTH;
    gbc_textArea.gridx = 7;
    gbc_textArea.gridy = 7;
    contentPane.add(textArea, gbc_textArea);



}


@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}


 }
0
Dee 14 janv. 2020 à 07:04