【问题标题】:JPanel and JFrame size not changing [duplicate]JPanel 和 JFrame 大小不变[重复]
【发布时间】:2017-03-11 19:30:44
【问题描述】:

我正在用 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();
}

【问题讨论】:

    标签: java swing jframe jpanel size


    【解决方案1】:

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

    【讨论】:

    • @camickr 真的,如果这对您来说是个问题,也许您应该停止回答应该标记为重复和/或否决存在的问题:P
    • 大多数问题都略有不同,找到与所有差异匹配的“重复项”需要时间。我试图突出问题并使用教程来解决问题。 OP 最初给出了三个建议:1) 类名,2) 不要使用空布局和 3) 覆盖 getPreferredSize()。 OP 用新的类名重新发布了代码,但完全忽略了第 2 和第 3 条建议,表明他们没有花时间阅读教程和下载代码。现在 OP 将期望得到每个问题的代码。
    • @camickr 完成并完成
    • 这是怎么复制的?该答案谈到将错误的组件添加到框架中。
    • @camickr 因为我所做的一切都不会让你开心,所以我会让你重新打开并找到合适的副本 - 我选择它的主要原因之一是因为它链接到一个你们描述自定义绘画方法的博客 - 我会删除答案,但我不能
    【解决方案2】:

    所以,我在 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
        }
    }
    

    希望对您有所帮助!

    【讨论】:

    • 两个帐户的错误方法; 1-你不需要设置框架的大小,原因很多,但主要是因为框架的大小需要包括(可变大小的)框架边框,这总是使内容变小; 2-你没有遵守油漆链,这将导致其他问题永无止境,此外,paintComponent永远不需要public
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多