I'm trying to get my Frame when 'Start' button is clicked to go the my class 'Frame', however when I do that the Frame simply just closes instead of doing anything. I am doing this in Java Swing for a school project, so Swing is part of the requirements. If anyone could tell me why this is happening, I would really appreciate it! (I took out the imports for this but I imported all the necessary things) Here's my 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
}
}
As I can see, you are trying to open a new one own Frame. Look at thit part:
@Override
public void actionPerformed(ActionEvent e) {
new Frame();
// closes the old form after start is clicked
dispose();
}
You are creating a Frame without a variable. And after creating a Frame object you should to make it visible, like this:
@Override
public void actionPerformed(ActionEvent e) {
Frame frame1 = new Frame();
frame1.setVisible(true);
// closes the old form after start is clicked
dispose();
}
Because your Frame appears invisible by default.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.