[英]Adding new Tab with JTabbedPane through event
I have two classes here: MainScreen and QueryScreen.我在这里有两个类:MainScreen 和 QueryScreen。 MainScreen has already implemented one JTabbedPane on int. MainScreen 已经在 int 上实现了一个 JTabbedPane。 QueryScreen extended the MainScreen. QueryScreen 扩展了 MainScreen。
I tried to add one tab calling one event through QueryScreen but its not coming up on the app.我试图添加一个选项卡,通过 QueryScreen 调用一个事件,但它没有出现在应用程序中。 Checkout please the sample code:请结帐示例代码:
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 );
}
}
MainScreen:主屏幕:
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: the MainScreen object and the setVisible from it is coming from the run class which has only the call for this MainScreen. ps:MainScreen 对象和它的 setVisible 来自运行类,它只调用这个 MainScreen。
How am I able to add this new tab?我怎样才能添加这个新标签?
Thanks in advance提前致谢
Edit One:编辑一:
In the future please post an SSCCE instead of copy/pasting some classes.将来请发布SSCCE而不是复制/粘贴某些课程。
Here's an SSCCE of your MainScreen, with the non-essentials stripped out, and a main method added:这是您的 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);
}
});
}
}
... and here's an SSCCE for QueryScreen: ...这是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);
}
});
}
}
As you can see, this works, and for the most part, all I did was remove unnecessary code and added a main to each.如您所见,这是有效的,而且在大多数情况下,我所做的只是删除了不必要的代码并为每个代码添加了一个 main。
If you're still having problems, please update your question with an SSCCE and post the specific problem you're having.如果您仍然遇到问题,请使用 SSCCE 更新您的问题并发布您遇到的具体问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.