类javax.swing.Popup实验

时间:2019-06-05 18:08:04

标签: java swing

我发现了javax.swing.Popup类。我编写了代码来测试其行为。代码如下所示。运行代码时,在显示JFrame之后,单击SHOW按钮,然后显示Popup。之后,我单击“隐藏”按钮,Popup消失了。但是,如果我再次单击“显示”按钮,则什么也没有发生。 Popup仅在首次单击“显示”按钮后出现。另外,如果我先单击“隐藏”按钮,再单击“显示”按钮,然后单击“显示”按钮,则不会出现Popup

我想念什么吗?
我在做错什么吗?

我承认我没有对此行为进行调查。我没有搜索互联网,也没有看过PopupPopupFactory类的代码,这仅仅是出于懒惰,希望有人能向我解释。

这是我的MCVE。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class PopupTest implements ActionListener, Runnable {
    private static final String HIDE = "HIDE";
    private static final String SHOW = "SHOW";
    private Popup popup;

    public void actionPerformed(ActionEvent actionEvent) {
        String actionCommand = actionEvent.getActionCommand();
        switch (actionCommand) {
            case HIDE:
                popup.hide();
                break;
            case SHOW:
                popup.show();
                break;
        }
    }

    public void run() {
        showGui();
    }

    private void showGui() {
        JFrame frame = new JFrame("Popup");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JLabel centerLabel = new JLabel("CENTER LABEL", SwingConstants.CENTER);
        frame.add(centerLabel, BorderLayout.CENTER);
        JPanel buttonsPanel = new JPanel();
        JButton showButton = new JButton(SHOW);
        showButton.addActionListener(this);
        buttonsPanel.add(showButton);
        JButton hideButton = new JButton(HIDE);
        hideButton.addActionListener(this);
        buttonsPanel.add(hideButton);
        frame.add(buttonsPanel, BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        JLabel popupLabel = new JLabel("Popup_Label");
        PopupFactory factory = PopupFactory.getSharedInstance();
        Point pt = centerLabel.getLocationOnScreen();
        int x = pt.x + 10;
        int y = pt.y - 10;
        popup = factory.getPopup(centerLabel, popupLabel, x, y);
    }

    public static void main(String[] args) {
        PopupTest instance = new PopupTest();
        EventQueue.invokeLater(instance);
    }
}

2 个答案:

答案 0 :(得分:1)

https://docs.oracle.com/javase/7/docs/api/javax/swing/Popup.html#hide()

您选择不阅读的文档非常清楚,hide()取代了Popup,对该Popup进行的任何其他方法调用都将导致不确定的行为。

您每次必须创建一个新的Popup实例。

答案 1 :(得分:1)

从hide()的文档中获得

  

“隐藏和处理弹出窗口。一旦处理了弹出窗口,您将   应该不再调用其上的方法。处置的弹出窗口可能是   回收,以后根据PopupFactory使用。”

因此,这是一个快速有效的修改版本。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

public class PopupTest implements ActionListener, Runnable {

  private static final String HIDE = "HIDE";
  private static final String SHOW = "SHOW";
  private Popup popup;
  private JLabel centerLabel;
  private JLabel popupLabel = new JLabel("Popup_Label");
  private PopupFactory factory = PopupFactory.getSharedInstance();

  public void actionPerformed(ActionEvent actionEvent) {
    String actionCommand = actionEvent.getActionCommand();
    switch (actionCommand) {
      case HIDE:
        if (popup == null) {
          return;
        }
        popup.hide();
        popup = null; // necessary to avoid using the disposed popup
        break;

      case SHOW:
        if (popup != null) { // it's already showing
          return;
        }
        Point pt = centerLabel.getLocationOnScreen();
        int x = pt.x + 10;
        int y = pt.y - 10;
        popup = factory.getPopup(centerLabel, popupLabel, x, y);
        popup.show();
        break;
    }
  }

  public void run() {
    showGui();
  }

  private void showGui() {
    JFrame frame = new JFrame("Popup");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    centerLabel = new JLabel("CENTER LABEL", SwingConstants.CENTER);
    frame.add(centerLabel, BorderLayout.CENTER);
    JPanel buttonsPanel = new JPanel();
    JButton showButton = new JButton(SHOW);
    showButton.addActionListener(this);
    buttonsPanel.add(showButton);
    JButton hideButton = new JButton(HIDE);
    hideButton.addActionListener(this);
    buttonsPanel.add(hideButton);
    frame.add(buttonsPanel, BorderLayout.PAGE_END);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    PopupTest instance = new PopupTest();
    EventQueue.invokeLater(instance);
  }
}