【问题标题】:Why am I unable to add a button along with rendering in my jpanel, they overlap为什么我无法在我的 jpanel 中添加按钮以及渲染,它们重叠
【发布时间】:2017-06-14 17:36:09
【问题描述】:

这是我的 Pong 游戏主类的代码,在我添加指令按钮之前它工作正常。当我添加指令按钮时,屏幕不再显示按钮以外的任何内容。

 package pongg;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
//import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Pong  extends JFrame implements ActionListener, KeyListener{
    public static Pong pong;
    int width =700, height = 700;
    public Renderer renderer;
    public Paddle player1;
    public Paddle player2;
    public Ball ball;
    JButton rules = new JButton ("INSTRUCTIONS"); 


    public boolean comp = false; //ai 
    public boolean w, s, up, down;  // so many booleans so that when u press two keys they dont interfere w each other
    public int gameStatus = 0; // 1 = paused, 0= stop, 2=play

    public Pong(){
    Timer timer = new Timer(20, this);
    JFrame jframe = new JFrame("PONG"); 
    renderer = new Renderer();
    jframe.setSize(width +15, height+35);
    jframe.setVisible(true);
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.add(renderer);


    jframe.addKeyListener (this);

    timer.start();

    if (gameStatus ==0){
        rules.setSize(200,100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.add(rules);
        setVisible(true);
    }

    }

    private void add(JButton rules2) {


    }

    public void start (){

    gameStatus =2;
    player1 = new Paddle(1);
    player2 = new Paddle (2);
    ball = new Ball(this);
    }

    public void update (){
    if (w){
    player1.move(true); // up is true and down is false
    }

    if (s){
    player1.move(false);
    }

    if (up){
    player2.move(true);
    }

    if (down){
    player2.move(false);
    }

    ball.update(player1, player2);
    }


    public void render (Graphics2D g){

    g.setColor (Color.ORANGE);
    g.fillRect(0,0, width, height);
    //g.setRenderingHint (RenderingHints.KEY_ANTIALISING, RenderingHint.VALUE_ANTIALIAS_ON);

    if (gameStatus ==0){

    g.setColor(Color.BLACK);
    g.setFont(new Font("Arial", Font.PLAIN, 40)); 
    g.drawString ("WELCOME TO PONG TWIST!", width/10, 100);

    g.setFont(new Font("Arial", Font.PLAIN, 20)); 
    g.drawString ("Press Space to Play", width/2 -150, height/2 - 50 );
    g.drawString ("Press Shift to Play with Computer", width/2 -200, height/2);


    }

    if (gameStatus ==2 || gameStatus ==1){

    g.setColor(Color.WHITE);
    g.setStroke(new BasicStroke (5f)); // make it thicker
    g.drawLine(width/2, 0, width/2, height); // FIX MAYBE


    player1.render (g);
    player2.render(g);
    ball.render(g);
    }
    if (gameStatus ==1){
    g.setColor(Color.WHITE);

    g.drawString ("PAUSED", width/2 -105, height /2 - 105);
    }
    }

【问题讨论】:

  • 什么是指令按钮?您在代码的哪一部分添加它?能不能说的详细点?

标签: java rendering jbutton overlap


【解决方案1】:

其中一个可能的问题是布局不正确。加rules.setLayout(null)之前可以试试:

rules.setSize(200, 100);
jframe.add(rules);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

另外,你可以有更多的解释here

【讨论】:

    猜你喜欢
    • 2012-10-18
    • 2010-09-23
    • 2021-12-06
    • 1970-01-01
    • 2018-01-07
    • 2014-11-26
    • 2014-06-01
    • 2017-04-17
    • 2017-10-01
    相关资源
    最近更新 更多