English 中文(简体)
JDialog 固定高度[复制]
原标题:JDialog fixed height [duplicate]
This question already has an answer here:
Closed 11 years ago.

Possible Duplicate:
JDialog allow user to only change width of the dialog

我有一位犹太人民主党,我想说的是,血汗是可憎的,但不是高潮。

这是我的现行法典:

addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        setLocation(getLocation().x, getLocation().y + (getHeight() - staticHeight));
        setSize(new Dimension(getWidth(), staticHeight));
        super.componentResized(e);
    }
});

问题在于,在窗户重新缩编后,该代码便被打上。 使窗户变现,然后回落。

当用户试图冲破窗户的高度时,我就这样做了。

问题回答

looks like you cant. there s a setResizable() method but its all-or-nothing.
you could try mitigating this by using a layout inside your JDialog so that your content remains the same height regardless of the JDialog height
or perhaps (a bit more radical) set it as not resizable and implement your own mouse listener for resizing yourself ? that way you could have full control

在等待一段时间后,我找不到任何真正令人满意的解决办法。 我认为,转解辩是直接在本组织一级处理的问题,因此,你只能说,你想要它完全不可行或完全不可行。

The code below will always prevent the dialog from being bigger, but as the user resize the dialog, the borders of the dialog still move.

rad也提出的另一个选择是,防止转基因,并设置一个含有摩擦听众的陈规律,以便倾听摩擦,并相应地进行消化。 然而,我认为这不会对使用者感到非常本土(我不认为你能够追捕迪亚洛边界上的事件)。

import java.awt.Frame;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                init();
            }
        });
    }

    public static void init() {
        final int staticHeight = 150;
        final JDialog dialog = new JDialog((Frame) null) {

            @Override
            protected JRootPane createRootPane() {
                JRootPane rp = new JRootPane() {
                    @Override
                    public void reshape(int x, int y, int w, int h) {
                        super.reshape(x, y, w, staticHeight);
                    }
                };
                rp.setOpaque(true);
                return rp;
            }

            @Override
            public void reshape(int x, int y, int width, int height) {
                super.reshape(x, y, width, staticHeight);
            }

        };
        dialog.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                dialog.setSize(dialog.getWidth(), staticHeight);
            }
        });
        dialog.pack();
        dialog.setVisible(true);

    }
}

Here try this code example, that s how you set the JDialog to not have vertical Resizing. Though I really doesn t want to take credit for this, the whole credit goes to the creator of this program which is "Darryl Burke" the link to the program is here.

import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  

public class WidthResizeableDialog {  

  Robot robot;  
  static final int HEIGHT = 400;  
  Point lastLocation;  

  public static void main(String[] args) {  
    SwingUtilities.invokeLater(new Runnable() {  

      @Override  
      public void run() {  
        new WidthResizeableDialog().makeUI();  
      }  
    });  
  }  

  public void makeUI() {  
    try {  
      robot = new Robot();  
    } catch (AWTException ex) {  
      ex.printStackTrace();  
    }  
    final JDialog dialog = new JDialog();  
    dialog.setSize(400, HEIGHT);  
    dialog.setLocationRelativeTo(null);  
    dialog.addWindowListener(new WindowAdapter() {  

      @Override  
      public void windowClosing(WindowEvent e) {  
        System.exit(0);  
      }  
    });  
    dialog.addComponentListener(new ComponentAdapter() {  

      @Override  
      public void componentMoved(ComponentEvent e) {  
        SwingUtilities.invokeLater(new Runnable() {  

          @Override  
          public void run() {  
            lastLocation = dialog.getLocation();  
          }  
        });  
      }  
    });  
    dialog.getRootPane().addComponentListener(new ComponentAdapter() {  

      @Override  
      public void componentResized(ComponentEvent e) {  
        int height = dialog.getHeight();  
        if (robot != null && height != HEIGHT) {  
          robot.mouseRelease(InputEvent.BUTTON1_MASK);  
          dialog.setLocation(lastLocation);  
          dialog.setSize(dialog.getWidth(), HEIGHT);  
        }  
      }  
    });  
    dialog.setVisible(true);  
  }  
} 




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签