简体   繁体   中英

Java: Import a class to Jframe

I am new to java and I am wondering what is the best way to go about importing a class to a JFrame since I have a few minor applications I would like to make run in a window other then eclipse.

Here is some of the code I am working with (Its a modified version of some code in the dummies book):

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;

public class GuessAgain {

/**
 * @param args
 */
public static void main(String[] args) {
    // Set up input and main info
    Scanner input = new Scanner(System.in);
    int numGuesses = 0;
    int randomNumber = new Random().nextInt(10) + 1;

    // Display welcome message
    out.println("~-=********************=-~");
    out.println("    The Guessing Game!    ");
    out.println("~-=********************=-~");

    // Give instruction
    out.print("Please enter a number between 1 and 10: ");
    int inputNumber = input.nextInt();
    numGuesses++;

    // Recurse till user gets the answer
    while (inputNumber != randomNumber) {
        out.println();
        out.println("~-=********************=-~");
        out.println("     Please try again     ");
        out.println("~-=********************=-~");
        out.print("Enter another number between 1 and ten: ");
        inputNumber = input.nextInt();
        numGuesses++;
    }
    out.print("You won after: " + numGuesses + " guesses");

}

}

So how do I go about putting all of that into a window?

You'll probably want to take a look at oracle's swing tutorial , that's what you need to learn how to use in order to make a frame/window. You can make a nice GUI (Graphic User Interface) for your game with that, rather than just using Eclipse's console ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM