通过事件通过JTabbedPane添加新标签页

问题描述 投票:1回答:1

我这里有两个类:MainScreen和QueryScreen。 MainScreen已经在int上实现了一个JTabbedPane。 QueryScreen扩展了MainScreen。

我试图添加一个通过QueryScreen调用一个事件的选项卡,但该事件未在应用程序中显示。请结帐示例代码:

QueryScreen:

 public class QueryScreen extends MainScreen {

        private JSplitPane engineList;
        final JPanel queryList = new JPanel();

        public QueryScreen(){

            tabbedPane.addTab( "Query List", queryList );
            add( tabbedPane, BorderLayout.CENTER );

        }
    }

主屏幕:

 public class MainScreen extends JFrame implements ActionListener {
        /**
         * 
         */
        JMenuBar bar;
        JMenu file, register;
        JMenuItem close, search;
        ImageIcon image1= new ImageIcon("rsc/img/logo.jpg");
        JLabel lbImage1;
        JTabbedPane tabbedPane = new JTabbedPane();
        final JPanel entrance = new JPanel();


        /**
         * 
         */

public MainScreen()
        {           
                lbImage1= new JLabel(image1, JLabel.CENTER);
            entrance.add(lbImage1);
            tabbedPane.addTab( "Entrance", entrance );
            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);




            //Ibimage1.setVerticalTextPosition(SwingConstants.CENTER);

            bar.add(file);
            bar.add(register);
            file.add(close);
            register.add(search);
            setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize());
            setTitle("SHST");
            setJMenuBar(bar);
            setDefaultCloseOperation(0);

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

            if(e.getSource()==search){
                Search s= new Search();
                s.setVisible(true);
            }

            }
    }

ps:MainScreen对象及其中的setVisible来自运行类,该类仅具有对此MainScreen的调用。

我如何添加此新标签页?

提前感谢

编辑一个:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS94RnR6Zy5wbmcifQ==” alt =“在此处输入图像描述”>

java swing jframe jpanel jtabbedpane
1个回答
3
投票

将来,请发布SSCCE,而不是复制/粘贴某些课程。

这是MainScreen的SSCCE,去除了不必要的内容,并添加了main方法:

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

public class MainScreen extends JFrame
{
  JTabbedPane tabbedPane = new JTabbedPane();
  final JPanel entrance = new JPanel();

  public MainScreen()
  {
    tabbedPane.addTab("Entrance", entrance);
    add(tabbedPane, BorderLayout.CENTER);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        JFrame frame = new MainScreen();
        frame.setSize(300, 200);
        frame.setVisible(true);
      }
    });
  }
}

...这是QueryScreen的SSCCE:

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

public class QueryScreen extends MainScreen
{
  final JPanel queryList = new JPanel();

  public QueryScreen()
  {
    tabbedPane.addTab("Query List", queryList);
    //add( tabbedPane, BorderLayout.CENTER );    /* not needed */
  }

  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        JFrame frame = new QueryScreen();
        frame.setSize(300, 200);
        frame.setVisible(true);
      }
    });
  }
}

如您所见,这行得通,而且在大多数情况下,我所做的只是删除不必要的代码,并为每个代码添加了一个主代码。

如果仍然有问题,请使用SSCCE更新您的问题,并发布您遇到的具体问题。

© www.soinside.com 2019 - 2024. All rights reserved.