簡體   English   中英

通過事件添加帶有 JTabbedPane 的新標簽

[英]Adding new Tab with JTabbedPane through event

我在這里有兩個類:MainScreen 和 QueryScreen。 MainScreen 已經在 int 上實現了一個 JTabbedPane。 QueryScreen 擴展了 MainScreen。

我試圖添加一個選項卡,通過 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。

我怎樣才能添加這個新標簽?

提前致謝

編輯一:

在此處輸入圖片說明

將來請發布SSCCE而不是復制/粘貼某些課程。

這是您的 MainScreen 的 SSCCE,去掉了非必需品,並添加了一個主要方法:

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

如您所見,這是有效的,而且在大多數情況下,我所做的只是刪除了不必要的代碼並為每個代碼添加了一個 main。

如果您仍然遇到問題,請使用 SSCCE 更新您的問題並發布您遇到的具體問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM