按下开始按钮Java后程序“冻结”

时间:2019-05-23 17:49:51

标签: java jframe jpanel jbutton cardlayout

我正在尝试实现一个开始按钮,按下该按钮会将用户带到另一个页面,您可以在其中玩简单的游戏。但是,每次按开始按钮时,程序均无法正确运行,并显示无法玩的游戏。该游戏应具有移动平台(可在SimpleWindow中运行),但在FrontPage中按下按钮后将无法运行。如何切换面板,以便程序都能在两者中正常运行?

起始页

$

应运行的程序

import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.swing.*;

public class FrontPage extends JPanel implements ActionListener {
    private JButton start;
    private JButton rules;



    public FrontPage() {
        setLayout(new GridBagLayout());

        start = new JButton("Start");
        rules = new JButton("Rules");

        add(start);
        add(rules);
        start.addActionListener(this);  
        rules.addActionListener(this);  

    }  

    public void actionPerformed(ActionEvent e) {  

        SimpleWindow window = new SimpleWindow();
        add(window);

        if (e.getSource() == start){    
            JFrame w = new JFrame("Simple Window");
            w.setBounds(100, 100, 1000, 600);
            w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //SimpleWindow panel = new SimpleWindow();
            SimpleWindow panel = window;
            w.addKeyListener(panel);
            w.add(panel);
            w.setResizable(true);
            w.setVisible(true);

            repaint();
        } else if (e.getSource() == start){  


        }
    }


    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int width = getWidth();
        int height = getHeight();

        Font f1 = new Font("TimesRoman", Font.BOLD, (width / 18));

        g.setFont(f1);
        g.drawString("SQUARED", (int) ((int) width / 2.5), (int) (2.5 * height / 8));

        // b.setBounds(50,100,100,100);

    }



    public static void main(String[] args) {
        JFrame a = new JFrame("Squared");
        a.setBounds(100, 100, 1000, 600);
        a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FrontPage panel = new FrontPage();
        // w.addActionListener(panel);
        panel.setBackground(Color.WHITE);
        a.add(panel);
        a.setResizable(true);
        a.setVisible(true);

        //panel.run();
    }

}

预期结果:移动平台和跑步者

实际结果:程序“冻结”

0 个答案:

没有答案