This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                6年前关闭。
            
        

这是代码:

Aa级

package com.MahBonnets.Game;

import javax.swing.*;

public class aa {

public static ab f = new ab();
public static int width = 600;
public static int height = 400;
public static void main(String args[]) {
        f.setSize(width, height);
        f.setResizable(false);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setTitle("MAH BONNETS IS GONE");
        f.setLocationRelativeTo(null);
        System.out.println("Running!!");
}
}


b

package com.MahBonnets.Game;

import java.awt.GridLayout;
import javax.swing.*;

public class ab extends JFrame {

public ac panel;

public ab() {
    panel = new ac(this);
    setLayout(new GridLayout (1, 1, 0, 0));
    add(panel);
}
}


和交流

package com.MahBonnets.Game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.*;

public class ac extends JPanel implements Runnable {
public Rectangle floor;

public int floorheight = 80;
public int fps = 1000;

public boolean objectDefine = false;

public Thread game;
public ac(ab f) {
    setBackground(Color.black);

    defineObjects();

    game = new Thread(this);
    game.start(); }

void defineObjects() {
        floor = new Rectangle(-10, aa.height-floorheight, aa.width+10, floorheight);
        objectDefine = true;
        repaint();
}

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

    if(objectDefine) {
        g.setColor(Color.RED);
        g.fillRect(floor.x, floor.y, floor.width, floor.height);
}
}


public void fpsSetter() {
try{
    Thread.sleep(fps/1000);
}catch(Exception e) {
    e.printStackTrace();

        }

  }
  @Override
  public void run(){
      // TODO Auto-generated method stub
  }
}


应该发生的没有发生的事情是,应该在JFrame的底部出现一个红色矩形。我对编程完全陌生,但是我已经查看了与矩形有关的代码部分,并且一切看起来都井井有条……至少……据我所知。

如果您有任何疑问,请帮助我。谢谢。

这是我一直关注的YouTube教程http://www.youtube.com/watch?v=0lfhcKAIr-8

最佳答案

不要依赖魔术数字...
不要依赖可能与实际情况不符的参数(public static int width = 600与子组件的大小将不一样)。使用getWidthgetHeight获取组件的实际大小...
摆脱defineObjects并依靠组件的实际已知状态
摆脱paint方法,改用paintComponent
static并不总是您的朋友
请勿将YouTube用作参考,除非张贴者有参考;)
务必阅读Performing Custom Painting
务必阅读Painting in AWT and Swing
务必阅读Concurrency in Swing


将您的paint方法替换为类似这样的内容...

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.

    int x = -10;
    int y = getHeight() - floorheight;
    int width = getWidth() + 10;
    int height = floorheight;
    floor = new Rectangle(x, y, width, height);
    g.setColor(Color.RED);
    g.fillRect(floor.x, floor.y, floor.width, floor.height);
}


而且Thread.sleep(1000 / 1000)几乎没有睡眠,没有区别;)-25fps大约40毫秒;)

关于java - 预期框架中为红色矩形,什么也看不到,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16392094/

10-11 22:28
查看更多