Resizing JFrame in one dimension

这一生的挚爱 提交于 2019-12-29 08:54:29

问题


I am trying to re-size my JFrame in only one dimension (width in this case) and I found this question JFrame re-sizable height ONLY which gave me a good answer for doing so;

addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
    setSize(new Dimension(preferredWidth, getHeight()));
    super.componentResized(e);
}
}); 

and I edited it slightly so that instead of locking width it locked height to a certain size and allowed width to be re-sizable.

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 often glitches. As I start to re-size it to make it wider there is a black line which flickers just before the frame re-sizes.

The next glitches are caused when re-sizing the height. It may just leave a large black area instead of snapping back to 300, and sometimes it will not snap back at all.

So my question is how can I improve this code to prevent these glitches from happening and instead of just having a height which it will snap back to can I disable the ability to re-size the height? If I can disable this ability, how would I do so?

Edit

I have also tried the following code

numberConversionWindow.setMinimumSize(new Dimension(420, 300));
numberConversionWindow.setMaximumSize(new Dimension(numberConversionWindow.getWidth(), 300));

However this still lets me re-size the height of the JFrame.

Any Help would be greatly appreciated

Edit 2

I have attempted to try and use another answer from JFrame re-sizable height ONLY. My problem for this attempt is a can't find symbol question. I have the code

public class NumberConverter
{
...
static 
{
    if (System.getProperty("sun.arch.data.model").equals("32"))
    {   // 32-bit JVM
        System.loadLibrary("my32bitdll");
        System.out.println("Running 32-bit JVM");

    }

    else 
    {
        // 64-bit JVM
        System.loadLibrary("my64bitdll");
        System.out.println("Running 64-bit JVM");
    }
}
//public static native int getComponentHWND(numberConversionWindow);
//public static native int setMinMaxResizeBoundaries(getComponentHWND, 420, 300, numberConversionWindow.getWidth(), 300);

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

When I compile I get the errors cannot find symbol - method setMinMaxResizeBoundaries(int,int,int,int,int) and cannot find symbol - getComponentHWND(numberConversionWindow). I would greatly appreciate someone explaining to me how I am meant to use setMinMaxResizeBoundaries & getComponentHWND properly, and how I am meant to input it in my code. As in wether I am meant to use the public static native int or I am meant to put it in the void numberConvertGUI()

The original answer on JFrame re-sizable height ONLY is

static {
if (System.getProperty("sun.arch.data.model").equals("32"))
{   // 32-bit JVM
    System.loadLibrary("my32bitdll");
    System.out.println("Running 32-bit JVM");

} else {
    // 64-bit JVM
    System.loadLibrary("my64bitdll");
    System.out.println("Running 64-bit JVM");
}
}
// Sets a window to never be resized above or below these minimum widths/heights
public static native int setMinMaxResizeBoundaries(int hwnd, int minWidth, int minHeight, int maxWidth, int maxHeight);

Extra bit of code

// Returns the HWND for the specified component, or -1 if does not exist
public static native int getComponentHWND(Component c);

回答1:


The short answer is you can't. In Swing all events go to your Java code, then update the on screen graphics... except for resizing windows. The window itself is a native control. Events go to the window first, then to the Java side. Your original code works on the Java side so the window has already resized by the time you size it back. That's what causes the glitchy behavior.

The only way around this (short of digging into C++ native code) is to disable native window decorations and render your own resize handles. Then your code would receive the resize events before the native window does and the glitches would go away. Not a trivial amount of work, but it might be feasible depending on your use case.



来源:https://stackoverflow.com/questions/30133683/resizing-jframe-in-one-dimension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!