在一个维度中调整JFrame的大小 [英] Resizing JFrame in one dimension

查看:132
本文介绍了在一个维度中调整JFrame的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅在一个维度(在这种情况下是宽度)重新调整我的JFrame的大小,我发现了这个问题的其他答案。我这次尝试的问题是找不到符号问题。
我有代码

 公共类NumberConverter 
{
...
static
{
if(System.getProperty(sun.arch.data.model)。equals(32))
{// 32位JVM
的System.loadLibrary( my32bitdll);
System.out.println(运行32位JVM);

}

else
{
// 64位JVM
System.loadLibrary(my64bitdll);
System.out.println(运行64位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();
}
}

当我编译时,我得到错误找不到符号 - 方法setMinMaxResizeBoundaries(int,int,int,int,int)找不到符号 - getComponentHWND(numberConversionWindow)。我非常感谢有人向我解释我打算如何使用 setMinMaxResizeBoundaries & getComponentHWND 正确,以及我打算如何在我的代码中输入它。如我所知,我打算使用 public static native int 或者我打算把它放在void numberConvertGUI()



关于 JFrame的原始答案只有大小的高度

  static {
if(System.getProperty(sun.arch.data) .model)。equals(32))
{// 32位JVM
System.loadLibrary(my32bitdll);
System.out.println(运行32位JVM);

} else {
// 64位JVM
System.loadLibrary(my64bitdll);
System.out.println(运行64位JVM);
}
}
//设置一个永远不会调整大小超过或低于这些最小宽度/高度的窗口
public static native int setMinMaxResizeBoundaries(int hwnd,int minWidth,int minHeight, int maxWidth,int maxHeight);

额外的代码

  //返回指定组件的HWND,如果不存在则返回-1 
public static native int getComponentHWND(Component c);


解决方案

简短的回答是你不能。在Swing中,所有事件都转到Java代码,然后更新屏幕图形...除了调整窗口大小。窗口本身是本机控件。事件首先进入窗口,然后进入Java端。您的原始代码在Java端工作,因此在您调整大小时窗口已经调整大小。这就是造成这种毛刺行为的原因。



解决这个问题的唯一方法是(无需深入研究C ++本机代码)是禁用本机窗口装饰并渲染自己的调整大小句柄。然后您的代码会在原生窗口之前收到调整大小事件,并且毛刺就会消失。这不是一项微不足道的工作,但根据您的使用情况,这可能是可行的。


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

解决方案

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.

这篇关于在一个维度中调整JFrame的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆