我正在开发允许用户在所选屏幕上打开特定 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 类的对象。但是在调用一些初始化方法的按钮的操作中,我只是初始化了我的“对话框”。我想念一件事——“对话”对象类的正确实现。所以我做到了,NPE 消失了。只需添加带有新 Swing 线程的 main 方法和 contentPane 的一些标准设置。就是这样。
如果您有任何建议,并且如果你们能告诉更多关于这个案例的信息,我将非常感激。
问题解决了。下面是 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);
}
}