2017-03-31 66 views
0

我正在使用GUI,我希望用戶能夠按「答覆」按鈕,然後控制檯將在啓用用戶輸入的情況下在textarea2上進行打印。爲了使JTextArea(textarea2)能夠作爲控制檯並在insertquestions方法中打印文本並接受用戶輸入,我需要做些什麼?JTextArea中的控制檯輸出和用戶輸入

package Scrambler; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

import java.util.Random; 
import java.util.Scanner; 

public class GUI extends JFrame { 

    private JMenuBar menuBar; 
    private JButton button1; 
    private JButton button2; 
    private JPanel panel1; 
    private JPanel panel2; 
    private JTextField textfield2; 
    private JTextArea textarea; 
    private JTextArea textarea2; 
    private JTextField textfield5; 
    private JTextField textfield7; 

    public GUI() { 

     this.setTitle("UnScrambleGUI"); 
     this.setSize(500, 400); 
     generateMenu(); 
     this.setJMenuBar(menuBar); 

     JPanel contentPane = new JPanel(null); 
     contentPane.setPreferredSize(new Dimension(500, 400)); 
     contentPane.setBackground(new Color(192, 192, 192)); 


     button1 = new JButton(); 
     button1.setBounds(56, 341, 103, 31); 
     button1.setBackground(new Color(214, 217, 223)); 
     button1.setForeground(new Color(51, 153, 0)); 
     button1.setEnabled(true); 
     button1.setFont(new Font("sansserif", 0, 12)); 
     button1.setText("Print Words"); 
     button1.setVisible(true); 

     button1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       insertwords(evt); 
      } 
     }); 


     button2 = new JButton(); 
     button2.setBounds(311, 341, 95, 31); 
     button2.setBackground(new Color(214, 217, 223)); 
     button2.setForeground(new Color(51, 153, 0)); 
     button2.setEnabled(true); 
     button2.setFont(new Font("sansserif", 0, 12)); 
     button2.setText("Answer"); 
     button2.setVisible(true); 

     button2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       insertquestions(evt); 
      } 
     }); 


     panel1 = new JPanel(null); 
     panel1.setBorder(BorderFactory.createEtchedBorder(1)); 
     panel1.setBounds(22, 92, 179, 237); 
     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(229, 170, 258, 161); 
     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); 

     textfield2 = new JTextField(); 
     textfield2.setBounds(74, 65, 89, 25); 
     textfield2.setBackground(new Color(153, 153, 153)); 
     textfield2.setForeground(new Color(255, 255, 255)); 
     textfield2.setEnabled(true); 
     textfield2.setFont(new Font("sansserif", 0, 12)); 
     textfield2.setText(" Word List"); 
     textfield2.setVisible(true); 

     textarea = new JTextArea(); 
     textarea.setBounds(10, 9, 156, 219); 
     textarea.setBackground(new Color(255, 255, 255)); 
     textarea.setForeground(new Color(0, 0, 0)); 
     textarea.setEnabled(true); 
     textarea.setFont(new Font("sansserif", 0, 12)); 
     textarea.setText(""); 
     textarea.setVisible(true); 

     textarea2 = new JTextArea(); 
     textarea2.setBounds(9, 9, 237, 143); 
     textarea2.setBackground(new Color(255, 255, 255)); 
     textarea2.setForeground(new Color(0, 0, 0)); 
     textarea2.setEnabled(true); 
     textarea2.setFont(new Font("sansserif", 0, 12)); 
     textarea2.setText(""); 
     textarea2.setVisible(true); 

     textfield5 = new JTextField(); 
     textfield5.setBounds(307, 140, 89, 28); 
     textfield5.setBackground(new Color(153, 153, 153)); 
     textfield5.setForeground(new Color(255, 255, 255)); 
     textfield5.setEnabled(true); 
     textfield5.setFont(new Font("sansserif", 0, 12)); 
     textfield5.setText(" Answer Here:"); 
     textfield5.setVisible(true); 

     textfield7 = new JTextField(); 
     textfield7.setBounds(164, 10, 175, 37); 
     textfield7.setBackground(new Color(102, 102, 102)); 
     textfield7.setForeground(new Color(255, 255, 255)); 
     textfield7.setEnabled(true); 
     textfield7.setFont(new Font("Tahoma", 0, 18)); 
     textfield7.setText(" Unscrambler Game"); 
     textfield7.setVisible(true); 

     contentPane.add(button1); 
     contentPane.add(button2); 
     contentPane.add(panel1); 
     contentPane.add(panel2); 
     contentPane.add(textfield2); 
     panel1.add(textarea); 
     panel2.add(textarea2); 
     contentPane.add(textfield5); 
     contentPane.add(textfield7); 

     this.add(contentPane); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 
     this.pack(); 
     this.setVisible(true); 


    } 

    private void insertwords(ActionEvent evt) { 

     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"; 

     String line1 = "-----------------------------------------"; 
     word1 = scramble(r, word1); 
     String one = "#1- " + word1; 
     word2 = scramble(r, word2); 
     String two = "#2- " + word2; 
     word3 = scramble(r, word3); 
     String three = "#3- " + word3; 
     word4 = scramble(r, word4); 
     String four = "#4- " + word4; 
     word5 = scramble(r, word5); 
     String five = "#5- " + word5; 
     word6 = scramble(r, word6); 
     String six = "#6- " + word6; 
     word7 = scramble(r, word7); 
     String seven = "#7- " + word7; 
     word8 = scramble(r, word8); 
     String eight = "#8- " + word8; 
     word9 = scramble(r, word9); 
     String nine = "#9- " + word9; 
     word10 = scramble(r, word10); 
     String ten = "#10- " + word10; 
     word11 = scramble(r, word11); 
     String elev = "#11- " + word11; 
     word12 = scramble(r, word12); 
     String twel = "#12- " + word12; 
     String line2 = ("-----------------------------------------"); 

     textarea.setText(one + "\n" + two + "\n" + three + "\n" + four + "\n" + five + "\n" + six + "\n" + seven + "\n" + eight + "\n" + nine + "\n" + ten + "\n" + elev + "\n" + twel + "\n"); 


    } 


    private void insertquestions(ActionEvent evt) { 
     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 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(); 
      } 
     }); 
    } 


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

     char a[] = inputString.toCharArray(); 

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

      char temp = a[n]; 
      a[n] = a[k]; 
      a[k] = temp; 
     } 

     return new String(a); 
    } 
} 
+0

最好的解決方案不是這樣做,而是簡單地將文本附加到JTextArea。爲什麼試圖將線性控制檯程序強制加入到事件驅動的GUI中,從而使事情變得比他們更難?您還有其他與此代碼無關的問題,包括使用空佈局和'setBounds(...)'的新手錯誤。 –

+0

啊,你也使用基於System.in的掃描儀。停止。真的,只要停下來,暫停並重新思考整個程序,因爲你正在與Swing庫的事件驅動模型作鬥爭,並且只會讓事情變得比應該更加困難。 –

+0

是的,我是Java的新手,對於錯誤感到抱歉。感謝您的幫助。 –

回答

1

正如評論所說,你不想鞋拔一個控制檯程序到GUI爲你建議,因爲儘管這是做,能,這需要通過線程跳火圈,並會編程針對 GUI庫的事件驅動範例。

相反,我強烈建議您「重構」這個程序,即重新構建並重新編寫它,並着眼於創建一組強大的類,這些類可以在事件驅動環境。我會將問題解構成非GUI模型類和GUI或「視圖」類,最重要的是前者,模型。

例如,對於模型,考慮創建以下類:

  • ScrambledWord:一類,它和保持測試字符串,即取並存儲「暗示」的字符串,可以產生隨機加擾測試字符串的版本,它有一個public boolean evaluate(String)方法可以測試一個字符串以查看它是否正確,並且具有適當的構造函數,設置程序和getter。
  • 學生:使用GUI保存學生測試分數的類
  • 測試:一個保存ScrambledWord對象的集合(例如ArrayList)的類,該對象包含一個Student對象,該對象可以遍歷此集合返回列表中的下一個ScrambledWord,它可以更改學生的測試分數。

然後用於GUI,考慮創建

  • 一個QuestionPanel的JPanel,其具有顯示當前測試擾頻字,一個JTextField其中用戶可以輸入他們的推測一個JLabel,和一個JButton用於當該學生想提交他們的猜測。如果猜測錯誤,則可以顯示錯誤的JOptionPane,並顯示提示。如果猜測是正確的,那麼顯示下一個問題。也許還可以給它另一個JButton,讓用戶在無法解決的情況下跳過當前的問題。
  • 一個包含Question JPanel的主JPanel,允許用戶開始測試,完成測試,顯示用戶的分數。

這裏的關鍵是按鈕的ActionListeners。提交按鈕的偵聽器將通過調用傳遞文本字段的文本的evaluate(...)方法來針對當前顯示的ScrambledWord對象測試JTextField中的文本。如果evaluate返回true,則增加學生的分數並在QuestionPanel JPanel中顯示下一個問題,可能與public void setScrambledWord(ScrambledWord sWord)

這些都只是建議,你可以按照需要或不,但我的主要建議是不要試圖強制控制檯應用程序進入GUI。如果你這樣做,你不會是一個快樂的程序員。