JDialog弹出窗口太小 [英] JDialog popup too small

查看:126
本文介绍了JDialog弹出窗口太小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次运行这段代码时,输​​出都是一个对话框,它出现在屏幕的左上角,而标题不会显示.

Every time I run this piece of code, the output is a dialog box that appears at the top left hand corner of the screen and the title does not show.

有什么方法可以更改此设置,以使对话框出现在中间并且大小可以接受吗?

Is there any way that I can change this so that the dialog appears at the middle and of acceptable size?

代码:

public class Test {

public static void main(String[] args) {

    JFrame f = new JFrame();

    final JDialog dialog = new JDialog(f, "Hello world", true);
    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    timer.setRepeats(false);
    timer.start();

    dialog.setVisible(true); 
    System.out.println("Dialog closed");

}
}

推荐答案

有什么方法可以更改此设置,以使对话框出现在中间并且大小可以接受吗?"

"Is there any way that I can change this so that the dialog appears at the middle and of acceptable size?"

如果仅向其中添加组件,打包并设置其相对于null的位置,则应该可以正常工作

If you just add components to it, pack it, and set it location relative to null, it should work fine

相对于.pack(),而不是设置大小,它更可取.为了使包装正常工作,您需要实际添加组件. .pack()将完全按照其名称建议进行操作-按照所有添加的组件的首选大小包装框架.

It is preferred to .pack() instead of setting a size. For pack to work, you need to actually add components. the .pack() will do exactly as it's name suggest - pack the frame with respecting all the added components' preferred sizes.

也可以使用setLocationRelativeTo()设置相对于组件的对话框位置.如果使用null,它将始终位于屏幕中央.但是,如果您设置相对于其父框架(框架)的位置,它将在框架上方居中显示.

Also with setLocationRelativeTo() you set the dialog loation relative to a component. If you use null, it will be centered on the screen always. But if you set the location relative to its parent, the frame, it will appear centered over the frame.

我完全不知道您要使用计时器实现什么,所以我只想无评论

I have absolutely no idea what you're trying to achieve with the timer, so I just prefer to no-comment

查看示例

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.Timer;

public class Test {

    public static void main(String[] args) {

        JFrame f = new JFrame();
        final MyDialog dialog = new MyDialog(f, "Title", true);
        Timer timer = new Timer(10000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        });
        timer.setRepeats(false);
        timer.start();

        dialog.setVisible(true);
        System.out.println("Dialog closed");

    }

    private static class MyDialog extends JDialog {

        public MyDialog(JFrame frame, String title, boolean modal) {
            super(frame, title, modal);

            setLayout(new BorderLayout());
            add(new JButton("NORTH"), BorderLayout.NORTH);
            add(new JButton("SOUTH"), BorderLayout.SOUTH);
            add(new JButton("EAST"), BorderLayout.EAST);
            add(new JButton("WEST"), BorderLayout.WEST);
            add(new JButton("CENTER"), BorderLayout.CENTER);

            pack();
            setLocationRelativeTo(null);

        }
    }
}


作为旁注,您应该像这样从事件调度线程(EDT)运行Swing应用


As a side note, you should be running Swing apps from the Event Dispatch Thread (EDT) like this

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable(){
        public void run() {
           //  the code from your main method here
        }
    });
}

这篇关于JDialog弹出窗口太小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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