2017-07-26 60 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