[code]
private JFrame getJFrame() {
if (jFrame == null) {
Toolkit tk=Toolkit.getDefaultToolkit();
Image img=tk.getImage("PIC/1.jpg");
jFrame = new JFrame(); jFrame.setIconImage(img);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setJMenuBar(getJJMenuBar());
jFrame.setSize(300, 200);
//jFrame.setResizable(false);禁止标题最大化按钮
jFrame.setContentPane(getJContentPane());
jFrame.setTitle("Application");

}
return jFrame;
}
[/code]
这是JFRAME代码  我想捕获关闭按钮事件 然后弹出一个确认dialog....
[code]
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Test application = new Test();
//application.getJFrame().setVisible(true);
application.getJLabel().show();
application.getJFrame().show();
}
});
}
[/code]
这是MAIN函数  我想让程序显示一个Label 可不对  不知道怎么写这个main  求大虾帮忙!!!!!!!!!!!!!

解决方案 »

  1.   

    JLabel 作为一个组件需要放入一个容器,通过呈现这个容器来显示这个组件。
    下面的程序运行时呈现了一个JLabel以及点击关闭按钮弹出确认对话框...
    package mobi.chenwei.test;import javax.swing.JOptionPane;public class MainFrame extends javax.swing.JFrame {
        
        /** Creates new form MainFrame */
        public MainFrame() {
            initComponents();
        }
        
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {        jLabel1 = new javax.swing.JLabel();        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
            setTitle("www.chenwei.mobi");
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    formWindowClosing(evt);
                }
            });        jLabel1.setText("This is a label!");
            getContentPane().add(jLabel1, java.awt.BorderLayout.CENTER);        pack();
        }// </editor-fold>    private void formWindowClosing(java.awt.event.WindowEvent evt) {
            int option = JOptionPane.showConfirmDialog(this, "Do you want to close this window?", "www.chenwei.mobi", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(option == JOptionPane.YES_OPTION){
                this.dispose();
            }
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new MainFrame().setVisible(true);
                }
            });
        }
        
        // Variables declaration - do not modify
        private javax.swing.JLabel jLabel1;
        // End of variables declaration
        
    }
      

  2.   

    恩 明白了  其实我想用Label的目的 就是 不想要FRAME这个容器   不需要标题栏 最大化 最小化 关闭按钮(因为整个UI需要插个图)能实现这种效果么?麻烦了~
    [code]
    test
    [/code]
      

  3.   

    初始化JFrame时设置setUndecoratted(false);禁用或启用此窗体的装饰。只有在窗体不可显示时才调用此方法。
      

  4.   

    对不起,应该是setUndecoratted(true);
      

  5.   

    窗口吗? Window不是更好~~~~, 类似Splash的效果.