【问题标题】:How do I display my program in my own created window?如何在我自己创建的窗口中显示我的程序?
【发布时间】:2020-07-02 05:13:16
【问题描述】:
import java.util.*;
public class RockPaperScissors {

    public String toGenerate() {
        String x = null;
        Random generator = new Random();
        int y = generator.nextInt(2);

        switch (y) {
        case 0 : x = "Rock"; break;
        case 1 : x = "Paper"; break;
        case 2 : x = "Scissors"; break;
        }
        return x ;
    }

    public void toDisplayResult(String x , String y) {
        String comp = x;
        String c = y;
        c = c.toLowerCase();
        comp = comp.toLowerCase();

        if(comp.equals(c))
            System.out.print("\nDRAW !!!");
        else if(comp.equals("rock") && c.equals("paper"))  //Not Excuting
            System.out.print("\nYou WIN !!!");
        else if(comp.equals("rock") && (c.equals("scissors") || c.equals("scissor")))
            System.out.print("\nYou LOSE !!!");
        else if(comp.equals("scissors") && c.equals("paper"))
            System.out.print("\nYou LOSE !!!");
        else if(comp.equals("scissors") && c.equals("rock"))
            System.out.print("\nYou WIN !!!");
        else if(comp.equals("paper") && (c.equals("scissor") || c.equals("scissors")))
            System.out.print("\nYou WIN !!!");
        else if(comp.equals("paper") && c.equals("rock"))
            System.out.print("\nYou LOSE !!!");

    }

    public void toIntroduce() {
        System.out.println("Hi !!! Welcome To \"The ROCK-PAPER-SCISSORS\" v0.0508\nDeveloped By Saikat Das\nInstagram : @saikat._");
    }

    public static void main(String args[]) {
        RockPaperScissors obj = new RockPaperScissors();
        Scanner in  = new Scanner(System.in);
        String x = null;
        obj.toIntroduce();
        System.out.println();

        do {
        System.out.print("Enter Your Choice : ");
        String c = in.next();
        String comp = obj.toGenerate();
        System.out.print(comp);
        System.out.println();

        obj.toDisplayResult(comp, c);
        System.out.println();

        System.out.print("Press \"Y\" To Play Again OR \"N\" To Quit ");
        x = in.next();
        System.out.println();

        }while(x.equalsIgnoreCase("Y"));

        System.out.println("Thanks For Playing !!!");
        System.out.println("Hope You Enjoyed This Epic Game");
        System.out.println("More Updates Will Be Coming");
        System.out.println("Follow Me ON Instagram And Stay Tuned !!!");
    }

}

我还学会了使用 JFrame 制作窗口。但我不知道如何在我创建的窗口中显示它。

import java.awt.*;

import javax.swing.JFrame;
public class Windows extends Canvas{
    public void window() {
        JFrame window = new JFrame();
        Canvas canvas = new Windows();
        window.setSize(800, 600);
        canvas.setSize(800, 600);
        window.add(canvas);
        window.pack();
        window.setTitle("This Is My Own Window");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
        window.getContentPane().setBackground(Color.white);
    }


    public static void main(String args[]) {
        Windows obj = new Windows();
        obj.window();

    }

}

我也在想这个方法是否可行,如果不可行,我该怎么做呢?

【问题讨论】:

  • 你需要再写一次,System.out.println 是用来在控制台打印的,你可以用它在Jcomponent上打印
  • 我还写什么???
  • 你需要了解swing包,然后你就会知道写什么

标签: java string swing console jframe


【解决方案1】:

正如另一条评论中提到的,您当然需要添加另一个库。我建议导入javax.swing.JOptionPane 并从那里学习。从那里有多种不同的东西,但通常只需导入它,您就可以通过执行以下操作来显示一条消息:

JOptionPane.showMessageDialog(null, "your message here");

当然还有其他方法可以解决这个问题。我自己不熟悉JFrame。如果您只是在寻找基础知识,那么JOptionPane 是您的最佳选择。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    相关资源
    最近更新 更多