java - 重新定位 JFrame 的 ContentPane

标签 java swing jframe

我试图在我的 JFrame 的内容 Pane 中绘制一个网格,但是当我这样做时,一切都已关闭。网格开始时太宽且偏移(大概设置在窗口边框与内容 Pane 重叠的位置)并且它无法正确绘制整个网格。当我使用 frame.setUndecorated(true); 时一切正常。

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.*;

@SuppressWarnings("serial")
class Creator extends JFrame{

    JFrame frame;
    int[][] Grid = new int[18][27];
    public Creator(){
        frame = this;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for(int i = 0; i < 18; i++) {
            for(int j = 0; j < 27; j++) {
                Grid[i][j] = 0;
            }
        }
        setSize(864, 544);

        getContentPane().addMouseListener(new MouseAdapter() {            
            @Override
            public void mouseClicked(MouseEvent e) {
                int yLocation = e.getX()/32;
                int xLocation = e.getY()/32;
                System.out.println(xLocation + " " + yLocation);

                if(e.getButton() == 1){
                    if(xLocation < 1 || yLocation < 1){
                        Grid[xLocation][yLocation] = 2;
                        System.out.println("Enemy Added");
                    }else if(xLocation > 16 || yLocation > 25){
                        Grid[xLocation][yLocation] = 2;
                        System.out.println("Enemy Added");
                    }else {
                        Grid[xLocation][yLocation] = 1;
                        System.out.println("Obstacle Added");
                    }
                }else {
                    Grid[xLocation][yLocation] = 0;
                    System.out.println("Item Removed");
                }
                frame.invalidate();
                frame.validate();
                frame.repaint();
            }
        });

    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, 864, 544);
        Graphics2D g2 = (Graphics2D) g;
        for(int i =0; i < 18; i++) {
            for(int j =0; j < 27; j++) {
                if(Grid[i][j] != 0){
                    if( i < 1 || j < 1 || i > 26 || j > 17) {
                        g2.setColor(Color.RED);
                        g2.fillRect(j*32, i*32, 32, 32);
                    }else {
                        g2.setColor(Color.BLUE);
                        g2.fillRect(j*32, i*32, 32, 32);
                    }
                }
                //System.out.print(Grid[i][j]);
            }
            //System.out.println();
        }
        for(int i = 0; i < 27; i++) {
            Line2D lin = new Line2D.Float(i*32, 0, i*32, 544);
            g2.draw(lin);
        }
        for(int i = 0; i < 18; i++) {
            Line2D lin = new Line2D.Float(0, i*32, 864, i*32);
            g2.draw(lin);
        }

    }

    public static void main(String []args){
        Creator s=new Creator();
        s.setVisible(true);
    }
}

最佳答案

不要设置框架的大小。

相反,创建 JPanel 或 JComponent 的子类,覆盖 paintComponent() 来绘制您的网格,并覆盖 getPreferredSize() 以返回它应该具有的大小(864 x 544)。将此面板添加到您的框架并调用 pack() 以确保框架尺寸自行调整为以其首选尺寸显示其组件所需的尺寸。

编辑:示例:

public class GridPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        // paint the grid here
    }

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

在 JFrame 构造函数中:

GridPanel gridPanel = new GridPanel();
this.add(gridPanel);
this.pack();

关于java - 重新定位 JFrame 的 ContentPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19067593/

相关文章:

java - 给定一个class文件,如何检测它是否使用JNI?

java - 一段关于剪刀石头布游戏的Java代码

java - 如何获得使用 Google 地点自动完成功能的 Intent ?

java - 一些功能没有按预期工作

java - JFrame 不保持显示 - java

java - 如何使用 StateManager gxt 3

swing - JLabel 水平定位未按预期工作

java - 如何停止2 javax.swing.Timer

Java JFrame 失去焦点时显示初始化内容

java - 如何检查 JFrame 中选中了哪些复选框?