javax.swing.JButton#hasFocus ( )源码实例Demo

下面列出了javax.swing.JButton#hasFocus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
源代码3 项目: jdk8u60   文件: ResetMostRecentFocusOwnerTest.java
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
源代码7 项目: jdk8u-jdk   文件: ResetMostRecentFocusOwnerTest.java
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
源代码8 项目: hottub   文件: ResetMostRecentFocusOwnerTest.java
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
源代码10 项目: openjdk-8   文件: ResetMostRecentFocusOwnerTest.java
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
源代码11 项目: jdk8u_jdk   文件: ResetMostRecentFocusOwnerTest.java
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
源代码12 项目: jdk8u-jdk   文件: ResetMostRecentFocusOwnerTest.java
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}
 
@Override
public void start() {

    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
            System.err.println(e);
        }
    }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);

    boolean gained = false;
    final Robot robot = Util.createRobot();

    JFrame frame1 = new JFrame("Main Frame");
    final JButton b1 = new JButton("button1");
    frame1.add(b1);
    frame1.pack();
    frame1.setLocation(0, 300);

    Util.showWindowWait(frame1);

    final JFrame frame2 = new JFrame("Test Frame");
    final JButton b2 = new JButton("button2");
    frame2.add(b2);
    frame2.pack();
    frame2.setLocation(300, 300);

    b2.setEnabled(false);
    b2.requestFocus();

    Util.showWindowWait(frame2);

    robot.delay(500);

    //
    // It's expeced that the focus is restored to <button1>.
    // If not, click <button1> to set focus on it.
    //
    if (!b1.hasFocus()) {
        gained = Util.trackFocusGained(b1, new Runnable() {
            public void run() {
                Util.clickOnComp(b1, robot);
            }
        }, 5000, false);

        if (!gained) {
            throw new RuntimeException("Unexpected state: focus is not on <button1>");
        }
    }

    robot.delay(500);

    //
    // Click <button2>, check that focus is set on the parent frame.
    //
    gained = false;
    gained = Util.trackFocusGained(frame2, new Runnable() {
        public void run() {
            Util.clickOnComp(b2, robot);
        }
    }, 5000, false);

    if (!gained) {
        throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
    }

    System.out.println("Test passed.");
}