我正在为学校制作一个像Cleverbot这样的自动聊天客户端。我有两个问题... 1)滚动条似乎由于某种原因不起作用。这是一个截图:
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements KeyListener{
//Main method
public static void main(String[] args){
new ChatBot();
}
//Swing settings
JPanel window=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
Image background = new ImageIcon("textEffect.png").getImage();
int x = (window.getWidth() - background.getWidth(null)) / 2;
int y = (window.getHeight() - background.getHeight(null)) / 2;
g.drawImage(background,x,y,null,this);
}
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea();
JTextField input=new JTextField(46);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
//Makes window and starts bot
public ChatBot(){
super("Pollockoraptor");
setSize(600,400);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setLineWrap(true);
dialog.setOpaque(false);
scroll.getViewport().setOpaque(false);
input.addKeyListener(this);
window.add(scroll);
window.add(label);
window.add(input);
//background color; new Color(97, 118, 131) is a nice color
window.setBackground(new Color(255, 255, 255));
add(window);
setVisible(true);
//Gui Layout
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//Dialog
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10, 10, 0, 10);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
c.gridheight = 2;
window.add(dialog, c);
//Input box
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 0, 10, 10);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = GridBagConstraints.REMAINDER;
window.add(input, c);
//Label
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 10, 10, 0);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
window.add(label, c);
input.requestFocus();
}
//knowledgeBase
String[][] knowledgeBase={
{"hi","hello","howdy","hey"},
{"hi","hello","hey"},
{"how are you", "how r u", "how r you", "how are u"},
{"good","doing well"},
{"shut up","noob","stop talking"}
};
//What to do when enter is pressed
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(false);
//get the user input
String quote=input.getText();
input.setText("");
if(!quote.equals("")){
addText("You:\t"+quote);
quote.trim();
while(quote.charAt(quote.length()-1)=='!' || quote.charAt(quote.length()-1)=='.' || quote.charAt(quote.length()-1)=='?'){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
int j=0;
//check the knowledgeBase for a match or change topic
while(response==0){
//if a match is found, reply with the answer
if(inArray(quote.toLowerCase(),knowledgeBase[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*knowledgeBase[(j*2)+1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[(j*2)+1][r]);
}
j++;
//if a match is not found, go to change topic
if(j*2==knowledgeBase.length-1 && response==0){
response=1;
}
}
//change topic if bot is lost
if(response==1){
int r=(int)Math.floor(Math.random()*knowledgeBase[knowledgeBase.length-1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[knowledgeBase.length-1][r]);
}
addText("\n");
}
}
}
//other events
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(true);
}
}
//format the input
public void addText(String str){
dialog.setText(dialog.getText()+str);
}
//check the knowledgeBase for a match
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}
我还有其他一切工作,但我还需要一种方法来建立一个我可以轻松编辑的响应数据库。我该怎么做?我是否必须使用MySQL或类似的东西?有没有更简单的方法可以做到这一点,我可以通过阅读文本文件制作类似于我的矩阵?
*************** EDIT ****************
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements KeyListener{
//Main method
public static void main(String[] args){
new ChatBot();
}
//Swing settings
JPanel window=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
Image background = new ImageIcon("textEffect.png").getImage();
int x = (window.getWidth() - background.getWidth(null)) / 2;
int y = (window.getHeight() - background.getHeight(null)) / 2;
g.drawImage(background,x,y,null,this);
}
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea(5,30);
JTextField input=new JTextField(46);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
//Makes window and starts bot
public ChatBot(){
super("Pollockoraptor");
setSize(600,400);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setLineWrap(true);
dialog.setOpaque(false);
scroll.getViewport().setOpaque(false);
input.addKeyListener(this);
window.add(scroll);
//background color; new Color(97, 118, 131) is a nice color
window.setBackground(new Color(255, 255, 255));
add(window);
setVisible(true);
//Gui Layout
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//Dialog
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10, 10, 0, 10);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
c.gridheight = 2;
window.add(scroll, c);
//Input box
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 0, 10, 10);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = GridBagConstraints.REMAINDER;
window.add(input, c);
//Label
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 10, 10, 0);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
window.add(label, c);
input.requestFocus();
}
//knowledgeBase
String[][] knowledgeBase={
{"hi","hello","howdy","hey"},
{"hi","hello","hey"},
{"how are you", "how r u", "how r you", "how are u"},
{"good","doing well"},
{"shut up","noob","stop talking"}
};
//What to do when enter is pressed
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(false);
//get the user input
String quote=input.getText();
input.setText("");
if(!quote.equals("")){
addText("You:\t"+quote);
quote.trim();
while(quote.charAt(quote.length()-1)=='!' || quote.charAt(quote.length()-1)=='.' || quote.charAt(quote.length()-1)=='?'){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
int j=0;
//check the knowledgeBase for a match or change topic
while(response==0){
//if a match is found, reply with the answer
if(inArray(quote.toLowerCase(),knowledgeBase[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*knowledgeBase[(j*2)+1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[(j*2)+1][r]);
}
j++;
//if a match is not found, go to change topic
if(j*2==knowledgeBase.length-1 && response==0){
response=1;
}
}
//change topic if bot is lost
if(response==1){
int r=(int)Math.floor(Math.random()*knowledgeBase[knowledgeBase.length-1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[knowledgeBase.length-1][r]);
}
addText("\n");
}
}
}
//other events
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(true);
}
}
//format the input
public void addText(String str){
dialog.append(str);
}
//check the knowledgeBase for a match
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}
*************** EDIT2 ****************
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements ActionListener{
//Main method
public static void main(String[] args){
new ChatBot();
}
//Swing settings
JPanel window=new JPanel(){
protected void paintComponent(Graphics g){
super.paintComponent(g);
Image background = new ImageIcon("textEffect.png").getImage();
int x = (window.getWidth() - background.getWidth(null)) / 2;
int y = (window.getHeight() - background.getHeight(null)) / 2;
g.drawImage(background,x,y,null,this);
}
};
JLabel label=new JLabel("Say: ");
JTextArea dialog=new JTextArea(5,30);
JTextField input=new JTextField(46);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
//Makes window and starts bot
public ChatBot(){
super("Pollockoraptor");
setSize(600,400);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setLineWrap(true);
dialog.setOpaque(false);
scroll.getViewport().setOpaque(false);
input.addActionListener(this);
window.add(scroll);
//background color; new Color(97, 118, 131) is a nice color
window.setBackground(new Color(255, 255, 255));
add(window);
setVisible(true);
//Gui Layout
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//Dialog
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10, 10, 0, 10);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
c.gridheight = 2;
window.add(scroll, c);
//Input box
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 0, 10, 10);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = GridBagConstraints.REMAINDER;
window.add(input, c);
//Label
c.weightx = 0;
c.weighty = 0;
c.anchor = GridBagConstraints.PAGE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, 10, 10, 0);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
window.add(label, c);
input.requestFocus();
}
//knowledgeBase
String[][] knowledgeBase={
{"hi","hello","howdy","hey"},
{"hi","hello","hey"},
{"how are you", "how r u", "how r you", "how are u"},
{"good","doing well"},
{"shut up","noob","stop talking"}
};
//What to do when enter is pressed
public void actionPerformed(ActionEvent e){
//get the user input
String quote=input.getText();
input.setText("");
if(!quote.equals("")){
addText("You:\t"+quote);
quote.trim();
while(quote.charAt(quote.length()-1)=='!' || quote.charAt(quote.length()-1)=='.' || quote.charAt(quote.length()-1)=='?'){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
int j=0;
//check the knowledgeBase for a match or change topic
while(response==0){
//if a match is found, reply with the answer
if(inArray(quote.toLowerCase(),knowledgeBase[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*knowledgeBase[(j*2)+1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[(j*2)+1][r]);
}
j++;
//if a match is not found, go to change topic
if(j*2==knowledgeBase.length-1 && response==0){
response=1;
}
}
//change topic if bot is lost
if(response==1){
int r=(int)Math.floor(Math.random()*knowledgeBase[knowledgeBase.length-1].length);
addText("\nPollockoraptor:\t"+knowledgeBase[knowledgeBase.length-1][r]);
}
addText("\n");
}
}
//format the input
public void addText(String str){
dialog.append(str);
}
//check the knowledgeBase for a match
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}
答案 0 :(得分:1)
)滚动条似乎无法正常工作
window.add(dialog, c);
您需要将滚动窗格添加到窗口,而不是dialaog。此外,当您创建对话框时,您应该使用以下内容:
JTextArea dialog = new JTextArea(5, 30);
因此可以以合理的大小创建文本区域。
如果(e.getKeyCode()== KeyEvent.VK_ENTER){
不要使用KeyListener来侦听Enter键。而是将ActionListener添加到文本字段。按下Enter键时将调用ActionListener。另外,为什么要切换文本字段的可编辑状态?没有必要这样做。
dialog.setText(dialog.getText()+ STR);
不要这样做以在文本区域添加文本。只需使用文本区域的append(...)
方法。
答案 1 :(得分:1)
很抱歉直言不讳,但你的问题只是一个草率的代码。
您将对话框两次添加到窗口容器中,一次在JScrollPane中,一次添加。
public ChatBot() {
super("Pollockoraptor");
setSize(600, 400);
setResizable(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
dialog.setLineWrap(true);
dialog.setOpaque(false);
scroll.getViewport().setOpaque(false);
input.addKeyListener(this);
// **** adding a bunch of junk **without** constraings??
window.add(scroll); // *** add scrollpane *with* dialog here
window.add(label);
window.add(input);
// .....
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
// Dialog
c.weightx = 1.0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(10, 10, 0, 10);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 3;
c.gridheight = 2;
window.add(dialog, c); // *** then add dialog by itself*** ???? WTF???
不要那样做。而是将其添加到JScrollPane,将JScrollPane添加到容器 with GridBagConstraints ,并保留它。
你有几个其他的组件,你正在添加到窗口没有GridBagConstraints ,几乎没有想到,几乎就像你没有计划(?)随机和懒散的编码。不要这样做。停止,首先计划您想要编码的内容,然后再创建代码。不要自由思考它,因为它是草率的,永远不会工作。诚实的错误很好,但是编码很乱,没有。