簡體   English   中英

使用 ActionListener 從一個 JFrame 跳轉到下一個不工作

[英]Jumping from one JFrame to the next not working using ActionListener

當單擊“開始”按鈕進入我的類“框架”時,我試圖獲取我的框架,但是當我這樣做時,框架只是關閉而不是做任何事情。 我正在 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();

        }

因為默認情況下您的 Frame 是不可見的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM