簡體   English   中英

使用來自另一個類的jbutton調用paint和thread的main方法

[英]Calling a main method with paint and thread using a jbutton from another class

伙計我在這里有兩節課。 當我使用來自我的其他類的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();
    }
}

您的代碼存在很多問題,太多而無法解決所有問題,但它包括:

  • 覆蓋JPanel的繪制方法。
  • 在那個調用這個super的paintComponent(??)的paint方法里面
  • 從正在運行的類中調用另一個類的main方法。
  • 在Swing gui中使用while (true) ...
  • ......等

關於你的主要問題 - 不要調用另一個類的主要方法,因為這樣做會丟掉所有的OOP。 而是創建一個實例並調用實例非靜態方法。 你的代碼是凍結的,因為它阻塞了Swing事件線程的方式while (true) 而不是那樣,使用Swing Timer。 但最重要的是,拋出這些代碼並重新開始。

建議改善:

  • 使用Swing Timer進行Swing動畫, while (true)不是while (true)
  • 沒有在main方法內部運行的游戲循環,因為它對於它來說太重要了。 讓它在控制游戲的主類中運行。 主要方法應該主要用於設置玩家,然后開始他們互動,就是這樣。
  • 不要調用另一個類靜態main方法。
  • 而是嘗試創建干凈的符合OOP的類,這些類不需要運行這個kludge。
  • 在輸入代碼之前記下您的代碼規范和計划。 首先組織你的想法。
  • 閱讀Swing Graphics。 教程: 這里
  • 覆蓋JPanel的paintComponent(...)方法,並確保在其中調用相同的 super方法。
  • 避免使用空布局,因為它們會導致脆弱的GUI在大多數平台上看起來很差,並且很難調試,增強或更改。

暫無
暫無

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

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