Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用ComponentAdapter调整面板大小(不工作)_Java_Swing_Jframe_Resize_Jpanel - Fatal编程技术网

Java 尝试使用ComponentAdapter调整面板大小(不工作)

Java 尝试使用ComponentAdapter调整面板大小(不工作),java,swing,jframe,resize,jpanel,Java,Swing,Jframe,Resize,Jpanel,当网格对象实例化时,将创建JFrame和JPanel。在JPanel上绘制线以创建方形网格。理想情况下,如果调整窗口大小,网格将缩放 import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.Graphics; import java.awt.Color; import java.awt.event.ComponentListener; import java.awt.event.ComponentEvent; i

当网格对象实例化时,将创建JFrame和JPanel。在JPanel上绘制线以创建方形网格。理想情况下,如果调整窗口大小,网格将缩放

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;

public class Grid {
    private int lines;
    private int space;
    public static final int DEFAULT_LINES = 5;
    public static final int DEFAULT_SPACE = 5;

private JPanel panel = new GridPanel();
private JFrame frame;

public Grid(String name, int lines, int space) {
    this.frame = new JFrame(name);
    this.lines = lines;
    this.space = space;
    this.frame.setSize((lines * space), (lines * space));
}

private class GridPanel extends JPanel {
    private int start = 0;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int end = (lines * space);
        g.setColor(Color.BLACK);
        for (int i = 0; i < lines; i++) {
            int crawl = (space * i);
            g.drawLine(start, crawl, end, crawl);
            g.drawLine(crawl, start, crawl, end);
        }
    }
}

/*private class GridHandler extends ComponentAdapter {

    @Override
    public void componentResized(ComponentEvent e) {
        super.componentResized(e);
        setSpace();
        frame.repaint();
        frame.revalidate();
    }
}*/

public void setSpace() {
    space = (frame.getSize().width * frame.getSize().height) / lines;
}

public void run() {
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.add(panel);
    //frame.addComponentListener(new GridHandler());
    frame.setVisible(true);
}
}


public class Run {
    public static final int ONE_LINES = 15;
    public static final int ONE_SPACE = 20;

    public static final int TWO_LINES = 35;
    public static final int TWO_SPACE = 16;

    public static void main(String[] args) {
        Grid grid1 = new Grid("Grid One", ONE_LINES, ONE_SPACE);
        Grid grid2 = new Grid("Grid Two", TWO_LINES, TWO_SPACE);
        grid1.run();
        grid2.run();
    }
}
import javax.swing.JPanel;
导入javax.swing.JFrame;
导入java.awt.Graphics;
导入java.awt.Color;
导入java.awt.event.ComponentListener;
导入java.awt.event.ComponentEvent;
导入java.awt.event.ComponentAdapter;
公共类网格{
专用int线;
私有int空间;
公共静态最终整数默认值_行=5;
公共静态final int DEFAULT_SPACE=5;
private JPanel panel=new GridPanel();
私有JFrame;
公共网格(字符串名称、整数行、整数空间){
this.frame=新JFrame(名称);
这条线=线;
这个空间=空间;
此.frame.setSize((行*空格),(行*空格));
}
私有类GridPanel扩展了JPanel{
私有int start=0;
@凌驾
公共组件(图形g){
超级组件(g);
int end=(行*空格);
g、 设置颜色(颜色为黑色);
对于(int i=0;i

这是仅有的两个正在使用的文件。处理程序当前被注释掉,代码执行预期的操作。将创建两个带有网格的窗口。但是,当处理程序实现时,网格不再显示。处理程序的正确实现是什么?

对于任何关心的人来说,这是一个可行的解决方案。事实证明,不需要组件化的事件处理程序。重新调整JFrame的大小时,将自动执行paintComponent

下面的代码创建两个单独的JFrame。每个JFrame包含一个JPanel。在JPanel上绘制一个独特的正方形网格。如果重新调整JFrame的大小,网格也将重新调整大小(同时仍保持方形


为什么不使用组件的
宽度
高度
并在调用
paintComponent
时计算网格大小?什么?谢谢你的建议!我已经在下面发布了我的解决方案(它使用了
宽度
高度
,正如您所建议的),我发现这样的代码更具可读性。再次感谢!
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;

public class Grid extends JPanel {
    private static final int DEFAULT_COUNT = 10;
    private static final int DEFAULT_SIZE = 100;

    private int count;

    public Grid(int count, int size) {
        if (size < 1) {
            this.count = DEFAULT_COUNT;
            this.setPreferredSize(new Dimension(DEFAULT_SIZE, DEFAULT_SIZE));
        } else {
            this.count = count;
            this.setPreferredSize(new Dimension(size, size));
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        graphics.setColor(Color.black);

        Dimension size = getSize();
        int w = size.width;
        int h = size.height;

        if (w > h) {
            w = h;
        } else if (h > w) {
            h = w;
        }

        int spaceWidth = (int)((double)w / count);
        int spaceHeight = (int)((double)h / count);
        for (int row = 0; row <= count; row++) {
            int y = (row * spaceHeight);
            int x = (row * spaceWidth);
            graphics.drawLine(0, y, (spaceWidth * count), y);
            graphics.drawLine(x, 0, x, (spaceHeight * count));
        }
    }
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;

public class Test {
    public static final int COUNT_1 = (-5);
    public static final int SIZE_1 = 4;

    public static final int COUNT_2 = 35;
    public static final int SIZE_2 = 16;

    public static void main(String[] args) {
        int area1 = COUNT_1 * SIZE_1;
        int area2 = COUNT_2 * SIZE_2;

        Grid grid = new Grid(COUNT_1, area1);
        JFrame frame = new JFrame("Grid");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(grid);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Grid grid2 = new Grid(COUNT_2, area2);
        JFrame frame2 = new JFrame("Grid");
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame2.add(grid2);
        frame2.pack();
        frame2.setLocationRelativeTo(null);
        frame2.setVisible(true);
    }
}