Java Swing内部框架作为对话框 [英] Java Swing Internal Frame As Dialog

查看:142
本文介绍了Java Swing内部框架作为对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在netbeans中创建了一个项目。我有一个内部框架,我想要显示为对话框。请帮帮我。
注意:我已经使用窗口的外观和感觉。

解决方案

不要使用 java.awt.Dialog javax.swing.JDialog 。相反,请查看 JOptionPane 方法以' showInternal .. '开头。 EG



  import java.awt。*; 
import java.awt.event。*;
import javax.swing。*;
import javax.swing.border.EmptyBorder;

public class InternalDialog {

public static void main(String [] args)throws异常{
Runnable r = new Runnable(){

@Override
public void run(){
//用户看到的GUI(无框架)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(2,3,2,3));

JDesktopPane dtp = new JDesktopPane();
gui.add(dtp);

ActionListener listener = new ActionListener(){

@Override
public void actionPerformed(ActionEvent e){
组件c =(组件)的getSource();
JOptionPane.showInternalMessageDialog(c,Message);
}
};
for(int ii = 0; ii< 3; ii ++){
JInternalFrame jif = new JInternalFrame();
dtp.add(jif);
jif.setLocation(new Point(ii * 30,ii * 20));
jif.setSize(200,50);
jif.setVisible(true);

JButton b =新的JButton(点击我!);
b.addActionListener(listener);
jif.add(b);
}

// TODO!
gui.setPreferredSize(new Dimension(280,150));
gui.setBackground(Color.WHITE);

JFrame f = new JFrame(Demo);
f.add(gui);
//确保关闭框架后JVM关闭,
//所有非守护程序线程完成
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//见http://stackoverflow.com/a/7143398/418556演示。
f.setLocationByPlatform(true);

//确保框架是最小尺寸,它需要为
//,以便在其中显示组件
f.pack();
//应该最后完成,以避免闪烁,移动,
//调整大小的工件。
f.setVisible(true);
}
};
//应该在EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater上创建和更新Swing GUI (R);
}
}


I have created one project in netbeans. I have one internal frame, which I want to be displayed as dialog. Please help me. Note:I have used windows look and feel.

解决方案

Don't use a java.awt.Dialog or javax.swing.JDialog. Instead look to the JOptionPane methods that start with 'showInternal..'. E.G.

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

public class InternalDialog {

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setBorder(new EmptyBorder(2, 3, 2, 3));

                JDesktopPane dtp = new JDesktopPane();
                gui.add(dtp);

                ActionListener listener = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Component c= (Component)e.getSource();
                        JOptionPane.showInternalMessageDialog(c, "Message");
                    }
                };
                for (int ii=0; ii<3; ii++) {
                    JInternalFrame jif = new JInternalFrame();
                    dtp.add(jif);
                    jif.setLocation(new Point(ii*30, ii*20));
                    jif.setSize(200,50);
                    jif.setVisible(true);

                    JButton b = new JButton("Click me!");
                    b.addActionListener(listener);
                    jif.add(b);
                }

                // TODO!
                gui.setPreferredSize(new Dimension(280, 150));
                gui.setBackground(Color.WHITE);

                JFrame f = new JFrame("Demo");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

这篇关于Java Swing内部框架作为对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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