Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 JFrame调整大小问题_Java_User Interface_Jframe - Fatal编程技术网

Java JFrame调整大小问题

Java JFrame调整大小问题,java,user-interface,jframe,Java,User Interface,Jframe,因此,我尝试创建一个JFrame,它将根据某个固定值调整宽度和高度 i、 e。 当您使用鼠标更改JFrame的大小时,如果新的宽度或高度值不是20的倍数(以下代码中的固定大小),它会将宽度或高度四舍五入到最接近的20,然后调整JFrame的大小 宽度似乎起作用,但高度不起作用 请帮忙 import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("serial") public class

因此,我尝试创建一个JFrame,它将根据某个固定值调整宽度和高度

i、 e。 当您使用鼠标更改JFrame的大小时,如果新的宽度或高度值不是20的倍数(以下代码中的固定大小),它会将宽度或高度四舍五入到最接近的20,然后调整JFrame的大小

宽度似乎起作用,但高度不起作用

请帮忙

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class test extends JFrame {

private static JPanel panel = new JPanel(null);
private static int SIZE = 20;

public test() {
    panel.setBackground(Color.RED);
    this.add(panel);
}

public static void main(String[] args) {
    final test frame = new test();
    frame.setSize(600, 400);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.addComponentListener(new ComponentListener() {
        public void componentResized(ComponentEvent e) {
            if ((panel.getWidth() % SIZE != 0) || (panel.getHeight() % SIZE != 0)) {
                int screenWidth = ((panel.getWidth() + SIZE) / SIZE) * SIZE;
                int screenHeight = ((panel.getHeight() + SIZE) / SIZE) * SIZE;
                frame.setSize(screenWidth, screenHeight);
            }
        }
        public void componentHidden(ComponentEvent arg0) {}
        public void componentMoved(ComponentEvent arg0) {}
        public void componentShown(ComponentEvent arg0) {}
    });
}
}

框架尺寸包括插图(边框尺寸)。面板大小不正确

您应该使用JFrame
getWidth()
getHeight()
frame.getInsets()
并将它们考虑到最终的大小

这是一个针对JPanel的版本(因为这似乎是您感兴趣的大小),并在考虑插图的同时适当地调整JFrame的大小

public void componentResized(ComponentEvent e) {
    if ((panel.getWidth() % SIZE != 0) || (panel.getHeight() % SIZE != 0)) {
        int panelWidth = ((panel.getWidth() + SIZE) / SIZE) * SIZE;
        int panelHeight = ((panel.getHeight() + SIZE) / SIZE) * SIZE;
        Insets i = frame.getInsets();
        frame.setSize(screenWidth + i.left + i.right, screenHeight + i.top + i.bottom);
    }
}

您的
componentResistized()
方法基于
面板检查值,但随后设置
帧的大小。尝试将方法中对
面板
的引用更改为
框架

public void componentResized(ComponentEvent e) {
  if ((frame.getWidth() % SIZE != 0) || (frame.getHeight() % SIZE != 0)) {
    int screenWidth = ((frame.getWidth() + SIZE) / SIZE) * SIZE;
    int screenHeight = ((frame.getHeight() + SIZE) / SIZE) * SIZE;
    frame.setSize(screenWidth, screenHeight);
  }
}