简体   繁体   中英

JFrame not showing up

I have one class called TabBuilder which I use to create the interface of my application, but I'm going through a weird problem. If I try to run the code as it's below it will draw the mainScreen but if I request the SearchScreen from the BarMenu it doesn't show up. If I try to execute only SearchScreen by itself (calling its builder) within the public static void main (String[] args) MainString it wont show up too. but If go to the request event and tip TabBuilder tb = new TabBuilder(); tb.requestTab(); TabBuilder tb = new TabBuilder(); tb.requestTab(); the screen will show up as it should be. So, what might be wrong? Thanks in advance

MainScreen:

public class MainScreen{

    public MainScreen()
    {
        TabBuilder tb = new TabBuilder();
        tb.mainTab();   
  }
}

SearchScreen:

public class SearchScreen{

    public void SearchScreen(){

        TabBuilder tb = new TabBuilder();
        tb.requestTab();        

    }
}

TabBuilder:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TabBuilder implements ActionListener {

    JTabbedPane tabbedPane = new JTabbedPane();
    JMenuItem close, request;
    protected JTextField txtrequest = new JTextField();
    JButton btrequest = new JButton();
    protected JFrame requestFrame = new JFrame();

    public void TabBuilder(){

    }

    public void mainTab(){

        JMenuBar bar;
        JMenu file, register;
        JFrame mainFrame = new JFrame();

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

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

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

        bar.add(file);
        bar.add(register);
        file.add(close);
        register.add(request);

        mainFrame.setExtendedState(mainFrame.getExtendedState() | mainFrame.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize());
        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 requestTab(){
        JLabel lbrequest;
        JPanel requestPane;

        btrequest= new JButton("request");
        lbrequest= new JLabel("Type Keywords in english to be requested below:");
        txtrequest= new JTextField();
        requestPane=new JPanel();
        requestPane.setBackground(Color.gray);
        requestPane.add(lbrequest);
        requestPane.add(txtrequest);
        requestPane.add(btrequest);
        requestPane.setLayout(new GridLayout(3,3));
        btrequest.setEnabled(true);

        requestFrame.add(requestPane);
        requestFrame.setTitle("SHST");
        requestFrame.setSize(400, 400);
        requestFrame.setVisible(true);
        requestFrame.setDefaultCloseOperation(1);
    }

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

        if(e.getSource()==request){
            TabBuilder tb = new TabBuilder();
            tb.requestTab();
        }
    } 

    public static void main (String[] args){
        MainScreen m = new MainScreen();
    }
}

The constructor of SearchScreen was settled as void . That caused nothing to return as object when calling the constructor. Newbie failure but simple solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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