1 回答

TA貢獻1839條經驗 獲得超15個贊
我相信你在這里沒有任何問題,只是你的 JOptionPane 隱藏在你的 IDE 窗口后面或者在后面的某個地方。為了始終將其放在前面,請嘗試使用以下內容:
if (hello.equals("hey")) {
JOptionPane pane = new JOptionPane();
JDialog dialog = pane.createDialog("My Test");
pane.setMessage("Hello There");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
}
這將使您在要使其可見的位置上更加靈活。另一種方式更短,但同樣的想法:
if (hello.equals("hey")) {
JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Hello There");
}
完整的代碼供您使用:
import javax.swing.*;
import java.util.Scanner;
public class HW2 {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Say hey");
String hello = kb.nextLine(); //use kb.nextLine().trim() if you dont want whitespaces
if (hello.equals("hey")) {
JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Hello There");
}
}
}
添加回答
舉報