JFrame不添加TabbedPane

时间:2013-07-09 11:09:47

标签: java swing jframe jtabbedpane

我的JFrame没有添加JTabbedPane,我不知道崩溃是否是我的日食的某种错误。没有语法错误或任何似乎对我错误的东西。任何人都可以尝试运行它,看看它是否有效?代码已准备好运行。提前致谢

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

2 个答案:

答案 0 :(得分:3)

您已在“内容”窗格中添加了JMenuBar。这不是必需的。

在代码mainFrame.add(bar);中删除此行,并且已添加mainFrame.setJMenuBar(bar);

答案 1 :(得分:0)

Yout不得将JTabbedPane直接添加到您的JFramemainFrame.add(tabbedPane,...))中,而是添加它来代替contentPane:mainFrame.getContentPane().add( tabbedPane, ...)