GrahicsDevice和JOptionPane问题 [英] GrahicsDevice and JOptionPane Issue
问题描述
当我单击按钮时,设置为全屏的应用程序将转到任务栏/最小化,因此我需要先在任务栏中单击它,然后才能看到我触发的JOptionPane
.您认为这是什么问题?我希望它运行平稳而不被最小化或转到任务栏.
When I click the button, the application which is set to full screen will go to taskbar/minimized, so I need to click it first in the taskbar before seeing the JOptionPane
that I triggered. What do you think is the problem with this? I'd like it to run smoothly without being minimized or going to taskbar.
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Sample");
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);
JButton btn = new JButton();
btn.setText("Btn");
JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Sample");
throw new UnsupportedOperationException("Not supported yet.");
}
});
}
推荐答案
我能想到的唯一解决方法是将WindowAdpater
添加到JFrame
中,它将覆盖windowIconified(..)
.还有一个boolean
用作程序的标志,以了解由于显示了JOptionPane
窗口何时被图标化.
The only work around I can think of is to add a WindowAdpater
to JFrame
which will override windowIconified(..)
. There is also a boolean
used as a flag for the program to know when the window is iconified due to JOptionPane
being shown.
它确实很hacky,只有在几次屏幕闪烁后,我们才能看到JOptionPane
和JFrame
很好地协同工作.
Its really hacky though and only after a few screen flashes do we see the JOptionPane
and JFrame
working together nicely.
这是代码:
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
private static boolean programmatic = false;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Sample");
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent we) {
//super.windowIconified(we);
if (programmatic) {
programmatic = false;
frame.setState(JFrame.NORMAL);
}
}
});
JButton btn = new JButton();
btn.setText("Btn");
final JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
programmatic = true;
JOptionPane.showMessageDialog(panel, "Sample");
}
});
frame.setVisible(true);
}
});
}
}
再想一想,JDialog
也会重现结果,我认为这是由于JOptionPane s和JDialog
s的="nofollow">模式.也许使用JDialog
并设置模态即可解决问题.
And thinking about it more, JDialog
also reproduces results, I think its due to the modality of JOptionPane
s and JDialog
s. Maybe using JDialog
and setting the modality would do the trick.
这篇关于GrahicsDevice和JOptionPane问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!