我的导师有问题指导我解决下面的问题。希望我能从社区找到一些答案。
我正在尝试将一个图像弹出到一个简单的控制台应用程序,从研究看起来我需要使用JImage / JFrame(我认为)。但是,我的导师认为我可以使用下面的代码(当我使用它时它不起作用,导师不知道为什么不这样做。)
System.out.println(new Picture(FileChooser.pickAFile())。show())。 这将返回"图片无法解析为类型"。
我相信它应该允许我选择一张图片,但它并没有。我是否将图片的目的地直接放入代码也无关紧要。
简单地说,当我从菜单中选择一个选项时,我需要一张图片打印到屏幕/新窗口,如果有人可以提供帮助,那就太棒了!
下面的完整代码(部分靠近底部/ loadMuseums方法)
package CWK2;
//IMPORTS REQUIRED FOR COMPILATION
import java.util.Scanner;
public class menuSystem {
public static void main(String[] args) throws Exception {
boolean incorrect = false; // SETS THE VARIABLE FOR THE WHILE CONDITION BELOW
String input = "";
while (incorrect == false) { // WHILE STATEMENT TO MAKE THE BELOW A
// REPEATING MENU IF WRONG INPUT
incorrect = true; // TO INITIALLY MAKE THE LOOP ONLY RUN ONCE (E.G CORRECT INPUT)
// PRINTING OF THE MENU
System.out.println("**WELCOME TO THE TOUR GUIDE**");
System.out.println();
System.out.println("1. Museums");
System.out.println("2. Eating Out");
System.out.println("3. Shopping Centres");
System.out.println("4. Historical / Places of Interest");
System.out.println();
System.out.print("Please enter your choice (1/2/3/4) : ");
Scanner choice = new Scanner(System.in);
input = choice.next();
System.out.println();
switch (input) { // SWITCHES FROM USER'S INPUT
case "1": {
loadMuseum();
choice.close(); // CLOSES THE SCANNER AND STOPS MENU REPEAT
break;
}
case "2": {
loadEateries();
choice.close(); // CLOSES THE SCANNER AND STOPS MENU REPEAT
break;
}
case "3": {
loadShops();
choice.close(); // CLOSES THE SCANNER AND STOPS MENU REPEAT
break;
}
case "4": {
loadHistoricals();
choice.close(); // CLOSES THE SCANNER AND STOPS MENU REPEAT
break;
}
default: { // DEFAULT METHOD USED FOR INCORRECT INPUT (ANYTHING
// OTHER THAN 1-4)
System.out.println("");
System.out.println("You have entered an incorrect option.");
System.out.println("You will need to try that again. Remember 1-4 are your choices.");
System.out.println("");
incorrect = false; // KEEPS THE REPEATING MENU GOING
break;
}
}
}
}
public static void loadMuseum() {
//insert pic
System.out.println(new Picture(FileChooser.pickAFile()).show()).
System.out.println("");
System.out.println("");
//insert pic
System.out.println("");
System.out.println("");
}
private static void loadEateries() {
//insert pic
System.out.println(""); //insert bio
System.out.println("");
//insert pic
System.out.println("");
System.out.println("");
}
private static void loadShops() {
//insert pic
System.out.println(""); //insert bio
System.out.println("");
//insert pic
System.out.println("");
System.out.println("");
}
private static void loadHistoricals() {
//insert pic
System.out.println(""); //insert bio
System.out.println("");
//insert pic
System.out.println(""); //insert bio
System.out.println("");
}
}
答案 0 :(得分:0)
为了显示图片,您需要一个JFrame或其他类型的容器。 System.out.println()打印到仅显示文本的控制台。
您可以在此处找到如何执行此操作:Displaying Image in Java