JPanel和JFrame的大小没有变化

时间:2017-03-11 19:30:44

标签: java swing jframe jpanel size

我正在用Java制作游戏,首先我没有使用导致repaint()闪烁的JPanel,因此我决定使用它。我不确定如何在我当前的代码中实现它。当我试图这样做时,我得到的只是一个小到它的窗户。我原来的Window类代码:

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 window = new Window(50);

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

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类

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();
}

2 个答案:

答案 0 :(得分:2)

Camickr是正确的 - 上去投票并将他的答案标记为正确,这只是为了避免他拉出他剩下的小头发

  • 您正在使用null布局,而不承担其责任
  • 无法为组件提供大小调整提示,以允许布局管理器(您不再使用)执行此操作

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

GameFrame

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);

    }

}

的GameCanvas

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++;
        }
    }
}

答案 1 :(得分:-1)

所以,我在AP计算机科学课上教授的方法是在你的主要设置框架大小和其他框架特征。这是一个例子:

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并将以下类添加到其中。

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
    }
}

我希望你觉得这很有帮助!