Victor Oliveira
Victor Oliveira

Reputation: 3713

JFrame doesn't add TabbedPane

My JFrame is not adding the JTabbedPane and I don't know if the crash is some sort of bug of my eclipse. There are no syntaxes errors or anything that seems to be to me wrong. Could anyone else try to run it and see if it works? The code is already ready to run. Thanks in advance

public class MainScreen extends JFrame implements ActionListener {

    JMenuBar bar;
    JMenu file, register;
    JMenuItem close, search;
    ImageIcon logo= new ImageIcon("rsc/img/sh-logo.jpg");
    ImageIcon worldIcon= new ImageIcon("rsc/img/world-icon.png");
    JLabel lbImage1;
    JTabbedPane tabbedPane = new JTabbedPane();
    JPanel entrance = new JPanel();

    public MainScreen()
    {
        JFrame mainFrame = new JFrame();

        lbImage1= new JLabel(logo, JLabel.CENTER);
        entrance.add(lbImage1);
        tabbedPane.addTab("SHST", worldIcon, entrance);
        mainFrame.add( tabbedPane, BorderLayout.CENTER);

        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);

        //Keyboard Shortcut
        register.setMnemonic(KeyEvent.VK_S);
        file.setMnemonic(KeyEvent.VK_F);
        search.setMnemonic(KeyEvent.VK_R);

        //mainFrame Setup
        bar.add(file);
        bar.add(register);
        file.add(close);
        register.add(search);
        mainFrame.add(bar);
        mainFrame.setExtendedState(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);
            }
        }; 

        addWindowListener(J);
}

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

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

Upvotes: 0

Views: 1311

Answers (2)

Ingo Leonhardt
Ingo Leonhardt

Reputation: 9894

Yout must not add the JTabbedPane directly into your JFrame (mainFrame.add(tabbedPane,...)) but add it do the contentPane instead: mainFrame.getContentPane().add( tabbedPane, ...)

Upvotes: 0

vels4j
vels4j

Reputation: 11298

You've added JMenuBar in Content pane. It is not required.

remove this line in your code mainFrame.add(bar); and mainFrame.setJMenuBar(bar); is already added.

Upvotes: 3

Related Questions