下面列出了怎么用javax.swing.JWindow的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testShowDropDownOnTextEntry() {
JWindow matchingWindow = textField.getActiveMatchingWindow();
// the window needs to be null initially because it must be properly parented when it is
// created
assertNull(
"The completion window has been created before any completion work " + "has began.",
matchingWindow);
// insert some text and make sure the window is created
typeText("d", true);
// get the contents of the window and make sure that they are updated
JList<String> jList = textField.getJList();
int size = jList.getModel().getSize();
// this will produce 'd2'
typeText("2", true);
assertNotEquals(
"The number of matching items in the list did not change after typing more " + "text.",
size, jList.getModel().getSize());
}
@Test
public void testDropdownLocationOnParentMove() {
// insert some text and make sure the window is created
typeText("d", true);
JWindow matchingWindow = textField.getActiveMatchingWindow();
Point location = runSwing(() -> matchingWindow.getLocationOnScreen());
Point frameLocation = parentFrame.getLocationOnScreen();
Point p = new Point(frameLocation.x + 100, frameLocation.y + 100);
runSwing(() -> parentFrame.setLocation(p));
waitForSwing();
JWindow currentMatchingWindow = textField.getActiveMatchingWindow();
Point newLocation = runSwing(() -> currentMatchingWindow.getLocationOnScreen());
assertNotEquals("The completion window's location did not update when its parent window " +
"was moved.", location, newLocation);
}
@Test
public void testSetText() {
setText("d");
JWindow matchingWindow = textField.getActiveMatchingWindow();
// make sure our set text call did not trigger the window to be created
assertNull(
"The completion window has been created before any completion work " + "has began.",
matchingWindow);
clearText();
typeText("d", true);
// one more time
clearText();
setText("c");
// make sure our set text call did not trigger the window to be created
matchingWindow = textField.getActiveMatchingWindow();
assertNull("The completion window has been created before any completion work has started",
matchingWindow);
}
@Test
public void testEscapeKey_MatchingWindowOpen() {
typeText("d", true);
String item = getSelectedListItem();
// hide the window without updating the contents of the text field
escape();
JWindow matchingWindow = textField.getActiveMatchingWindow();
assertTrue("The selection window is still showing after cancelling editing.",
!matchingWindow.isVisible());
assertNotEquals("Cancelling the completion window incorrectly updated the contents " +
"of the text field with the selected item in the list.", item, textField.getText());
assertNoEditingCancelledEvent();
}
@Test
public void testSelectionFromWindowByClicking() {
typeText("d", true);
JList<String> dataList = textField.getJList();
String selected = getSelectedListItem();
// fire a mouse click into the window
Point clickPoint = dataList.indexToLocation(dataList.getSelectedIndex());
clickMouse(dataList, MouseEvent.BUTTON1, clickPoint.x, clickPoint.y, 2, 0);
JWindow matchingWindow = textField.getActiveMatchingWindow();
assertNull("The selection window is still showing after cancelling editing.",
matchingWindow);
assertEquals("The text of the selected item was not placed in the selection field.",
selected, textField.getText());
}
public void testSetNodesSurvivesMultipleAdd_RemoveNotifyCalls() throws Exception {
final PropertySheet ps = new PropertySheet();
Node n = new AbstractNode( Children.LEAF );
JWindow window = new JWindow();
ps.setNodes( new Node[] {n} );
window.add( ps );
window.remove( ps );
window.add( ps );
window.remove( ps );
window.add( ps );
window.remove( ps );
window.setVisible(true);
assertNotNull(ps.helperNodes);
assertEquals("Helper nodes are still available even after several addNotify()/removeNotify() calls",
ps.helperNodes[0], n);
}
public static int getJWindowCount() {
return new QueueTool().invokeAndWait(new QueueTool.QueueAction<Integer>(null) {
@Override
public Integer launch() throws Exception {
Window[] windows = Window.getWindows();
int windowCount = 0;
for (Window w : windows) {
if (w.getClass().equals(JWindow.class)) {
windowCount++;
}
}
return windowCount;
}
});
}
public static JWindow getJWindow(int index) {
return new QueueTool().invokeAndWait(new QueueTool.QueueAction<JWindow>(null) {
@Override
public JWindow launch() throws Exception {
Window[] windows = Window.getWindows();
int windowIndex = 0;
for (Window w : windows) {
if (w.getClass().equals(JWindow.class)) {
if (windowIndex == index) {
return (JWindow) w;
}
windowIndex++;
}
}
return null;
}
});
}
/**
* Shows the notification
* @param window window to show
*/
protected static void showNotification(final JWindow window) {
try {
sLock.lock();
sWindows.addLast(window);
window.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
window.removeWindowListener(this);
sWindowOpen = false;
nextWindow();
}
});
nextWindow();
} finally {
sLock.unlock();
}
}
/**
* shows the next window on the stack
*/
private static void nextWindow() {
try {
sLock.lock();
if(!sWindowOpen && sWindows.size() > 0) {
sWindowOpen = true;
final JWindow window = sWindows.removeFirst();
Timer delayVisibleTimer = new Timer(DELAY, new ActionListener() {
public void actionPerformed(ActionEvent e) {
final Timer t = (Timer) e.getSource();
t.stop();
window.setVisible(true);
window.getGlassPane().setVisible(true);
}
});
delayVisibleTimer.start();
}
} finally {
sLock.unlock();
}
}
/**
* Create a transparent window
*/
protected JWindow createWindow() {
JWindow window = new JWindow();
window.getRootPane().putClientProperty(PROPERTY_IS_QPOPUP, true);
window.setType(Type.POPUP);
window.getRootPane().putClientProperty("Window.shadow", Boolean.FALSE);
float k = .95f;
window.getRootPane().putClientProperty("Window.opacity", k);
window.setOpacity(k);
window.setBackground(new Color(0, 0, 0, 0));
window.getRootPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
window.getRootPane().add(contents, c);
return window;
}
/**
* Frees any resources the <code>Popup</code> may be holding onto.
*/
protected void dispose()
{
Component component = getComponent();
Window window = SwingUtilities.getWindowAncestor(component);
if (component instanceof JWindow)
{
((Window) component).dispose();
component = null;
}
// If our parent is a DefaultFrame, we need to dispose it, too.
if (window instanceof DefaultFrame)
{
window.dispose();
}
}
public static void showSplash() {
screen = new JWindow();
final URL resource = MainFrame.class.getResource("mpcmaidlogo400_400.png");
final JLabel label = new JLabel(new ImageIcon(resource));
screen.getContentPane().add(label);
screen.setLocationRelativeTo(null);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension labelSize = screen.getPreferredSize();
screen
.setLocation(screenSize.width / 2 - (labelSize.width / 2), screenSize.height / 2
- (labelSize.height / 2));
screen.pack();
screen.setVisible(true);
label.repaint();
screen.repaint();
}
public MesquiteFileDialog (MesquiteWindow f, String message, int type) {
super(getFrame(f), message, type);
if (type == FileDialog.LOAD && (MesquiteTrunk.isMacOS() || MesquiteTrunk.isMacOSX()) && MesquiteTrunk.getOSXVersion()>10){
titleWindow = new JWindow();
titleWindow.setSize(twWidth,twHeight);
titleWindowLabel = new Label();
titleWindowLabel.setBackground(ColorDistribution.veryLightYellow); //ColorTheme.getExtInterfaceBackground()); //ColorDistribution.veryLightGray
titleWindow.add(titleWindowLabel);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int v, h;
h = (screenSize.width-twWidth)/2;
v = 26;
titleWindow.setLocation(h, v);
titleWindowLabel.setText(" " + message);
// Color darkBlue = new Color((float)0.0, (float)0.0, (float)0.7);
titleWindowLabel.setForeground(ColorDistribution.darkBlue); //ColorTheme.getExtInterfaceElement(true));
}
this.message = message;
this.type = type;
currentFileDialog = this;
//mfdThread = new MFDThread(this);
//mfdThread.start();
MainThread.incrementSuppressWaitWindow();
}
/**
* Frees any resources the <code>Popup</code> may be holding onto.
*/
protected void dispose()
{
Component component = getComponent();
Window window = SwingUtilities.getWindowAncestor(component);
if (component instanceof JWindow)
{
((Window) component).dispose();
component = null;
}
// If our parent is a DefaultFrame, we need to dispose it, too.
if (window instanceof DefaultFrame)
{
window.dispose();
}
}
public static void main(String[] args) throws Exception
{
Optional<JWindow> splash = LoadingSplashScreen
.setupScreen(WebAnno.class.getResource("splash.png"));
SpringApplicationBuilder builder = new SpringApplicationBuilder();
// Add the main application as the root Spring context
builder.sources(WebAnno.class).web(SERVLET);
// Signal that we may need the shutdown dialog
builder.properties("running.from.commandline=true");
init(builder);
builder.listeners(event -> {
if (event instanceof ApplicationReadyEvent
|| event instanceof ShutdownDialogAvailableEvent) {
splash.ifPresent(it -> it.dispose());
}
});
builder.run(args);
}
public static void main(String[] args) {
Robot r = Util.createRobot();
JWindow w = new JWindow();
w.setSize(100, 100);
w.setVisible(true);
Util.waitForIdle(r);
final JPopupMenu menu = new JPopupMenu();
JButton item = new JButton("A button in popup");
menu.add(item);
w.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
menu.show(me.getComponent(), me.getX(), me.getY());
System.out.println("Showing menu at " + menu.getLocationOnScreen() +
" isVisible: " + menu.isVisible() +
" isValid: " + menu.isValid());
}
});
Util.clickOnComp(w, r);
Util.waitForIdle(r);
if (!menu.isVisible()) {
throw new RuntimeException("menu was not shown");
}
menu.hide();
System.out.println("Test passed.");
}
@Test
public void testNavigationKeysTriggerCompletionWindowToShow() {
// insert some text and make sure the window is created
typeText("d", true);
// hide the window to test its triggering on up
hideWindowPressKeyThenValidate(KeyEvent.VK_UP);
// hide the window to test its triggering on down
hideWindowPressKeyThenValidate(KeyEvent.VK_DOWN);
// hide the window to test its triggering on keypad up
hideWindowPressKeyThenValidate(KeyEvent.VK_KP_UP);
// hide the window to test its triggering on keypad down
hideWindowPressKeyThenValidate(KeyEvent.VK_KP_DOWN);
// now with no text in the text field
clearText();
JWindow matchingWindow = textField.getActiveMatchingWindow();
assertTrue("The completion window is showing after a clearing the text field",
!matchingWindow.isShowing());
up();
assertTrue("The completion window is showing after pressing the up key in the text field " +
"while the text field is empty", !matchingWindow.isShowing());
down();
assertTrue(
"The completion window is showing after pressing the down key in the text field" +
"while the text field is empty",
!matchingWindow.isShowing());
}
@Test
public void testSetMatchingWindowHeight() {
int newSize = 200;
runSwing(() -> textField.setMatchingWindowHeight(newSize));
waitForSwing();
typeText("d", true);
JWindow matchingWindow = textField.getActiveMatchingWindow();
Dimension windowSize = matchingWindow.getSize();
assertEquals(newSize, windowSize.height);
}
@Test
public void testSetMatchingWindowHeight_MatchingWindowOpen() {
typeText("d", true);
int newSize = 200;
runSwing(() -> textField.setMatchingWindowHeight(newSize));
waitForSwing();
JWindow matchingWindow = textField.getActiveMatchingWindow();
Dimension windowSize = matchingWindow.getSize();
assertEquals(newSize, windowSize.height);
}
public static void main(String[] args) {
Robot r = Util.createRobot();
JWindow w = new JWindow();
w.setSize(100, 100);
w.setVisible(true);
Util.waitForIdle(r);
final JPopupMenu menu = new JPopupMenu();
JButton item = new JButton("A button in popup");
menu.add(item);
w.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
menu.show(me.getComponent(), me.getX(), me.getY());
System.out.println("Showing menu at " + menu.getLocationOnScreen() +
" isVisible: " + menu.isVisible() +
" isValid: " + menu.isValid());
}
});
Util.clickOnComp(w, r);
Util.waitForIdle(r);
if (!menu.isVisible()) {
throw new RuntimeException("menu was not shown");
}
menu.hide();
System.out.println("Test passed.");
}
public static void main(String[] args) {
Robot r = Util.createRobot();
JWindow w = new JWindow();
w.setSize(100, 100);
w.setVisible(true);
Util.waitForIdle(r);
final JPopupMenu menu = new JPopupMenu();
JButton item = new JButton("A button in popup");
menu.add(item);
w.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
menu.show(me.getComponent(), me.getX(), me.getY());
System.out.println("Showing menu at " + menu.getLocationOnScreen() +
" isVisible: " + menu.isVisible() +
" isValid: " + menu.isValid());
}
});
Util.clickOnComp(w, r);
Util.waitForIdle(r);
if (!menu.isVisible()) {
throw new RuntimeException("menu was not shown");
}
menu.hide();
System.out.println("Test passed.");
}
/**
* Fades the window and sets it visible
*
* @param window
*/
private static void fadein(JWindow window) {
final boolean supportsTranslucency = window.getGraphicsConfiguration().getDevice().isWindowTranslucencySupported( GraphicsDevice.WindowTranslucency.TRANSLUCENT );
AWTUtilities.setWindowOpacity(window, supportsTranslucency ? 0.3f : 1.0f);
AWTUtilities.setWindowOpacity(window, supportsTranslucency ? 0.5f : 1.0f);
AWTUtilities.setWindowOpacity(window, supportsTranslucency ? 0.9f : 1.0f);
window.setVisible(true);
}
public static void main(String[] args) {
Robot r = Util.createRobot();
JWindow w = new JWindow();
w.setSize(100, 100);
w.setVisible(true);
Util.waitForIdle(r);
final JPopupMenu menu = new JPopupMenu();
JButton item = new JButton("A button in popup");
menu.add(item);
w.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
menu.show(me.getComponent(), me.getX(), me.getY());
System.out.println("Showing menu at " + menu.getLocationOnScreen() +
" isVisible: " + menu.isVisible() +
" isValid: " + menu.isValid());
}
});
Util.clickOnComp(w, r);
Util.waitForIdle(r);
if (!menu.isVisible()) {
throw new RuntimeException("menu was not shown");
}
menu.hide();
System.out.println("Test passed.");
}
public void findContainerNP() {
List<List<String>> np = config.findContainerNP(Window.class);
AssertJUnit.assertEquals(1, np.size());
np = config.findContainerNP(JInternalFrame.class);
AssertJUnit.assertEquals(1, np.size());
np = config.findContainerNP(JWindow.class);
AssertJUnit.assertEquals(2, np.size());
}
public void findContainerNP() {
List<List<String>> np = config.findContainerNP(Window.class);
AssertJUnit.assertEquals(1, np.size());
np = config.findContainerNP(JInternalFrame.class);
AssertJUnit.assertEquals(1, np.size());
np = config.findContainerNP(JWindow.class);
AssertJUnit.assertEquals(2, np.size());
}
private static void safeSetBackground(JWindow window, Color background) {
GraphicsConfiguration gc = window.getGraphicsConfiguration();
if (!gc.isTranslucencyCapable()) return; // PERPIXEL_TRANSLUCENT not supported
if (gc.getDevice().getFullScreenWindow() == window) return; // fullscreen windows not supported
window.setBackground(background);
}
protected void setUp() throws Exception {
split = new MultiSplitPane();
split.setDividerSize( DIVIDER_SIZE );
testWindow = new JWindow();
testWindow.setVisible( true );
//testWindow.getContentPane().add( split );
}
private static void safeSetBackground(JWindow window, Color background) {
GraphicsConfiguration gc = window.getGraphicsConfiguration();
if (!gc.isTranslucencyCapable()) return; // PERPIXEL_TRANSLUCENT not supported
if (gc.getDevice().getFullScreenWindow() == window) return; // fullscreen windows not supported
window.setBackground(background);
}
public void show(Point location) {
Rectangle screenBounds = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gds = ge.getScreenDevices();
for (GraphicsDevice device : gds) {
GraphicsConfiguration gc = device.getDefaultConfiguration();
screenBounds = gc.getBounds();
if (screenBounds.contains(location)) {
break;
}
}
// showing the popup tooltip
cp = new TooltipContentPanel(master.getTextComponent());
Window w = SwingUtilities.windowForComponent(master.getTextComponent());
contentWindow = new JWindow(w);
contentWindow.add(cp);
contentWindow.pack();
Dimension dim = contentWindow.getSize();
if (location.y + dim.height + SCREEN_BORDER > screenBounds.y + screenBounds.height) {
dim.height = (screenBounds.y + screenBounds.height) - (location.y + SCREEN_BORDER);
}
if (location.x + dim.width + SCREEN_BORDER > screenBounds.x + screenBounds.width) {
dim.width = (screenBounds.x + screenBounds.width) - (location.x + SCREEN_BORDER);
}
contentWindow.setSize(dim);
contentWindow.setLocation(location.x, location.y - 1); // slight visual adjustment
contentWindow.setVisible(true);
Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
w.addWindowFocusListener(this);
contentWindow.addWindowFocusListener(this);
}