我的自定义JDialog显示为最小化,JScrollPane的滚动消失 [英] My custom JDialog appears minimized and JScrollPane's scroll disappears

查看:413
本文介绍了我的自定义JDialog显示为最小化,JScrollPane的滚动消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个基本的swing程序,在通过 JFileChooser JDialog 框中显示图像C>。我在对话框中使用了 JScrollPane ,并在窗格内使用 JLabel 将图像添加为图标。

I have created a basic swing program which displays an image in a JDialog box after it has been selected through a JFileChooser. I have used a JScrollPane inside the dialog box and a JLabel on which image is added as an icon, inside the pane.

以下是我用来构建对话框及其内容的代码,这些对象已经通过简单的新调用初始化:

The following is the code I have used to construct the dialog box and its contents, the objects are already initialized with a simple new call:

    jDialog1.setTitle("Image");
    jDialog1.setModal(true);

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jScrollPane2.setViewportView(jLabel1);

Netbeans已用于构建GUI。

Netbeans has been used to build the GUI.

以下是我用来加载和显示图片的代码:

The following is the code I have used to load and display the image:

    int rval = jFileChooser1.showDialog(this, "Show");
    File f = jFileChooser1.getSelectedFile();
    jDialog1.setTitle(jDialog1.getTitle() + " : " + f.getName());
    try {
        BufferedImage bf = ImageIO.read(f);
        jLabel1.setIcon(new ImageIcon(bf));
    }
    catch (IOException ioe) {
    }
    jDialog1.setLocationRelativeTo(null);
    jDialog1.setVisible(true);
    jDialog1.pack();

虽然图像已加载并显示,但问题是

Though the image is loaded and displayed, the trouble is


  • 我的对话框在左上角以最小化的形式出现,我必须拖动它的角来查看图像。包括相同的图像。

  • My dialog box appears in a minimized form in the upper left hand corner and i have to drag its corner to view the image. Including an image of the same.

滚动窗格的水平滚动在对话框完全展开并且图像完全显示之前很久就会消失。虽然垂直滚动条动作很好。

The horizontal scroll of the scroll pane disappears long before the dialog box is fully extended and the image is fully displayed. Though the vertical scroll bar acts fine.

我尝试设置 JDialog 和 JLabel 但问题仍然存在。

I have tried setting size and preferred size of both JDialog and JLabel but the problems still persist.

提前完成Thanx!

注意:使用 pack()方法解决水平滚动条问题在对话框上,但最小化的对话框仍然存在。此外,对话框现在也出现在屏幕的中央。

Note: The horizontal scroll bar problem is solved by using the pack() method on the dialog box but the minimized dialog box is still there. Also, the dialog box now appears in the center of the screen as well.

推荐答案

方法调用的顺序对于模态对话框很重要,你必须在使对话框可见之前调用包(因为否则在关闭对话框之前不会执行包):

Sequence of method calls matters for modal dialogs, you must call the pack before making the dialog visible (because otherwise the pack wouldn't be executed before the dialog is closed):

dialog.pack();
// Edit: manually double its size:
dialog.setSize(dialog.getPreferredSize().width * 2, dialog.getPreferredSize().height * 2);
dialog.setVisible(true);

这篇关于我的自定义JDialog显示为最小化,JScrollPane的滚动消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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