繁体   English   中英

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

[英]when calling a jframe class the second time it duplicates it but with no jbuttons,jtextarea,jtextfield etc just the jframe background

我目前正在对一所学校的项目和我遇到的时候我叫时发生的问题JFrame类,每当我打电话simSimi.javamainMenu.java首先它会完全把它没有任何问题,但第二次我将其称为simSimi.javamainMenu.java会生成框架的副本,但没有按钮,文本区域等,只是框架和背景,如果您能帮助我解决此出现的问题,它将对我有很多帮助:)

这里的代码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();
}

这是simSimi

在此处输入图片说明

在此处输入图片说明 这是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);
    }

}

您需要做的第一件事就是停止使所有内容保持static 摆脱所有可以在扩展JFrame类中使用的static关键字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM