如何正确地将这些方法输入这些文本字段?

时间:2017-03-10 18:22:35

标签: java

我正在努力用GUI制作一个解密游戏。

我想要" toprint"来自争夺类的方法要打印在一个(更高的)文本字段和"问题"在另一个(较小的)文本字段中打印的方法。我希望文本字段带有"问题"访问用户输入的方法,如控制台/终端。我希望我能很好地解释这一点,因为我几乎是编码的新手。

如何从我们的GUI中的scramble类中获取这些方法以在这些文本字段中打印(以及必须在文本字段中接受的用户输入)?

package ScramberGame;

import java.util.Random;
import java.io.*;
import java.util.Scanner;

public class scramble
{
 public static void main(String[] args) {
    new scramble().printwords();
    new scramble().question();
  }


 public void printwords(){
     Random r = new Random();

   String word1 = "application";
   String word2 = "programming";
   String word3 = "variable";
   String word4 = "method";
   String word5 = "class";
   String word6 = "package";
   String word7 = "import";
   String word8 = "boolean";
   String word9 = "debugger";
   String word10 = "syntax";
   String word11 = "compile";
   String word12 = "computer";

   System.out.println("-----------------------------------------");
   word1 = scramble( r, word1 );
   System.out.println("#1- Unscramble: " + word1 );
   word2 = scramble( r, word2 );
   System.out.println("#2- Unscramble: " + word2 );
   word3 = scramble( r, word3 );
   System.out.println("#3- Unscramble: " + word3 );
   word4 = scramble( r, word4 );
   System.out.println("#4- Unscramble: " + word4 );
   word5 = scramble( r, word5 );
   System.out.println("#5- Unscramble: " + word5 );
   word6 = scramble( r, word6 );
   System.out.println("#6- Unscramble: " + word6 );
   word7 = scramble( r, word7 );
   System.out.println("#7- Unscramble: " + word7 );
   word8 = scramble( r, word8 );
   System.out.println("#8- Unscramble: " + word8 );
   word9 = scramble( r, word9 );
   System.out.println("#9- Unscramble: " + word9);
   word10 = scramble( r, word10 );
   System.out.println("#10- Unscramble: " + word10 );
   word11 = scramble( r, word11 );
   System.out.println("#11- Unscramble: " + word11 );
   word12 = scramble( r, word12 );
   System.out.println("#12- Unscramble: " + word12 );
   System.out.println("-----------------------------------------");
  }


 public void question(){
  boolean incorrect = true;

  for (int attempts = 0; attempts < 3 && incorrect; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #1?");
    String answer1 = input.nextLine();

   if(!"application".equals(answer1)){
       System.out.println("Incorrect. Hint: This runs on your computer.");    
    } else {
       System.out.println("Correct.");
       incorrect = false;

   }
  }

  boolean incorrect2 = true;

  for (int attempts = 0; attempts < 3 && incorrect2; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #2?");
    String answer2 = input.nextLine();

   if(!"programming".equals(answer2)){
      System.out.println("Incorrect. Hint: Writing code for a computer.");
    }else{
       System.out.println("Correct.");
       incorrect2 = false;
   }
  }

  boolean incorrect3 = true;

  for (int attempts = 0; attempts < 3 && incorrect3; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #3?");
    String answer3 = input.nextLine();

   if(!"variable".equals(answer3)){
        System.out.println("Incorrect. Hint: A value in a program that can be changed.");
    }else{
       System.out.println("Correct.");
       incorrect3 = false;
   }
  }  

  boolean incorrect4 = true;

  for (int attempts = 0; attempts < 3 && incorrect4; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #4?");
    String answer4 = input.nextLine();

   if(!"method".equals(answer4)){
   System.out.println("Incorrect. Hint: Tells the computer to do a procedure.");
    }else{
       System.out.println("Correct.");
       incorrect4 = false;
   }
  }

  boolean incorrect5 = true;

   for (int attempts = 0; attempts < 3 && incorrect5; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #5?");
    String answer5 = input.nextLine();

   if(!"class".equals(answer5)){
        System.out.println("Incorrect. Hint: Used to describe one or more objects in a program.");
    }else{
       System.out.println("Correct.");
       incorrect5 = false;
   }
  }

  boolean incorrect6 = true;

  for (int attempts = 0; attempts < 3 && incorrect6; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #6?");
    String answer6 = input.nextLine();

   if(!"package".equals(answer6)){
        System.out.println("Incorrect. Hint: Organizes a set of classes or interfaces.");
    }else{
       System.out.println("Correct.");
       incorrect6 = false;
   }
  }

  boolean incorrect7 = true;

  for (int attempts = 0; attempts < 3 && incorrect7; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #7?");
    String answer7 = input.nextLine();

   if(!"import".equals(answer7)){
       System.out.println("Incorrect. Hint: To send data from one program to another.");
    }else{
        System.out.println("Correct.");
        incorrect7 = false;
   }
  }

  boolean incorrect8 = true;

  for (int attempts = 0; attempts < 3 && incorrect8; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #8?");
    String answer8 = input.nextLine();

   if(!"boolean".equals(answer8)){
        System.out.println("Incorrect. Hint: True or False?");
    }else{
        System.out.println("Correct.");
        incorrect8 = false;
   }
  }

  boolean incorrect9 = true;

  for (int attempts = 0; attempts < 3 && incorrect9; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #9?");
    String answer9 = input.nextLine();

   if(!"debugger".equals(answer9)){
        System.out.println("Incorrect. Hint: Used to exterminate bugs.");
    }else{
        System.out.println("Correct.");
        incorrect9 = false;
   }
  }

  boolean incorrect10 = true;

  for (int attempts = 0; attempts < 3 && incorrect10; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #10?");
    String answer10 = input.nextLine();

   if(!"syntax".equals(answer10)){
       System.out.println("Incorrect. Hint: Set of rules to follow while programming.");
    }else{
     System.out.println("Correct.");
     incorrect10 = false;
   }
  }

  boolean incorrect11 = true;

  for (int attempts = 0; attempts < 3 && incorrect11; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #11?");
    String answer11 = input.nextLine();

   if(!"compile".equals(answer11)){
        System.out.println("Incorrect. Hint: To translate code into a understandable language for a machine.");
    }else{
       System.out.println("Correct.");
       incorrect11 = false;
   }
  }

  boolean incorrect12 = true;

   for (int attempts = 0; attempts < 3 && incorrect12; attempts++) {
    Scanner input = new Scanner(System.in);
    System.out.println("What is the answer to #12?");
    String answer12 = input.nextLine();

   if(!"computer".equals(answer12)){
        System.out.println("Incorrect. Hint: What you are using right now.");
    }else{
       System.out.println("Correct.");
       incorrect12 = false;
   }
  }}


 public static String scramble( Random random, String inputString )
  {

    char a[] = inputString.toCharArray();


    for( int i=0 ; i<a.length-1 ; i++ )
    {
        int j = random.nextInt(a.length-1);

        char temp = a[i]; a[i] = a[j];  a[j] = temp;
    }      

    return new String( a );
 }
}
package ScramberGame;


/**
*Text genereted by Simple GUI Extension for BlueJ
*/
import ScramberGame.scramble;
import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.border.Border;
import javax.swing.*;


public class GUI_project extends JFrame implements ActionListener {

    private JMenuBar menuBar;
    private JButton button1;
    private JPanel panel1;
    private JPanel panel2;
    private JTextArea textarea4;
    private JTextArea textarea5;
    private JTextArea textarea6;
    private JTextField textfield2;
    private JTextField textfield3;

    //Constructor 
    public GUI_project(){

        this.setTitle("Unscrambler Game");
        this.setSize(500,400);
        //menu generate method
        generateMenu();
        this.setJMenuBar(menuBar);

        //pane with null layout
        JPanel contentPane = new JPanel(null);
        contentPane.setPreferredSize(new Dimension(500,400));
        contentPane.setBackground(new Color(204,204,204));


        button1 = new JButton();
        button1.addActionListener(this);
        button1.setBounds(195,51,103,34);
        button1.setBackground(new Color(214,217,223));
        button1.setForeground(new Color(0,153,51));
        button1.setEnabled(true);
        button1.setFont(new Font("Tahoma",0,12));
        button1.setText("Begin Game");
        button1.setVisible(true);

        panel1 = new JPanel(null);
        panel1.setBorder(BorderFactory.createEtchedBorder(1));
        panel1.setBounds(12,122,173,233);
        panel1.setBackground(new Color(214,217,223));
        panel1.setForeground(new Color(0,0,0));
        panel1.setEnabled(true);
        panel1.setFont(new Font("sansserif",0,12));
        panel1.setVisible(true);

        panel2 = new JPanel(null);
        panel2.setBorder(BorderFactory.createEtchedBorder(1));
        panel2.setBounds(241,233,223,125);
        panel2.setBackground(new Color(214,217,223));
        panel2.setForeground(new Color(0,0,0));
        panel2.setEnabled(true);
        panel2.setFont(new Font("sansserif",0,12));
        panel2.setVisible(true);

        textarea4 = new JTextArea();
        textarea4.setBounds(300,207,108,20);
        textarea4.setBackground(new Color(153,153,153));
        textarea4.setForeground(new Color(255,255,255));
        textarea4.setEnabled(true);
        textarea4.setFont(new Font("Tahoma",0,12));
        textarea4.setText("    Answer Here:");
        textarea4.setBorder(BorderFactory.createBevelBorder(1));
        textarea4.setVisible(true);

        textarea5 = new JTextArea();
        textarea5.setBounds(11,13,152,204);
        textarea5.setBackground(new Color(255,255,255));
        textarea5.setForeground(new Color(0,0,0));
        textarea5.setEnabled(true);
        textarea5.setFont(new Font("sansserif",0,12));
        textarea5.setText("");
        textarea5.setBorder(BorderFactory.createBevelBorder(1));
        textarea5.setVisible(true);

        textarea6 = new JTextArea();
        textarea6.setBounds(12,10,201,106);
        textarea6.setBackground(new Color(255,255,255));
        textarea6.setForeground(new Color(0,0,0));
        textarea6.setEnabled(true);
        textarea6.setFont(new Font("sansserif",0,12));
        textarea6.setText("");
        textarea6.setBorder(BorderFactory.createBevelBorder(1));
        textarea6.setVisible(true);

        textfield2 = new JTextField();
        textfield2.setBounds(47,88,96,29);
        textfield2.setBackground(new Color(153,153,153));
        textfield2.setForeground(new Color(255,255,255));
        textfield2.setEnabled(true);
        textfield2.setFont(new Font("Tahoma",0,12));
        textfield2.setText("    Word List");
        textfield2.setVisible(true);

        textfield3 = new JTextField();
        textfield3.setBounds(146,11,200,37);
        textfield3.setBackground(new Color(102,102,102));
        textfield3.setForeground(new Color(255,255,255));
        textfield3.setEnabled(true);
        textfield3.setFont(new Font("Tahoma",0,18));
        textfield3.setText("    Unscrambler Game ");
        textfield3.setVisible(true);

        //adding components to contentPane panel
        contentPane.add(button1);
        contentPane.add(panel1);
        contentPane.add(panel2);
        contentPane.add(textarea4);
        panel1.add(textarea5);
        panel2.add(textarea6);
        contentPane.add(textfield2);
        contentPane.add(textfield3);

        //adding panel to JFrame and seting of window position and close operation
        this.add(contentPane);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.pack();
        this.setVisible(true);
    }

      public void actionPerformed(ActionEvent a) {
       String[] args = {};
       scramble.main(args);
  }

    //method for generate menu
    public void generateMenu(){
        menuBar = new JMenuBar();

        JMenu file = new JMenu("File");
        JMenu tools = new JMenu("Tools");
        JMenu help = new JMenu("Help");

        JMenuItem open = new JMenuItem("Open   ");
        JMenuItem save = new JMenuItem("Save   ");
        JMenuItem exit = new JMenuItem("Exit   ");
        JMenuItem preferences = new JMenuItem("Preferences   ");
        JMenuItem about = new JMenuItem("About   ");


        file.add(open);
        file.add(save);
        file.addSeparator();
        file.add(exit);
        tools.add(preferences);
        help.add(about);

        menuBar.add(file);
        menuBar.add(tools);
        menuBar.add(help);
    }



     public static void main(String[] args){
        System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new GUI_project();
            }
        });
    }

}

0 个答案:

没有答案