簡體   English   中英

重定向Java中的輸入和輸出

[英]Redirecting Input and Output in Java

我目前正在為我的學校作業使用聊天機器人。 我已經完成了整個聊天部分,使用Scanner進行用戶輸入,並使用System.out.println()顯示對話。 現在,我想為聊天機器人實現GUI。 我得到了一個非常簡單的GUI,其中的JTextField和JTextArea分別是輸入框和顯示框。

但是現在我對如何將它們鏈接在一起一無所知。 就像Scanner,而不是System.in,它將從JTextField讀取輸入,而不是在控制台中顯示輸出,而是在JTextArea中顯示它們。

有人可以幫我嗎? 就像我應該學習實現將聊天機器人和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("");
       }
   });
}

不要將layout設置為null ,也不要使用setLocation()來安排組件,而要使用正確的布局。 請參考《布局管理器視覺指南》。

作為您的方案的示例,您可以使用BoxLayout (由於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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM