2017-07-26 40 views
0

我目前正在爲我的學校作業開發聊天機器人。我完成了整個聊天部分,使用掃描儀進行用戶輸入,並使用System.out.println()顯示對話。 現在我想爲chatbot實現一個GUI。我得到了一個非常簡單的GUI,分別是JTextField和JTextArea分別是輸入框和顯示框。在Java中重定向輸入和輸出

但現在我完全無法知道如何將它們連接在一起。 這就像掃描儀,而不是System.in,將讀取來自JTextField的輸入,而不是在控制檯中顯示輸出,將它們顯示在JTextArea中。

任何人都可以幫助我嗎?像我應該學會如何將chatbot和GUI鏈接在一起?

如果你想看看我的GUI代碼,它是如下:

public class GUI_V2 extends JFrame { 

private JTextField txtEnter = new JTextField(); 
//Chat area; 
private JTextArea txtChat = new JTextArea(); 

//Scroll 
private final JScrollPane scroll = new JScrollPane(txtChat); 


public GUI_V2(){ 
    //Frame Attributes 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setSize(2000,2000); 
    this.setVisible(true); 
    this.setResizable(false); 
    this.setLayout(null); 
    this.setTitle("Menu ChatBot"); 

    //textEnter Attributes 
    txtEnter.setLocation(20,1825); 
    txtEnter.setSize(1950,100); 
    txtEnter.setFont(new Font("Arial",Font.PLAIN,45)); 

    //txtChat Attributes 
    txtChat.setLocation(22,5); 
    txtChat.setSize(1950,1800); 
    txtChat.setFont(new Font("Arial",Font.BOLD,45)); 
    txtChat.setBackground(java.awt.Color.getHSBColor(0.4957f,0.0902f,1.0f)); 
    txtChat.setLineWrap(true); 
    txtChat.setWrapStyleWord(true); 
    txtChat.setEditable(false); 

    //scroll Attributes 
    scroll.setLocation(22,5); 
    scroll.setSize(1950,1800); 

    //Add Items To Frame 
    this.add(txtEnter); 
    this.add(scroll); 



    //txtEnter Action Event: 
    txtEnter.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent arg0){ 
      //add userInput into the txtChat 
      String uText = txtEnter.getText(); 
      txtChat.append("You" + ": " + uText + "\n"); 
      //auto scroll down 
      txtChat.setCaretPosition(txtChat.getDocument().getLength()); 
      //set the txtEnter field to be empty 
      txtEnter.setText(""); 
     } 
    }); 
} 
+0

好了,你的投入似乎恰到好處。可悲的是你不顯示你的輸出。 – XtremeBaumer

+0

所以你需要添加JTextField的值到JTextArea當按鈕點擊或焦點改變或什麼? – Blasanka

+0

沒有。GUI很好。我想將chatbot類的輸出指向GUI的JTextArea。並使用JTextField的輸入作爲我的聊天機器人的輸入 –

回答

0

不要設置佈局null和不使用setLocation()安排組件,而不是使用一個適當的佈局。請參考A Visual Guide to Layout Managers.

作爲一個例子你的情況,你可以使用BoxLayout(這不會安排你的組件正確bcuz BoxLayout拉伸組件可用空格):

變化:

this.setLayout(null); 

要:

this.setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); 

還需要添加main()

public static void main(String[] args) { 
    new GUI_V2(); 
} 

由於您已設置爲大尺寸,因此無法滾動到頂部看到JTextArea中的文字。

最後一件事,這裏沒有實際需要延伸JFrame。如果我必須重寫JFrame中的方法,我自己擴展JFrame。如果你不想覆蓋方法,你可以簡單地從JFrame創建對象。

這是我要做的事:

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class GUI_V2{ 

    public JFrame frame; 
    private JPanel panel; 
    private JTextField txtEnter; 
    //Chat area; 
    private JTextArea txtChat; 

    //Scroll 
    private JScrollPane scroll; 


    public GUI_V2(){ 
     initComp(); 
    } 

    public void initComp(){ 
     frame = new JFrame("Menu ChatBot"); 
     frame.setLayout(new CardLayout()); 

     panel = new JPanel(); 
     panel.setSize(1000, 1000); 
     panel.setLayout(new BorderLayout()); 

     txtEnter = new JTextField(); 
     //textEnter Attributes 
     txtEnter.setSize(1000,100); 
     txtEnter.setFont(new Font("Arial",Font.PLAIN,45)); 
     panel.add(txtEnter, BorderLayout.PAGE_START); 

     txtChat = new JTextArea(); 
     //txtChat Attributes 
     txtChat.setSize(1000,900); 
     txtChat.setFont(new Font("Arial",Font.BOLD,45)); 
     txtChat.setBackground(java.awt.Color.getHSBColor(0.4957f,0.0902f,1.0f)); 
     txtChat.setLineWrap(true); 
     txtChat.setWrapStyleWord(true); 
     txtChat.setEditable(false); 

     scroll = new JScrollPane(txtChat); 
     //scroll Attributes 
     scroll.setSize(1000,900); 
     panel.add(scroll, BorderLayout.CENTER); 

     frame.add(panel); 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //frame.setResizable(false); 
     frame.setVisible(true); 

     doAction(); 
    } 

    public void doAction(){ 
     //txtEnter Action Event: 
     txtEnter.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0){ 
       //add userInput into the txtChat 
       String uText = txtEnter.getText(); 
       txtChat.append("You" + ": " + uText + "\n"); 
       //auto scroll down 
       txtChat.setCaretPosition(txtChat.getDocument().getLength()); 
       //set the txtEnter field to be empty 
       txtEnter.setText(""); 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     new GUI_V2(); 
    } 
} 
+0

感謝您糾正我的錯誤。我對GUI仍然很陌生。但主要問題是如何將chatbot類的輸出(使用系統輸出行)重定向到JTextArea。並將JTextField的輸入引導至我的聊天室類 –

+0

中的掃描器,並說明其目的。我甚至無法正確理解你在說什麼。 – Blasanka