如何使 JOptionPane 对话框显示为任务栏上的任务?
-
23-08-2019 - |
题
编辑:
问题遵循水平规则;我自己的答案先于它。
基于来自的帮助 奥斯卡·雷耶斯, ,我精心设计了这个解决方案:
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class MyApp extends JFrame {
public static void main(String [] args) {
new MyApp();
}
public MyApp() {
super("MyApp");
setUndecorated(true);
setVisible(true);
setLocationRelativeTo(null);
String i = JOptionPane.showInputDialog(this, "Enter your name:", getTitle(), JOptionPane.QUESTION_MESSAGE);
if(null != i) {
JOptionPane.showInputDialog(this, "Your name is:", getTitle(), JOptionPane.INFORMATION_MESSAGE, null, null, i.concat(i));
}
dispose();
}
}
请注意,我还在 JOptionPane.showInputDialog 中显示输出。这样,输出就会在文本字段中突出显示,因此我只需按 控制键+C 将输出复制到系统剪贴板,然后按 ESC键 驳回申请。
我为我的简单应用程序创建了一个简单的 GUI。我的应用程序提示输入一个 JOptionPane.showInputDialog
, ,执行计算,然后显示单个输出 JOptionPane.showMessageDialog
. 。我有时会切换到最大化的浏览器窗口或其他窗口进行复制,然后想切换回 JOptionPane 对话框进行粘贴。
我想让我的 JOptionPane 对话框作为任务显示在任务栏上,这样我就可以像几乎任何其他正在运行的程序一样切换到它。我更喜欢 JOptionPane 的简单性,而不是必须创建 JFrame、FlowLayout、Icon、JTextField、JButton、ActionListener 等等。
- JOptionPane 可以作为任务显示在任务栏上吗?
- 如果是这样,我该如何让它显示出来?
- 如果没有的话,有没有其他像单行一样的东西
JOptionPane.show[whatever]dialog()
?
解决方案
- JOptionPane 可以作为任务显示在任务栏上吗?
不
- 如果没有,还有其他像 JOptionPane.show[whatever]dialog() 这样的单行代码吗?
不完全是一条线(但有一些额外的 6 条线:P)
我敢打赌,您可以非常轻松地将所有这些内容放入一个实用方法中,并在需要时通过一次调用来调用它。
以下代码将为您的应用程序添加一个任务栏。
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class OptionTest {
public static void main ( String [] args ) {
JFrame frame = new JFrame("My dialog asks....");
frame.setUndecorated( true );
frame.setVisible( true );
frame.setLocationRelativeTo( null );
String message = JOptionPane.showInputDialog(frame,
"Would this be enough?.",
"My dialog asks....",
JOptionPane.INFORMATION_MESSAGE);
System.out.println( "Got " + message );
frame.dispose();
}
}
顺便说一句,在 Windows Vista 中,我可以使用 Alt+Tab 切换到 OptionPane,无需执行任何其他操作(尽管正如您所说,我无法在任务栏中看到它)
其他提示
这个问题取决于平台。在我的系统(带有 OpenBox 的 Debian Linux)上,JOptionPanes 始终显示在任务栏上。我无法阻止任何 JDialog 出现在任务栏上。