在所选监视器上显示JDialog

时间:2013-09-30 05:00:38

标签: java swing nullpointerexception jdialog multiscreen

我正在处理允许用户在所选屏幕上打开特定JDialog的功能。我非常接近结束它,但我还没有管理一件事。下面的方法在“screen [screen] .setFullScreenWindow”中抛出NullPointerException。虽然,JDialog出现了。在我关闭之后,NPE跳转到我的控制台窗口。我知道问题出在setFullScreenWindow方法中,但我不知道如何在选定的屏幕上“弹出”JDialog。

/*
 * SHOW ON SPECIFIC MONITOR
 */
public void showOnScreen(GraphicsDevice[] screenlist, int screen, JDialog dialog) {
     if (screen > -1 && screen < screenlist.length) {
          try {
               screenlist[screen].setFullScreenWindow(dialog);
          } catch (NullPointerException e) {
               e.printStackTrace();
          }
     } else if (screenlist.length > 0) {
          screenlist[0].setFullScreenWindow(dialog);
     } else {
          throw new RuntimeException("No Screens Found");
     }
}

我在JButton动作中调用该方法:

    buttonStartDemo.addActionListener(new ActionListener() {
 public void actionPerformed(ActionEvent e) {
      VisualField_DemoSession_JDialog dialog = new VisualField_DemoSession_JDialog();
      GraphicsDevice[] screenlist = getScreenList();
      if (screenlist.length > 1) {
           Object[] options = {"1", "2"};
           int n = JOptionPane.showOptionDialog(new JFrame(), "On which screen you want to open Demo Session?", "Question!", JOptionPane.YES_NO_OPTION, 
                     JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
           if (n == JOptionPane.YES_OPTION) {
                showOnScreen(screenlist, 0, dialog);
           } else if (n == JOptionPane.NO_OPTION) {
                showOnScreen(screenlist, 1, dialog);
           }
      } else if (screenlist.length == 1) {
           showOnScreen(screenlist, 0, dialog);
      } else {
           throw new RuntimeException("No Screens Found");
      }
 }

});

这是获取屏幕列表的方法:

public GraphicsDevice[] getScreenList() {
 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 GraphicsDevice[] gs = ge.getScreenDevices();
 return gs;
}

堆栈追踪:

java.lang.NullPointerException
 at java.awt.GraphicsDevice.setFullScreenWindow(Unknown Source)
 at sun.awt.Win32GraphicsDevice.setFullScreenWindow(Unknown Source)
 at plugin.vision.visualfield.VisualField_Main_JFrame.showOnScreen(VisualField_Main_JFrame.java:349)
 at plugin.vision.visualfield.VisualField_Main_JFrame$3.actionPerformed(VisualField_Main_JFrame.java:146)
 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
 at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
 at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
 at java.awt.EventQueue.access$200(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue$4.run(Unknown Source)
 at java.awt.EventQueue$4.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)

编辑2013/09/30,晚上8:10

确定。我想我弄清楚了这个问题是关于什么的。所以主要是,我试图将方法setFullScreenWindow称为“对话框”,它是某些JDialog类的对象。但是在调用一些init方法的按钮的操作中,我只是初始化我的“对话框”。我想念一件事 - 正确实现“对话框”对象类。所以我做到了,NPE消失了。简单地说,我使用新的Swing线程和contentPane的一些标准设置添加main方法。就是这样。

如果您有任何建议,如果您能告诉我们有关此案例的更多信息,我将非常感激。

问题解决了。低于JDialog类的标准框架:

public class DialogTest extends JDialog {

 private final JPanel contentPanel = new JPanel();

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
      try {
           DialogTest dialog = new DialogTest();
           dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
           dialog.setVisible(true);
      } catch (Exception e) {
           e.printStackTrace();
      }
 }

 /**
  * Create the dialog.
  */
 public DialogTest() {
      setBounds(100, 100, 450, 300);
      getContentPane().setLayout(new BorderLayout());
      contentPanel.setLayout(new FlowLayout());
      contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
      getContentPane().add(contentPanel, BorderLayout.CENTER);
 }

}

0 个答案:

没有答案