JPanel 和 JFrame 大小不变

JPanel and JFrame size not changing

我正在 Java 中制作游戏,首先我没有使用导致 repaint() 闪烁的 JPanel,所以我决定使用它。我不确定如何在我当前的代码中实现它。当我尝试这样做时,我得到的只是一个 window,它非常小。我的原Windowclass代码:

public class Window extends JFrame {
private double stepLen;

public Window(double stepLen) {
    this.stepLen = stepLen;

    this.setSize(800, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
    this.setTitle("Frogger");
    this.setLayout(null);

    getContentPane().setBackground(Color.black);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (dim.width - this.getSize().width)/2;
    int y = (dim.height - this.getSize().height)/2;

    this.setLocation(x, y);

    JLabel goal = new JLabel();
    goal.setText("|=========|");
    goal.setForeground(Color.WHITE);
    goal.setFont(new Font("Seif", Font.PLAIN, 20));
    add(goal);
    goal.setBounds(325, -10, 600, 50);

    setFocusable(true);
    requestFocusInWindow();

    this.setVisible(true);
}

这段代码有效,它创建了一个 window。 主要 class:

Window window = new Window(50);

然后我尝试这样做: 我有单独的 GameFrame (JFrame) 和 GameCanvas (JPanel) classes。 框架看起来像这样:

public class GameFrame extends JFrame{
private double stepLen;

public GameFrame() {
    this.stepLen = 50;

    this.setSize(800, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);
    this.setTitle("Frogger");
    this.setLayout(null);
    this.setVisible(true);

    this.getContentPane().setBackground(Color.black);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (dim.width - this.getSize().width)/2;
    int y = (dim.height - this.getSize().height)/2;

    GameCanvas gcanvas = new GameCanvas();
    this.add(gcanvas);
    this.pack();

    this.setLocation(x, y);

}

}


}

和 GameCanvas class

public class GameCanvas extends JPanel {
public GameCanvas() {
    setDoubleBuffered(true);
    JLabel goal = new JLabel();
    goal.setText("|=========|");
    goal.setForeground(Color.WHITE);
    goal.setFont(new Font("Seif", Font.PLAIN, 20));
    this.add(goal);
    goal.setBounds(325, -10, 600, 50);

    this.getPreferredSize();

    this.setVisible(true);

    this.repaint();
}

所以,我在 AP Computer Science class 中学到的方法是在 main 中设置你的帧大小和其他帧特征。这是一个例子:

import javax.swing.JFrame;

public class theSetupClass{
    public static void main(String[] args){
        JFrame theGUI = new JFrame();

        theGUI.setSize(300,400); //Sets the frame size to 300 by 400
        theGUI.setTitle("Example");
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        theComponentClass component = new theComponentClass(); //Create new theComponentClass
        frame.add(component);//Add theComponentClass to theGUI

        frame.setVisible(true);
    }
}

上面的代码创建了 JFrame 并向其中添加了以下 class。

import java.awt.*;
import javax.swing.*;
public class theComponentClass extends JComponent{ 
    public void paintComponent(Graphics g){
        Graphics2D g2 = (Graphics2D) g;

        Rectangle r = new Rectangle(10,10,this.getWidth()-10,this.getHeight()-10); 
        //Creates a rectangle that is 10 pixels away from all sides of the frame
        g2.fill(r); //Draws and fills the rectangle
    }
}

希望对您有所帮助!

Camickr 是正确的 - 投票并标记他的答案是正确的,这只是为了避免他拔出他剩下的一点点头发

  • 您正在使用 null 布局,但没有接管它的职责
  • 未能为组件提供调整大小提示以允许布局管理器(您不再使用)完成它的工作

这些都是常见的错误,已经提供了无数的答案

游戏框架

public class GameFrame extends JFrame {

    private double stepLen;

    public GameFrame() {
        this.stepLen = 50;

        this.setSize(800, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setTitle("Frogger");
        // Well, there's your problem...
        //this.setLayout(null);
        // Don't do this here...
        this.setVisible(true);

        this.getContentPane().setBackground(Color.black);

        // Simpler way to achieve this
        //Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        //int x = (dim.width - this.getSize().width) / 2;
        //int y = (dim.height - this.getSize().height) / 2;

        GameCanvas gcanvas = new GameCanvas();
        this.add(gcanvas);
        this.pack();

        //this.setLocation(x, y);

        setLocationRelativeTo(null);
        setVisible(true);

    }

}

游戏画布

public class GameCanvas extends JPanel {

    public GameCanvas() {
        // Pointless
        //setDoubleBuffered(true);
        JLabel goal = new JLabel();
        goal.setText("|=========|");
        goal.setForeground(Color.WHITE);
        goal.setFont(new Font("Seif", Font.PLAIN, 20));
        this.add(goal);
        // Pointless
        //goal.setBounds(325, -10, 600, 50);

        // Pointless
        //this.getPreferredSize();
        // Pointless
        //this.setVisible(true);
        // Pointless
        //this.repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }

    @Override
    public void paintComponent(Graphics g) {

        int firstRoad = 5;
        int i = 0;
        int max = 10;

        Graphics2D g2 = (Graphics2D) g;

        super.paintComponent(g2);
        g2.setColor(Color.WHITE);
        g2.drawRect(5, 30, 75, 40);

        while (i < max) {
            g2.setColor(Color.WHITE);
            g2.setStroke(new BasicStroke(3));

            if (i % 2 == 0) {
                g.setColor(Color.WHITE);
                g.drawRect(3, firstRoad + 50 * i, 793, 50);
                //g.fillRect(3, firstRoad + 50 * i, 793, 50);
            } else {
                g2.setColor(Color.WHITE);
                g2.drawRect(3, firstRoad + 50 * i, 793, 50);
            }
            i++;
        }
    }
}