使用另一个类的jbutton调用用油漆和线的主要方法



我在这里有两个课。当我使用其他课程的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的油漆方法。
  • 在该油漆方法中,称此超级涂料Component(??)
  • 从运行类中调用另一类的主要方法。
  • 在秋千GUI中使用while (true) ...
  • ...等

关于您的主要问题 - 不要将其他班级的主要方法称为这样做。而是创建一个实例并调用实例非静态方法。您的代码由于while (true)而冻结,因为它阻止了摇摆事件线程。而不是使用摆动计时器。但最重要的是,将此代码扔掉并重新开始。

改进的建议:

  • 使用摆动计时器进行秋千动画,而不是while (true)
  • 没有在主方法内部运行的游戏循环,因为它太重要了。让它在控制游戏的主要类中运行。主要方法应主要用于设置播放器,然后开始进行交互,仅此而已。
  • 请勿调用其他类静态主要方法。
  • 相反,尝试创建清洁的OOP符合类别的类,不需要此kludge即可运行。
  • 在输入代码之前,写下您的代码规格和计划。首先组织您的想法。
  • 在秋千图形上阅读。教程:这里
  • 覆盖jpanel的paintComponent(...)方法,并确保在其内部调用相同的超级方法。
  • 避免使用无效布局,因为它们会导致大多数平台上看起来很差,并且很难进行调试,增强或更改。

最新更新