2015-12-30 160 views
0

我目前工作的一个学校项目,我遇到了我所说的JFrame时出现的问题类,每当我打电话simSimi.javamainMenu.java首先它会完美地称它为没有任何问题,但第二次我打电话simSimi.javamainMenu.java它产生的一个副本框架,但没有按钮,textarea等只是框架和背景它真的会帮助我很多,如果你帮助我这个发生的问题,谢谢:)调用JFrame类它重复,但第二次时,没有Jbuttons中,JTextArea,使其的JTextField等只是JFrame的背景

这里为mainMenu,我叫simSimi

//if(e.getSource()==play){ 
     simSimi ss = new simSimi(); 
     ss.setVisible(true); 
     ss.setSize(420,500); 
     ss.setLocation(300, 154); 
     ss.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     ss.setResizable(false); 
     this.setVisible(false); 
     dispose(); 
    } 

这里的simSimi这里我称之为MAINMENU当退出按钮被按下

//if(e.getSource==exitButton){ 
mainMenu MM = new mainMenu(); 
MM.setVisible(true); 
MM.setSize(719,480); 
MM.setLocation(300, 154); 
MM.setResizable(false); 
MM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
this.setVisible(false); 
this.dispose(); 
} 

here's the simSimi

enter image description here

代码的代码

enter image description here here's the mainMenu

这里的simSimi源代码

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package aichat; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JRootPane; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.swing.ScrollPaneConstants; 
import javax.swing.SwingUtilities; 

/** 
* 
* @author MASTER 
*/ 
public class simSimi extends JFrame implements ActionListener, KeyListener { 
    static JTextArea chatBox = new JTextArea(); 
    static JTextField msgBox = new JTextField(); 
    static ImageIcon restartIcon = new ImageIcon("restartImage.png"); 
    static Icon sendButtonImage = new ImageIcon("sendImage.png") {}; 
    static Icon teachImage = new ImageIcon("teachImage.gif"); 
    static Icon exitImage = new ImageIcon("exitImage.png"); 
    static JButton sendButton = new JButton(sendButtonImage); 
    static JButton exitButton = new JButton(exitImage); 
    static JButton restartButton= new JButton(restartIcon); 
    static JButton teachButton = new JButton(teachImage); 
    static Boolean AnswerFound; 
    static JScrollPane scroll = new JScrollPane(chatBox); 



    public simSimi() throws IOException{ 
    super("AIBot"); 
    setLayout(null); 

    JRootPane rootPnae = SwingUtilities.getRootPane(sendButton); 
    rootPane.setDefaultButton(sendButton); 
    mainMenu.whoOpenWho=0; 
    chatBox.setEditable(false); 
    chatBox.setLineWrap(true); 
    chatBox.setWrapStyleWord(true); 

    setContentPane(new JLabel(new ImageIcon 
     ("simSimiBackground.png"))); 

    scroll.setBounds(5,5,320,420); 
    msgBox.setBounds(5,430,320,30); 
    teachButton.setBounds(330,5,73,75); 
    restartButton.setBounds(330,85,73,75); 
    exitButton.setBounds(330,165,73,75); 
    sendButton.setBounds(330,430,73,30); 


    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
    add(exitButton); 
    add(msgBox); 
    add(sendButton); 
    add(scroll); 
    add(teachButton); 
    add(restartButton); 

    msgBox.addKeyListener(this); 
    sendButton.addActionListener(this); 
    restartButton.addActionListener(this); 
    teachButton.addActionListener(this); 
    exitButton.addActionListener(this); 
    } 
    @Override 
    public void keyTyped(KeyEvent e){ 
     //not used 
    } 
    @Override 
    public void keyPressed(KeyEvent e){ 
     int keyCode = e.getKeyCode(); 
     if(keyCode==KeyEvent.VK_ENTER){ 
     if(e.getSource()==sendButton){ 
      int i=0; 
      int x=0; 

      String Question[]= new String[9999999]; 
      String Answer[]=new String[9999999]; 
      String UserMessage; 
      String UserName="JohnAlbert"; 
      String BotName="Piyaya"; 


      if(msgBox.getText().isEmpty()){ 
      JOptionPane.showMessageDialog(this, "msgBox must not be empty!" 
          , "WARNING", JOptionPane.WARNING_MESSAGE); 
      } 
      else{ 
      UserMessage = msgBox.getText(); 
      msgBox.setText(""); 
      chatBox.setText(chatBox.getText()+UserName+" : "+UserMessage+"\n"); 
      try{//file handling stufff e.g. opening files and assigning some pre defined function 
      BufferedReader QuestionBR = new BufferedReader(new FileReader("Questions.txt")); 
      BufferedReader AnswerBR= new BufferedReader(new FileReader("Answer.txt")); 

      File QuestionFile =new File("Questions.txt"); 
      File AnswerFile = new File("Answer.txt"); 
      FileWriter QfileWritter = new FileWriter(QuestionFile.getName(),true); 
      BufferedWriter QbufferWritter = new BufferedWriter(QfileWritter); 
      FileWriter AfileWritter = new FileWriter(AnswerFile.getName(),true); 
      BufferedWriter AbufferWritter = new BufferedWriter(AfileWritter); 


       //transfer the question.txt to array 
      while((Question[i] = QuestionBR.readLine())!=null){ 
       i++; 
      } 
      i=0; 
       //transfer the answer.txt to array 
      while((Answer[i] = AnswerBR.readLine())!=null){ 
       i++; 
      } 
      i=0; 
      //find if question has a answer 
      while(Question[x]!=null){ 
       if(Question[x].toLowerCase().equals(UserMessage.toLowerCase())){ 
       AnswerFound=true; 
       break; 
       } 
       else{ 
        AnswerFound=false; 
       } 
       x++; 
      } 

      if(AnswerFound==true){ 
       chatBox.setText(chatBox.getText()+BotName+" : "+Answer[x]+"\n"); 
      } 
      else{ 
       chatBox.setText(chatBox.getText()+BotName+" : What?\n"); 
       //simismi will response beyond its queries 
       if(JOptionPane.showConfirmDialog(this, "Simsimi: I dont know about "+UserMessage+ 
       ".\nDo you want me to learn this?", "Teach Me", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)==0){ 
        AbufferWritter.write("\r\n"+JOptionPane.showInputDialog(null,"And my answer is...")); 
        QbufferWritter.write("\r\n"+UserMessage); 
        QbufferWritter.close(); 
        AbufferWritter.close(); 
        msgBox.setText(""); 
       } 
      } 

      //catch all errors  
      }catch(FileNotFoundException FF){ 
      JOptionPane.showMessageDialog(null,FF); 
      }catch(IOException IO){ 
      JOptionPane.showMessageDialog(null,IO);  
      } 
      } 

     }// if(e.getSource()==sendButton){ 

     }//if(keyCode==KeyEvent.VK_ENTER){ 


    }// public void keyPressed(KeyEvent e){ 



    @Override 
    public void keyReleased(KeyEvent txt){ 
     //not used 
    } 
    @Override 
    public void actionPerformed(ActionEvent e){ 

     try{//file handling stufff e.g. opening files and assigning some pre defined function 

     if(e.getSource()==sendButton){ 
      int i=0; 
      int x=0; 

      String Question[]= new String[9999999]; 
      String Answer[]=new String[9999999]; 
      String UserMessage; 
      String UserName="JohnAlbert"; 
      String BotName="Piyaya"; 


      if(msgBox.getText().isEmpty()){ 
      JOptionPane.showMessageDialog(this, "msgBox must not be empty!" 
          , "WARNING", JOptionPane.WARNING_MESSAGE); 
      } 
      else{ 
      UserMessage = msgBox.getText(); 
      msgBox.setText(""); 
      chatBox.setText(chatBox.getText()+UserName+" : "+UserMessage+"\n"); 

      BufferedReader QuestionBR = new BufferedReader(new FileReader("Questions.txt")); 
      BufferedReader AnswerBR= new BufferedReader(new FileReader("Answer.txt")); 

      File QuestionFile =new File("Questions.txt"); 
      File AnswerFile = new File("Answer.txt"); 
      FileWriter QfileWritter = new FileWriter(QuestionFile.getName(),true); 
      BufferedWriter QbufferWritter = new BufferedWriter(QfileWritter); 
      FileWriter AfileWritter = new FileWriter(AnswerFile.getName(),true); 
      BufferedWriter AbufferWritter = new BufferedWriter(AfileWritter); 


       //transfer the question.txt to array 
      while((Question[i] = QuestionBR.readLine())!=null){ 
       i++; 
      } 
      i=0; 
       //transfer the answer.txt to array 
      while((Answer[i] = AnswerBR.readLine())!=null){ 
       i++; 
      } 
      i=0; 
      //find if question has a answer 
      while(Question[x]!=null){ 
       if(Question[x].toLowerCase().equals(UserMessage.toLowerCase())){ 
       AnswerFound=true; 
       break; 
       } 
       else{ 
        AnswerFound=false; 
       } 
       x++; 
      } 

      if(AnswerFound==true){ 
       chatBox.setText(chatBox.getText()+BotName+" : "+Answer[x]+"\n"); 
      } 
      else{ 
       chatBox.setText(chatBox.getText()+BotName+" : What?\n"); 
       //simismi will response beyond its queries 
       if(JOptionPane.showConfirmDialog(this, "Simsimi: I dont know about "+UserMessage+ 
       ".\nDo you want me to learn this?", "Teach Me", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)==0){ 
        AbufferWritter.write("\r\n"+JOptionPane.showInputDialog(null,"And my answer is...")); 
        QbufferWritter.write("\r\n"+UserMessage); 
        QbufferWritter.close(); 
        AbufferWritter.close(); 
        msgBox.setText(""); 
       } 
      } 


      } 

     }// if(e.getSource()==sendButton){ 


     else if(e.getSource()==restartButton){ 
      chatBox.setText(" "); 

     }else if(e.getSource()==teachButton){ 
      /* 
      teach tc = new teach(); 
      tc.setVisible(true); 
      tc.setSize(325,450); 
      tc.setLocation(300,154); 
      tc.setResizable(false); 
      tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      simSimi ss = new simSimi(); 
      ss.setVisible(false); 
      ss.dispose(); 
      */ 
     }else if(e.getSource()==exitButton){ 
       mainMenu MM = new mainMenu(); 
       MM.setVisible(true); 
       MM.setSize(719,480); 
       MM.setLocation(300, 154); 
       MM.setResizable(false); 
       MM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       this.setVisible(false); 
       this.dispose(); 
     } 
     //catch all errors  
      }catch(FileNotFoundException FF){ 
      JOptionPane.showMessageDialog(null,FF); 
      }catch(IOException IO){ 
      JOptionPane.showMessageDialog(null,IO);  
      }   
    } 


    public static void main(String args[]) throws IOException{ 
    simSimi ss = new simSimi(); 
    ss.setVisible(true); 
    ss.setSize(420,500); 
    ss.setLocation(300, 154); 
    ss.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    ss.setResizable(false);  

     } 


    } 

这里的MAINMENU源代码

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package aichat; 

import java.awt.event.*; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import javax.swing.*; 

/** 
* 
* @author MASTER 
*/ 

public class mainMenu extends JFrame implements ActionListener { 

    static JButton play = new JButton("Play"); 
    static JButton settings = new JButton("Settings"); 
    static JButton teach = new JButton("Teach"); 
    static JButton exit = new JButton("Exit"); 
    static int whoOpenWho=1; 

    public mainMenu()throws MalformedURLException{ 
     super("Main Menu"); 
     setLayout(null); 
      setContentPane(new JLabel(new ImageIcon 
     ("mainMenuBackground.jpg"))); 


     play.setBounds(200,150,100,50); 
     teach.setBounds(200,220,100,50); 
     settings.setBounds(400,150,100,50); 
     exit.setBounds(400,220,100,50); 
     add(play); 
     add(settings); 
     add(teach); 
     add(exit); 

     play.addActionListener(this); 
     teach.addActionListener(this); 
     settings.addActionListener(this); 
     exit.addActionListener(this); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    try{ 

     if(e.getSource()==play){ 
      simSimi ss = new simSimi(); 
      ss.setVisible(true); 
      ss.setSize(420,500); 
      ss.setLocation(300, 154); 
      ss.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      ss.setResizable(false); 
      this.setVisible(false); 
      dispose(); 
     } 
     else if(e.getSource()==settings){ 

     } 
     else if(e.getSource()==teach){ 
      /* 
      teach tc = new teach(); 
      tc.setVisible(true); 
      tc.setSize(325,450); 
      tc.setLocation(300,154); 
      tc.setResizable(false); 
      tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      this.setVisible(false); 
      dispose(); 
      */ 
     } 
     else if(e.getSource()==exit){ 
      System.exit(0); 
     } 
     }catch(IOException IO){ 
     JOptionPane.showMessageDialog(null,IO); 
     } 
    } 

    public static void main(String args[])throws MalformedURLException{ 
     mainMenu MM = new mainMenu(); 
     MM.setVisible(true); 
     MM.setSize(719,480); 
     MM.setLocation(300, 154); 
     MM.setResizable(false); 
     MM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

} 
+0

抱歉,这是我第一次张贴在这里:) –

+1

考虑提供一个[可运行示例](https://stackoverflow.com/help/mcve),这说明你的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混淆和更好的响应 – MadProgrammer

+2

也看看[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556)和[如何使用CardLayout] (http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)和[如何制作对话框](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog .html)一些替代品 – MadProgrammer

回答

0

你需要做的是停止生产一切static的第一件事。摆脱所有static关键字,您可以在任何延伸JFrame的课程中使用。

+0

感谢所有的帮助家伙我没有发现问题,所以我只是重新键入代码谢谢:)) –