gpt4 book ai didi

java - 使用 ActionListener 从一个 JFrame 跳转到下一个不起作用

转载 作者:行者123 更新时间:2023-12-02 09:07:04 26 4
gpt4 key购买 nike

当单击“开始”按钮进入我的类“框架”时,我试图获取我的框架,但是当我这样做时,框架只是关闭而不是执行任何操作。我正在 Java Swing 中为学校项目执行此操作,因此 Swing 是要求的一部分。如果有人能告诉我为什么会发生这种情况,我将非常感激! (我为此取出了导入,但导入了所有必要的东西)这是我的代码:

    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

}


}

最佳答案

正如我所看到的,您正在尝试打开一个新的自己的框架。看这部分:

@Override
public void actionPerformed(ActionEvent e) {
new Frame();
// closes the old form after start is clicked
dispose();

}

您正在创建一个没有变量的框架。创建 Frame 对象后,您应该使其可见,如下所示:

@Override
public void actionPerformed(ActionEvent e) {
Frame frame1 = new Frame();
frame1.setVisible(true);
// closes the old form after start is clicked
dispose();

}

因为默认情况下您的框架是不可见的。

关于java - 使用 ActionListener 从一个 JFrame 跳转到下一个不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59727371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com