rt

解决方案 »

  1.   

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;public class Test extends JFrame {

    private JButton button = null; public Test() {
    button = new JButton("打开新窗口");
    button.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    openWindow();
    }});
    //this.getContentPane().add(null, BorderLayout.NORTH);
    this.getContentPane().add(button);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 200);
    this.setVisible(true);
    }

    public void openWindow(){
    JDialog dia = new JDialog(this, true);
    dia.setSize(100, 80);
    dia.setVisible(true);
    } public static void main(String[] args) {
    new Test();
    }
    }
      

  2.   

    谢谢,不过如果不是Dialog窗体怎么办?还有requestFocus()怎么用?
      

  3.   

    能不能用setAlwaysOnTop(true);来实现?
      

  4.   

    给你一个例子,A,B两个画面,其中A画面点击按钮打开B画面。
    功能其实都在A里面实现,B就是一个页面,无任何功能。A代码:
    package com.test;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;import javax.swing.JButton;
    import javax.swing.JFrame;public class A extends JFrame implements ActionListener, WindowListener { private javax.swing.JPanel jContentPane = null; private JButton jButton = null;
    /**
     * This is the default constructor
     */
    public A() {
    super();
    initialize();
    }
    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
    this.setSize(300,200);
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
    }
    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private javax.swing.JPanel getJContentPane() {
    if(jContentPane == null) {
    jContentPane = new javax.swing.JPanel();
    jContentPane.setLayout(null);
    jContentPane.add(getJButton(), null);
    }
    return jContentPane;
    }

    /**
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     */    
    private JButton getJButton() {
    if (jButton == null) {
    jButton = new JButton();
    jButton.setBounds(40, 48, 72, 21);
    jButton.setText("alert");
    jButton.addActionListener(this);
    }
    return jButton;
    }
        /* (non-Javadoc)
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
         */
        public void actionPerformed(ActionEvent e) {
            if(e.getSource() == getJButton()){
                B testB = new B();
                testB.addWindowListener(this);
                testB.show();
                setEnabled(false);
            }
        }
        /* (non-Javadoc)
         * @see java.awt.event.WindowListener#windowActivated(java.awt.event.WindowEvent)
         */
        public void windowActivated(WindowEvent e) {
            // TODO Auto-generated method stub
            
        }
        /* (non-Javadoc)
         * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
         */
        public void windowClosed(WindowEvent e) {
            setEnabled(true);
            toFront();
            
        }
        /* (non-Javadoc)
         * @see java.awt.event.WindowListener#windowClosing(java.awt.event.WindowEvent)
         */
        public void windowClosing(WindowEvent e) {
            // TODO Auto-generated method stub
            
        }
        /* (non-Javadoc)
         * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
         */
        public void windowDeactivated(WindowEvent e) {
            // TODO Auto-generated method stub
            
        }
        /* (non-Javadoc)
         * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
         */
        public void windowDeiconified(WindowEvent e) {
            // TODO Auto-generated method stub
            
        }
        /* (non-Javadoc)
         * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
         */
        public void windowIconified(WindowEvent e) {
            // TODO Auto-generated method stub
            
        }
        /* (non-Javadoc)
         * @see java.awt.event.WindowListener#windowOpened(java.awt.event.WindowEvent)
         */
        public void windowOpened(WindowEvent e) {
            // TODO Auto-generated method stub
            
        }
        
        public static void main(String args[]){
            A a = new A();
            a.show();
        }
     }=================================
    B代码:
    package com.test;import javax.swing.JFrame;
     
    import javax.swing.JTextField;public class B extends JFrame { private javax.swing.JPanel jContentPane = null; private JTextField jTextField = null;
    /**
     * This is the default constructor
     */
    public B() {
    super();
    initialize();
    }
    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
    this.setSize(300,200);
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
    }
    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private javax.swing.JPanel getJContentPane() {
    if(jContentPane == null) {
    jContentPane = new javax.swing.JPanel();
    jContentPane.setLayout(null);
    jContentPane.add(getJTextField(), null);
    }
    return jContentPane;
    }
    /**
     * This method initializes jTextField
     * 
     * @return javax.swing.JTextField
     */    
    private JTextField getJTextField() {
    if (jTextField == null) {
    jTextField = new JTextField();
    jTextField.setBounds(58, 47, 145, 29);
    jTextField.setText("This is new Frame");
    }
    return jTextField;
    }
     }
      

  5.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;public class TestFocusRecover extends AbstractAction {
        JFrame fr;
        TestFocusRecover(JFrame fr, String s) {
            super(s);
            this.fr = fr;
        }    public void actionPerformed(ActionEvent e) {
            // 此处隐藏也可以
            // fr.setVisible(false);        SubWindow sub = new SubWindow(fr);
            sub.setPreferredSize(new Dimension(300, 200));        sub.pack();
            sub.setVisible(true);
        }    public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Container container = frame.getContentPane();        JButton b = new JButton();
            b.setAction(
                new TestFocusRecover(frame, "Open"));        container.add(b);        frame.pack();
            frame.setVisible(true);
        }    class SubWindow extends JFrame{    
            public SubWindow(final JFrame fr) {
                // 有无皆可
                // setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);            addWindowListener(
                    new WindowAdapter(){
                        // 关键是这里。
                        public void windowClosing(WindowEvent e) {
                            fr.setVisible(true);
                        }
                });
            }
        }
    }