当第二次调用jframe类时,它复制它但没有jbuttons,jtextarea,jtextfield等只是jframe背景

时间:2015-12-30 07:19:19

标签: java swing jframe

我正在开展一个学校项目,我遇到一个问题,当我打电话给JFrame课时,每当我在中调用 simSimi.java 时mainMenu.java 起初它会完美地调用它而没有任何问题,但第二次调用 simSimi.java mainMenu.java 时它会产生一个副本框架,但没有按钮,textarea等只是框架和背景,如果你帮助我解决这个问题,它真的会帮助我很多,谢谢:)

这是我调用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);
    }

}

1 个答案:

答案 0 :(得分:0)

您需要做的第一件事就是停止制作所有内容static。在static扩展的任何类中删除所有JFrame个关键字。