简体   繁体   English

如何在基于文本的Java项目中实现Jframe

[英]How do I implement a Jframe into a text-based Java project

package quiz;  //assigns package name
import java.util.Scanner;  //adds Scanner utility from java library
import java.util.Random;  //adds Random utility from java library

public class Quiz //start Quiz class
{
    String question;  //initializes question string 
    String answer;  //initializes answer variable 
    int correct=0, number;  //initializes scoring variables
    Quiz[] quizBank = new Quiz[20];  //initializes quizBank array 


    public static void main(String[] args) //start of main
    {
        Quiz bank = new Quiz();  //creates new Quiz object
        bank.bankList();  //assigns name of portion of program to build the collection of questions and answers
        bank.askQuestion();  //assigns name of portion of program to ask the questions
    }  //end main

    public void bankList() //start of bankList
    {
        quizBank[0] = new Quiz();  //Creates new object
        quizBank[0].question = "The smallest prime number";  //Initialize object variables
        quizBank[0].answer = "2";  //Initialize object variables

        quizBank[1] = new Quiz();  //Creates new object
        quizBank[1].question = "Area of triangle with base = 4 and height = 3";  //Initialize object variables
        quizBank[1].answer = "6";  //Initialize object variables    

        quizBank[2] = new Quiz();  //Creates new object
        quizBank[2].question = "Area of square with side = 5";  //Initialize object variables
        quizBank[2].answer = "25";  //Initialize object variables   

        quizBank[3] = new Quiz();  //Creates new object
        quizBank[3].question = "Square root of 144";  //Initialize object variables
        quizBank[3].answer = "12";  //Initialize object variables   

        quizBank[4] = new Quiz();  //Creates new object
        quizBank[4].question = "No. of states in US";  //Initialize object variables
        quizBank[4].answer = "50";  //Initialize object variables   

        quizBank[5] = new Quiz();  //Creates new object
        quizBank[5].question = "No. of continents in the world";  //Initialize object variables
        quizBank[5].answer = "7";  //Initialize object variables

        quizBank[6] = new Quiz();  //Creates new object
        quizBank[6].question = "In which year did man land on the moon";  //Initialize object variables
        quizBank[6].answer = "1969";  //Initialize object variables 

        quizBank[7] = new Quiz();  //Creates new object
        quizBank[7].question = "How many colors in a rainbow";  //Initialize object variables
        quizBank[7].answer = "7";  //Initialize object variables    

        quizBank[8] = new Quiz();  //Creates new object
        quizBank[8].question = "How many colors in the US flag";  //Initialize object variables
        quizBank[8].answer = "3";  //Initialize object variables    

        quizBank[9] = new Quiz();  //Creates new object
        quizBank[9].question = "Square of 25";  //Initialize object variables
        quizBank[9].answer = "625";  //Initialize object variables

                quizBank[10] = new Quiz();  //Creates new object
        quizBank[10].question = "The smallest square number";  //Initialize object variables
        quizBank[10].answer = "8";  //Initialize object variables

        quizBank[11] = new Quiz();  //Creates new object
        quizBank[11].question = "What is the square root of 16";  //Initialize object variables
        quizBank[11].answer = "4";  //Initialize object variables   

        quizBank[12] = new Quiz();  //Creates new object
        quizBank[12].question = "Area of square with side = 10";  //Initialize object variables
        quizBank[12].answer = "25";  //Initialize object variables  

        quizBank[13] = new Quiz();  //Creates new object
        quizBank[13].question = "Square root of 36";  //Initialize object variables
        quizBank[13].answer = "6";  //Initialize object variables   

        quizBank[14] = new Quiz();  //Creates new object
        quizBank[14].question = "No. of sides on a cube";  //Initialize object variables
        quizBank[14].answer = "6";  //Initialize object variables   

        quizBank[15] = new Quiz();  //Creates new object
        quizBank[15].question = "No. of 0's in a MILLION";  //Initialize object variables
        quizBank[15].answer = "6";  //Initialize object variables

        quizBank[16] = new Quiz();  //Creates new object
        quizBank[16].question = "how many years in a century";  //Initialize object variables
        quizBank[16].answer = "100";  //Initialize object variables 

        quizBank[17] = new Quiz();  //Creates new object
        quizBank[17].question = "What colour do you get when mixing blue and yellow";  //Initialize object variables
        quizBank[17].answer = "green";  //Initialize object variables   

        quizBank[18] = new Quiz();  //Creates new object
        quizBank[18].question = "How many colors in the French flag";  //Initialize object variables
        quizBank[18].answer = "3";  //Initialize object variables   

        quizBank[19] = new Quiz();  //Creates new object
        quizBank[19].question = "Cube root of 8";  //Initialize object variables
        quizBank[19].answer = "2";  //Initialize object variables
    }  //end of bankList

    public void askQuestion() //start of askQuestion
    {
        Scanner input = new Scanner(System.in);  //prepare to read input from keyboard
        System.out.println("********************************");  //prints heading top border
        System.out.println(" Welcome to my Quiz Application");  //prints heading
        System.out.println("********************************");  //prints heading bottom border

        for (number=1; number<=20; number++)  //start of counter for loop
        {
            Random draw = new Random();  //creates new random object
            int choose = draw.nextInt(20);  //randomly chooses a question
            System.out.printf("%d. %s?%n", number, quizBank[choose].question);  //prints question
            String entered = input.nextLine();  //read input

            if (entered.compareTo(quizBank[choose].answer)==0)  //checks the users input
            {
                System.out.println("*** Correct! ***");  //prints correct response
                correct = correct + 1;  //counts number of correct answers
            }
            else  //start of response for wrong answers
                System.out.println("--- Incorrect! ---");  //print the incorrect response
        }  //end of counter for loop

        System.out.println("*******************");  //prints footer top border
        System.out.printf(" Your score is %d/%d%n", correct, number-1);  //prints results
        System.out.println("*******************");  //prints footer bottom border
    }  //end of askQuestion

}  //end public class

First of all, don't expect people to do your homework. 首先,不要期望别人会做你的功课。 And second, you could research a bit. 其次,您可以进行一些研究。 And finally, to your question, you could use a JTextPane inside JScroolPane to show the output, and to the input you could add a JTextField with a button next to it. 最后,对于您的问题,您可以在JScroolPane中使用JTextPane来显示输出,向输入中添加一个JTextField并在其旁边添加一个按钮。 If you don't know how, I suggest to visit Oracle Tutorials: 如果您不知道如何,建议您访问Oracle教程:

Tutorial Main Page: https://docs.oracle.com/javase/tutorial/ 教程主页: https//docs.oracle.com/javase/tutorial/

Swing Tutorial Table of Contents page: https://docs.oracle.com/javase/tutorial/uiswing/TOC.html Swing教程目录页面: https : //docs.oracle.com/javase/tutorial/uiswing/TOC.html

EDIT: I suggest you to make the implementations separating the data model with the visual model. 编辑:我建议您进行将数据模型与可视模型分开的实现。

Have a nice day. 祝你今天愉快。

You can use a JLabel to show the question, it has a setText(String) method which you can use to change the text. 您可以使用JLabel来显示问题,它具有setText(String)方法,可用于更改文本。 To get input from the user you can use a JTextField, it has a getText() method which returns a String of the input. 要从用户那里获取输入,可以使用JTextField,它具有getText()方法,该方法返回输入的String。 For the score and the correct answer you can also use labels. 对于分数和正确答案,您也可以使用标签。 To submit the answer you can use a JButton, to check if it is pressed you can implement an ActionListener and check if the source of the click event is equal to the submit button(event.getSource() == submitButton)... 要提交答案,可以使用JButton,可以检查是否按下了JListener,可以实现ActionListener并检查click事件的源是否等于Submit Button(event.getSource()== SubmitButton)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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