2014-10-04 42 views
1

我正在爲學校製作一個像Cleverbot這樣的自動聊天客戶端。我有2個問題... 1)滾動條似乎沒有工作出於某種原因。下面是截圖: enter image description hereJScrollPane滾動條在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

對不起,你的問題只是一個sl code的代碼。

您將對話框兩次添加到窗口容器中,一次在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中,使用GridBagConstraints將JScrollPane添加到容器,並將其留在那裏。

您有幾個其他組件可以添加到沒有GridBagConstraints的窗口中,幾乎沒有任何想法,就好像您在沒有計劃(?)的情況下隨機編碼一樣。不要這樣做。停下來,先計劃你想要編寫的代碼,然後才能創建你的代碼。不要隨意打字,因爲這是sl and的,永遠不會工作。誠實的錯誤是好的,但草率的編碼,不。

+0

+1注意到所有正在添加的「垃圾」沒有限制。這些代碼都不需要。 – camickr 2014-10-04 03:08:38

+0

@camickr:謝謝,我也立即回答你的問題。這個人只是隨便扔垃圾在牆上,看看有什麼東西粘在一起,這不是你如何成功創建一個程序。我很生硬,可能太直率了,但是這些錯誤太令人震驚,我無法自拔。 – 2014-10-04 03:09:25

+0

謝謝你,我現在想這個權利......我是新來擺動,所有這些東西,對於一些顯而易見的問題非常抱歉:) – 2014-10-04 03:47:48

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(...)方法即可。