JavaSwing Dialog

try
        {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
            //UIManager.put("RootPane.setupButtonVisible", false);
            org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            
        }
        catch(Exception e)
        {
            //TODO exception
        }
        
        // 创建窗体对象
        JFrame jFrame =new JFrame();
        // 设置窗体大小
        jFrame.setSize(800, 500);
        // 设置窗体全屏展示
        //jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        // 设置窗体显示位置
        //jFrame.setLocation(100,200);
        // 设置窗体显示正中间
        jFrame.setLocationRelativeTo(null);
        // 设置窗体标题
        jFrame.setTitle("窗体标题");
        // 设置窗体不可全屏显示
        //jFrame.setResizable(false);
        // 设置窗体关闭后退出程序
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置此窗口是否应该始终位于其他窗口上方
        jFrame.setAlwaysOnTop(true);
        // 设置窗体图标
        jFrame.setIconImage(new ImageIcon(HelloWorld.class.getResource("/images/book.png")).getImage());
        

        
        JPanel panel =new JPanel(null);
        
        JButton btn01 =new JButton("消息对话框");
        btn01.setLocation(10, 10);
        btn01.setSize(100, 40);
        
        btn01.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // 参数1: 窗体,Dialog不能单独显示,只能依附在窗口上
                // 参数2: 消息内容
                // 参数3: 消息标题
                // 参数4: 自定义图标
                JOptionPane.showMessageDialog(
                        jFrame, 
                        "你的会员3天后到期",
                        "会员到期提示",
                        JOptionPane.WARNING_MESSAGE,
                        new ImageIcon(HelloWorld.class.getResource("/images/book.png"))
                );
                
            }
        });
        
        
        JButton btn02 =new JButton("确认对话框");
        btn02.setLocation(10, 60);
        btn02.setSize(100, 40);
        
        btn02.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // 参数1: 窗体,Dialog不能单独显示,只能依附在窗口上
                // 参数2: 消息内容
                // 参数3: 消息标题
                // 参数4: 确认对话框操作按钮
                int value=JOptionPane.showConfirmDialog(
                        jFrame, 
                        "确认删除?",
                        "删除提醒",
                        JOptionPane.OK_CANCEL_OPTION
                        
                );
                // 确认对话框的返回值
                System.out.println(value);
                
            }
        });
        
        JButton btn03 =new JButton("输入对话框");
            btn03.setLocation(10, 110);
            btn03.setSize(100, 40);
            
            btn03.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 参数1: 窗体,Dialog不能单独显示,只能依附在窗口上
                    // 参数2: 消息内容
                    String value=JOptionPane.showInputDialog(
                        jFrame, 
                        "请输入内容"
                    );
                    // 输入对话框的返回值
                    System.out.println(value);
                }
            });
            
            
            
            
            JButton btn04 =new JButton("内部消息对话框");
            btn04.setLocation(10, 160);
            btn04.setSize(100, 40);
            
            btn04.addActionListener(new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    // 参数1: 窗体,Dialog不能单独显示,只能依附在窗口上
                    // 参数2: 消息内容
                    JOptionPane.showInternalMessageDialog(
                            jFrame.getContentPane(),
                            "内部消息对话框"
                            
                    );
                }
            });
            
        
            

        JButton btn05 =new JButton("自定义对话框");
        btn05.setLocation(10, 210);
        btn05.setSize(100, 40);
            
        btn05.addActionListener(new ActionListener() {
                
            @Override
            public void actionPerformed(ActionEvent e) {
                // 实例化自定义对话框
                MyDialog myDialog=new MyDialog(jFrame, "自定义对话框");
                // 自定义方法返回自定义对话框的输入值
                String value=myDialog.getValue();
                System.out.println(value);
                    
            }
        });
            
            
        
        panel.add(btn01);
        panel.add(btn02);
        panel.add(btn03);
        panel.add(btn04);
        panel.add(btn05);
        
        jFrame.add(panel);
        // 设置窗体可见
        jFrame.setVisible(true);
            

 

public class MyDialog  extends JDialog{
    
    private String value;
    private JLabel label;
    private JTextField field;
    private JButton btn;
    private JButton btn2;
    
    public MyDialog(Frame owner,String title) {
        // owner: 调用者的JFrame对象
        // title: dialog标题
        super(owner,title);
        init();
    }
    
    public void init () {
        
        // 设置dialog窗体大小
        this.setSize(600, 300);
        // 设置dialog窗体显示正中间
        this.setLocationRelativeTo(null);
        // 设置为模态对话框
        this.setModal(true);
        
        
        
        
        Container container=   this.getContentPane();
        container.setLayout(null);
        
        label =new JLabel("用户名:");
        label.setLocation(10, 10);
        label.setSize(60,20);
        
        field =new JTextField();
        field.setLocation(70, 10);
        field.setSize(90,20);
        
        btn =new JButton("确定");
        btn.setLocation(10, 50);
        btn.setSize(90,20);
        
        btn2 =new JButton("取消");
        btn2.setLocation(10, 100);
        btn2.setSize(90,20);
        
        
        btn.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取输入框文本
                value =field.getText();
                // 关闭对话框
                MyDialog.this.dispose();
            }
        });
        
        btn2.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // 关闭对话框
                MyDialog.this.dispose();
            }
        });
        
        container.add(label);
        container.add(field);
        container.add(btn);
        container.add(btn2);
        
        // 设置dialog可见(必须在最后面显示,否则对话框的组件不显示)
        this.setVisible(true);
        
    }
    
    public String getValue() {
        return this.value;
    }

}

 

posted @ 2024-12-17 21:41  leungqingyun  阅读(14)  评论(0)    收藏  举报