2013-12-15 103 views
1

我想將控制檯帶入具有與掃描儀輸入交互功能的jFrame窗口應用程序。基本上,無論在控制檯中發生什麼,我都希望在TextArea的應用程序窗口中顯示它。有一個簡單的解決方案嗎?如何模擬帶有掃描儀輸入的控制檯或將控制檯帶入jFrame應用程序

這是我非常簡單的代碼

ConsoleLogic類:

import java.util.Scanner; 


public class ConsoleLogic { 

    public static void main(String[] args) { 

     System.out.println("How old are you?"); 

     Scanner scanner = new Scanner(System.in); 
     int input = scanner.nextInt(); 

     System.out.println(""); 
     System.out.println("How many siblings do you have?"); 
     int input2 = scanner.nextInt(); 

     System.out.println("Thank you for your answer!"); 
     System.out.println("You are "+input+ " years old and you have "+input2+" 2siblings."); 

    } 

} 

ConsoleGui類:

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JTextArea; 
import javax.swing.JLabel; 

public class ConsoleGui extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ConsoleGui frame = new ConsoleGui(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public ConsoleGui() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     JLabel lblDisplayOutput = new JLabel("Display output:"); 
     lblDisplayOutput.setBounds(22, 25, 124, 16); 
     contentPane.add(lblDisplayOutput); 

     JTextArea textArea = new JTextArea();// ideally I would like to bring 
               // console and the inputs inside 
               // here 
     textArea.setBounds(22, 65, 402, 185); 
     contentPane.add(textArea); 
    } 
} 

非常感謝您的幫助。

編輯:我想有這樣的事情在這樣的畫面:(在Photoshop製造)

enter image description here

回答

2

除非你知道如何寫一個Swing GUI程序,忘記了框架,只需使用一個JOptionPane ,如果你想要做的只是把文本放入一個textField中。試試這個:

public static void main(String[] args) { 

    System.out.println("How old are you?"); 

    Scanner scanner = new Scanner(System.in); 
    int input = scanner.nextInt(); 

    System.out.println(""); 
    System.out.println("How many siblings do you have?"); 
    int input2 = scanner.nextInt(); 

    String s1 = "Thank you for your answer!"; 
    String s2 = "You are "+input+ " years old and you have "+input2+" 2siblings."; 

    JOptionPane.showMessageDialog(null, s1 + "\n" + s2); // pop up dialog 
} 

編輯:

public static void main(String[] args) { 

    String age = JOptionPane.showInputDialog(null, "How old are you?"); 
    System.out.println(age); 

    String siblings = JOptionPane.showInputDialog(null, "How many siblings do you have?"); 
    System.out.println(siblings); 

    String s1 = "Thank you for your answer!"; 
    String s2 = "You are "+age+ " years old and you have "+siblings+" 2siblings."; 

    JOptionPane.showMessageDialog(null, s1 + "\n" + s2); // pop up dialog 
} 

編輯2:

我不想爲你做,你監守不會學習的方式。所以這裏是我將要做的一步一步的邏輯。

  1. 有一個JTextArea顯示對話
  2. 有一個JTextField獲得輸入
  3. 有問題的陣列
  4. 的第一個問題
  5. 初始化文本區域獲取從輸入文本字段並將其附加到文本區域,但首先將其保存到變量
  6. 從數組中打印第二個文本區域。
  7. 從文本字段獲取下一個答案,將其保存到變量並打印到文本區域
  8. 組成最終字符串並將其附加到文本區域。

注意:每次用戶點擊輸入文本字段時,索引變量都應遞增,以獲取問題數組中的下一個索引。

+0

嗨,謝謝你的回答。但不是隻獲取最後一個MessageDialog文本,我需要在窗口中執行掃描器輸入。基本上,控制檯中發生的一切都應該發生在窗口的某個地方。 – medzi

+0

看我的編輯。測試一下。 –

+0

我編輯了我的問題。像這樣可行嗎? – medzi