简体   繁体   English

重新定位JFrame的ContentPane

[英]Re-positioning the ContentPane of a JFrame

I'm trying to draw a grid inside the content pane of my JFrame but when I do, everything is off. 我正在尝试在我的JFrame的内容窗格中绘制网格,但是当我这样做时,一切都已关闭。 The grid starts too wide and offset (presumably set where the border of the window overlaps the contentpane) and it doesn't draw the entire grid correctly. 网格开始太宽并且偏移(可能设置在窗口边框与内容窗格重叠的位置)并且它不能正确地绘制整个网格。 When i use frame.setUndecorated(true); 当我使用frame.setUndecorated(true); everything works perfectly. 一切都很完美。

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

Don't set the size of the frame. 不要设置框架的大小。

Instead, create a subclass of JPanel or JComponent, override paintComponent() to paint your grid, and override getPreferredSize() to return the size it should have (864 x 544). 相反,创建JPanel或JComponent的子类,覆盖paintComponent()以绘制网格,并覆盖getPreferredSize()以返回它应具有的大小(864 x 544)。 Add this panel to your frame and call pack() to make sure that the frame size adjusts itself to the size needed to display its component with their preferred size. 将此面板添加到框架并调用pack()以确保框架大小将自身调整为显示其组件所需大小的首选大小。

EDIT: example: 编辑:示例:

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

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

and in the JFrame constructor: 并在JFrame构造函数中:

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

JB Nizet, gives you the excellent solution. JB Nizet为您提供出色的解决方案。 Change your code as follows: 更改您的代码如下:

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 JPanel{


    static int[][] Grid = new int[18][27];
    public Creator(){        
        setSize(getPreferredSize());
    }

    public void paint(Graphics g) {
        g.clearRect(0, 0, getWidth(), getHeight());
        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, getHeight());
            g2.draw(lin);
        }
        for(int i = 0; i < 18; i++) {
            Line2D lin = new Line2D.Float(0, i*32, getWidth(), i*32);
            g2.draw(lin);
        }

    }

    public static void main(String []args){
        JFrame frame=new JFrame();
        Creator s=new Creator();
        frame.setSize(864, 544); 
        frame.add(s);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.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();
            }
        });
//        s.setUndecorated(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM