开发者

JFrame resize question

开发者 https://www.devze.com 2023-03-24 17:19 出处:网络
So I am trying to create a JFrame that will resize to a width and height based off a certain fixed value.

So I am trying to create a JFrame that will resize to a width and height based off a certain fixed value.

i.e. when you use the mouse to change the size of the JFrame, if the new width or height value is not a multiple of 20 (the fixed size in the below code), it will round the width or height to the nearest 20, then resize the JFrame.

The width appears to work but the height doesn't...

please help

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 ((pan开发者_如何学Pythonel.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) {}
    });
}
}


Your componentResized() method checks values based on the panel but then you set the size of the frame. Try changing the references to panel to frame in your method:

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


The frame size includes the insets (the size of the borders). The panel size does not.

You should either use the JFrame getWidth() and getHeight() or frame.getInsets() and factor those in to your eventual sizes.

Here's a version that works against the JPanel (as that seems to be the size you are interested in) and appropriately sizes the JFrame whilst taking the insets into account.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号