我正在爲學校製作一個像Cleverbot這樣的自動聊天客戶端。我有2個問題... 1)滾動條似乎沒有工作出於某種原因。下面是截圖: JScrollPane滾動條在GridBagLayout之後並不起作用
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或類似的東西嗎?有沒有更簡單的方法可以做到這一點,我可以通過閱讀文本文件來製作一個類似於我所擁有的矩陣?
***************編輯****************
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;
}
}
+1注意到所有正在添加的「垃圾」沒有限制。這些代碼都不需要。 – camickr 2014-10-04 03:08:38
@camickr:謝謝,我也立即回答你的問題。這個人只是隨便扔垃圾在牆上,看看有什麼東西粘在一起,這不是你如何成功創建一個程序。我很生硬,可能太直率了,但是這些錯誤太令人震驚,我無法自拔。 – 2014-10-04 03:09:25
謝謝你,我現在想這個權利......我是新來擺動,所有這些東西,對於一些顯而易見的問題非常抱歉:) – 2014-10-04 03:47:48