使用来自另一个类的jbutton调用paint和thread的main方法

时间:2014-03-07 00:20:46

标签: java swing

伙计我在这里有两节课。当我使用来自我的其他类的jbutton调用类游戏的主要方法时,只会出现带有白色屏幕的帧。这是代码。谢谢您的帮助。第一节

@SuppressWarnings("serial")
public class Game extends JPanel {

    Ball ball = new Ball(this);
    Racquet racquet = new Racquet(this);
    int score = 0;
    int speed = 1;


    private int getScore() {


        return score;
    }
    private int getSpeed(){

        return speed;
    }

    public Game() {


        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                racquet.keyReleased(e);
            }

            @Override
            public void keyPressed(KeyEvent e) {
                racquet.keyPressed(e);
            }
        });
        setFocusable(true);

    }

    public void move() {
        ball.move();
        racquet.move();
    }

    @Override
    public void paint(Graphics g) {


        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);


        ball.paint(g2d);
        racquet.paint(g2d);

        g2d.setColor(Color.black);
        g2d.setFont(new Font("Showcard Gothic", Font.BOLD, 20));

        g2d.drawString(String.valueOf("Your Score:" + getScore()), 340, 30);


        g2d.drawString(String.valueOf("Game Speed:" + getSpeed()), 340, 220);
        g2d.drawLine(300, 400, 300, -50);



    }


    public void gameOver() {

        JOptionPane.showMessageDialog(this, "your score is: " + getScore(),
                "Game Over", JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
    }




    public static void main(String[] args) throws InterruptedException {

        JFrame frame = new JFrame("Mini Tennis");

        Game game = new Game();

        frame.add(game);
        frame.setSize(550, 400);

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            game.move();
            game.repaint();
            Thread.sleep(10);

    }
}
} 

第二课

public class Main extends JFrame implements ActionListener{

    JPanel mainPanel;
    JLabel title;
    Color bgColor = new Color(51,137,237);
    JButton startBtn, regBtn, viewBtn, exitBtn;
    public Main(){

    setSize(550, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    mainPanel();
    setVisible(true);

    }
    void mainPanel(){

        mainPanel = new JPanel();
        add(mainPanel);
        mainPanel.setBackground(bgColor);
        mainPanel.setLayout(null);

        title = new JLabel("Ball Catcher");
        mainPanel.add(title);
        title.setBounds(130,1,500,200);
        title.setForeground(Color.white);
        title.setFont(new Font("Showcard Gothic", Font.BOLD, 35));

        startBtn = new JButton("START");
        mainPanel.add(startBtn);
        startBtn.setBounds(200,150,130,40);
        startBtn.addActionListener(this);
        startBtn.setForeground(Color.WHITE);
        startBtn.setFont(new Font("Showcard Gothic", Font.BOLD, 20));
        startBtn.setBackground(bgColor);

        regBtn = new JButton("REGISTER");
        mainPanel.add(regBtn);
        regBtn.setBounds(200,200,130,40);
        regBtn.setForeground(Color.WHITE);
        regBtn.addActionListener(this);
        regBtn.setFont(new Font("Showcard Gothic", Font.BOLD, 15));
        regBtn.setBackground(bgColor);

        viewBtn = new JButton("VIEW SCORE");
        mainPanel.add(viewBtn);
        viewBtn.setBounds(200,250,130,40);
        viewBtn.setForeground(Color.WHITE);
        viewBtn.setFont(new Font("Showcard Gothic", Font.BOLD, 15));
        viewBtn.setBackground(bgColor);

        exitBtn = new JButton("EXIT");
        mainPanel.add(  exitBtn);
        exitBtn.setBounds(200,300,130,40);
        exitBtn.setForeground(Color.WHITE);
        exitBtn.setFont(new Font("Showcard Gothic", Font.BOLD, 15));
        exitBtn.setBackground(bgColor);


    }

    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == startBtn){
            String[] args={};
           try {
            Game.main(args);
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        }

        if(e.getSource() == regBtn){

            new Register();
            dispose();

        }

    }
    public static void main(String[] args){

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());


                    break;  
                }
            }
        } catch (Exception ex) 
        {

        }
        new Main();
    }
}

1 个答案:

答案 0 :(得分:2)

你的代码存在很多问题,太多不能解决所有问题,但它包括:

  • 覆盖JPanel的绘画方法。
  • 在调用此super的paintComponent(??)
  • 的paint方法中
  • 从正在运行的类中调用另一个类的main方法。
  • 在Swing gui中使用while (true) ...
  • ...... etc

关于你的主要问题 - 不要调用另一个类的主要方法,因为这样做会丢掉所有的OOP。而是创建一个实例并调用实例非静态方法。因为它阻止了Swing事件线程,你的代码因为while (true)而冻结了。而不是那样,使用Swing Timer。但最重要的是,抛出这些代码并重新开始。

改善建议:

  • 使用Swing Timer进行Swing动画,而不是while (true)
  • 没有在主方法内部运行的游戏循环,因为它对于它来说太重要了。让它在控制游戏的主类中运行。主要方法应该主要用于设置玩家,然后开始互动,就是这样。
  • 不要调用另一个类静态主方法。
  • 而是尝试创建干净的符合OOP标准的类,不需要运行此类kludge的类。
  • 在输入代码之前记下您的代码规范和计划。首先组织你的想法。
  • 阅读Swing Graphics。教程:here
  • 覆盖JPanel的paintComponent(...)方法,并确保在其中调用 相同的 超级方法。
  • 避免使用空布局,因为它们会导致脆弱的GUI在大多数平台上看起来很差,并且很难调试,增强或更改。
相关问题