显示 JTabbedPane 的第一个选项卡
我有一个 Jtabbedpane,其中包含 4 个选项卡(每个选项卡都是一个 Jpanel)。
当我从 netbeans 6.8 运行应用程序时,所选选项卡将与关闭应用程序之前选择的选项卡相同。
有没有办法在每次运行应用程序时选择第一个选项卡(索引 0)? 代码如下:
mainTabbedPanel = new javax.swing.JTabbedPane();
mainTabbedPanel.setName("mainTabbedPanel");
mainTabbedPanel.addTab(resourceMap.getString("panel1"), panel1);
mainTabbedPanel.addTab(resourceMap.getString("panel2"), panel2);
mainTabbedPanel.addTab(resourceMap.getString("panel3"), panel3);
mainTabbedPanel.addTab(resourceMap.getString("panel4"), panel4);
这些位于 netbeans 的方法 private void initComponents() 中 并在启动时(我的应用程序的构造函数)将调用 initComponents 。 我尝试在调用 initComponents() 之后放置 mainTabbedPanel.setSelectedIndex(0) 但没有成功。
I have a Jtabbedpane which contains 4 tabs (each one a Jpanel).
When I run the application from netbeans 6.8 the selected tab will be the same one as it was selected before closing the application.
Is there a way to select the first tab (index 0) each time I run my application?
Here is the Code:
mainTabbedPanel = new javax.swing.JTabbedPane();
mainTabbedPanel.setName("mainTabbedPanel");
mainTabbedPanel.addTab(resourceMap.getString("panel1"), panel1);
mainTabbedPanel.addTab(resourceMap.getString("panel2"), panel2);
mainTabbedPanel.addTab(resourceMap.getString("panel3"), panel3);
mainTabbedPanel.addTab(resourceMap.getString("panel4"), panel4);
Those are in the method private void initComponents() of netbeans
and on startup (constructor of my application) initComponents will be called.
I try to put mainTabbedPanel.setSelectedIndex(0) after calling initComponents() but didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,将选择第一个选项卡。如果发生除此之外的其他情况,则一定有代码在使用 setSelectedIndex(...) 来重置选项卡。您需要搜索生成的代码以找出其位置并删除该代码。
如果您不知道如何删除代码,那么您可以尝试在执行默认代码后重置索引。这是通过在 GUI 可见后使用 SwingUtilities.invokeLater 来完成的。你的代码会是这样的:
By default the first tab will be selected. If something other than this is happening then there must be code somewhere that is using the setSelectedIndex(...) to reset the tab. You need to search the generated code to find out where this is and remove the code.
If you don't know how to remove the code then you can try to reset the index after the default code is executing. This is done by using SwingUtilities.invokeLater after the GUI is visible. Your code would be something like: