I am trying to resize my JFrame in only one dimension (width in this case), and I found this question only for JFrame heights), which gave me a good answer for this;
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
setSize(new Dimension(preferredWidth, getHeight()));
super.componentResized(e);
}
});
and I edited it a bit so that instead of the lock width, the height was fixed to a certain size and the width allowed.
import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Component;
import javax.swing.*;
import java.io.*;
import java.lang.*;
public class mcve
{
JFrame numberConversionWindow = new JFrame("Number Conversion");
public void numberConvertGUI()
{
numberConversionWindow.setBounds(10, 10, 420, 300);
numberConversionWindow.addComponentListener(new ComponentAdapter()
{
@Override
public void componentResized(ComponentEvent e)
{
numberConversionWindow.setSize(new Dimension(numberConversionWindow.getWidth(), 300));
super.componentResized(e);
numberConversionWindow.repaint();
}
});
numberConversionWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
numberConversionWindow.setLayout(new GridLayout(1,1));
numberConversionWindow.setVisible(true);
}
public static void main(String[] args)
{
mcve mc = new mcve();
mc.numberConvertGUI();
}
}
However, there is a problem with this code. It is often buggy. When I start resizing to make it wider, there is a black line that flickers before resizing the frame.
The following glitches occur when recalibrating the height. It can simply leave a large black area instead of snapping back to 300, and sometimes it doesn’t lean back at all.


, , , , , , , ? , ?
Edit
numberConversionWindow.setMinimumSize(new Dimension(420, 300));
numberConversionWindow.setMaximumSize(new Dimension(numberConversionWindow.getWidth(), 300));
JFrame.
2
JFrame ). .
public class NumberConverter
{
...
static
{
if (System.getProperty("sun.arch.data.model").equals("32"))
{
System.loadLibrary("my32bitdll");
System.out.println("Running 32-bit JVM");
}
else
{
System.loadLibrary("my64bitdll");
System.out.println("Running 64-bit JVM");
}
}
public void numberConvertGUI()
{
numberConversionWindow.setBounds(10, 10, 420, 300);
int hwndForJFrame = getComponentHWND(numberConversionWindow);
numberConversionWindow.setMinMaxResizeBoundaries(hwndForJFrame, 420, 300, numberConversionWindow.getWidth(), 300);
...
}
public static void main(String[] args)
{
NumberConverter nC = new NumberConverter();
nC.numberConvertGUI();
}
}
cannot find symbol - method setMinMaxResizeBoundaries(int,int,int,int,int) cannot find symbol - getComponentHWND(numberConversionWindow). , - , setMinMaxResizeBoundaries getComponentHWND, . , public static native int, numberConvertGUI()
JFrame =
static {
if (System.getProperty("sun.arch.data.model").equals("32"))
{
System.loadLibrary("my32bitdll");
System.out.println("Running 32-bit JVM");
} else {
System.loadLibrary("my64bitdll");
System.out.println("Running 64-bit JVM");
}
}
public static native int setMinMaxResizeBoundaries(int hwnd, int minWidth, int minHeight, int maxWidth, int maxHeight);
public static native int getComponentHWND(Component c);