简体   繁体   English

主屏幕上的JFrame复制

[英]JFrame duplication on main screen

My code is duplicating the creation of the main screen when I hit the button of the search..the search event should only open one new frame with a textbox to type query. 当我按下搜索按钮时,我的代码正在复制主屏幕的创建。搜索事件仅应打开一个带有文本框的新框架以键入查询。 The creation of another frame causes this duplication or is this some sort of bug? 创建另一个框架会导致此重复,还是某种错误? I tryed to use the mainFrame to upload the date of the searchPane (p1 in the code) but when I do this, this solves my problem of the window, but the defaultCloseOperation() on the Search Windows causes the close of the entire program - how may I solve this situtation? 我尝试使用mainFrame上载searchPane的日期(代码中为p1),但是当我这样做时,这解决了我的窗口问题,但是Search Windows上的defaultCloseOperation()导致整个程序关闭-我该如何解决这种情况?

thanks in advance 提前致谢

SearchScreen 搜索屏幕

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SearchScreen extends MainScreen{

    JButton btsearch;
    JLabel lbsearch;
    protected JTextField txtsearch;
    JPanel p1;
    protected JFrame searchFrame = new JFrame();

    public SearchScreen(){

    //Button Creation
        btsearch= new JButton("Search");

    //Label Creation
        lbsearch= new JLabel("Type Keywords in english to be searched below:");

    //TextBox   
        txtsearch= new JTextField();

    //Pane Creation 
        p1=new JPanel();
            p1.setBackground(Color.gray);

    //Pane Components
        p1.add(lbsearch);
            p1.add(txtsearch);
                p1.add(btsearch);

    //JFrame Layout Setup   
        p1.setLayout(new GridLayout(3,3));

            btsearch.setEnabled(true);

    //Adding JPaneel    
        searchFrame.add(p1);

    //JFrame Setup
        searchFrame.setTitle("SHST");
            searchFrame.setSize(400, 400);
                searchFrame.setVisible(true);
                    searchFrame.setDefaultCloseOperation(1);

    }

}

MainScreen 主屏幕

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class MainScreen implements ActionListener {


    JMenuBar bar;
    JMenu file, register;
    JMenuItem close, search;
    JPanel entrance = new JPanel();
    JFrame mainFrame = new JFrame();

    public MainScreen()
    {

        bar= new JMenuBar();
        file= new JMenu("File");
            register= new JMenu("Search");

        close= new JMenuItem("Close");
            close.addActionListener(this);

        search= new JMenuItem("Request Query");
            search.addActionListener(this);

        //mainFrame Setup
        bar.add(file);
            bar.add(register);
                file.add(close);
                    register.add(search);

            mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH); 
                mainFrame.setTitle("SHST");
                    mainFrame.setJMenuBar(bar);
                        mainFrame.setDefaultCloseOperation(0);
                            mainFrame.setVisible(true);

                             WindowListener J=new WindowAdapter(){
                                    public void windowClosing(WindowEvent e){
                                    System.exit(0);
                                    }
                                }; 

                                mainFrame.addWindowListener(J);

}

    public void actionPerformed(ActionEvent e){
        if(e.getSource()==close){
            System.exit(0);
    }

            if(e.getSource()==search){
                SearchScreen s= new SearchScreen();

            }

public static void main (String[] args){

        MainScreen m= new MainScreen();

    }

        }

Your issue is here 您的问题在这里

 if(e.getSource()==search){
                    SearchScreen s= new SearchScreen();

                }

Using inheritance implementation is dangereous, a SearchScreen is a MainScreen then the constructor of this class is always called. 使用继承实现是危险的,SearchScreen是MainScreen然后始终调用此类的构造函数。

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

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