私は一定の固定値に基づいて幅と高さにリサイズするJFrameを作成しようとしています。JFrameのサイズ変更の質問
マウスを使用して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) {}
});
}
}