I am building a Java Swing application, and am trying to use nested JTabbedPane components in it, and am having mixed results.
我正在构建一个Java Swing应用程序,并且我正在尝试使用嵌套的JTabbedPane组件,并且结果好坏参半。
In my top-level JFrame, I am able to successfully construct and load tabbed panes with JPanels. In the top level of the application, there are two sections, each assigned to a tab: General and Stream, both based on JPanels. Stream possesses its own JTabbedPane, as it is composed of three subordinate sections: Metadata, Input, and Instance, all also based on JPanels. All of the code written in the JFrame component works as expected. Tabs are initialized, JFrames are assigned to tabs, and it all displays correctly up to a point. Here is the code so far in my JFrame component:
在我的*JFrame中,我能够使用JPanel成功构建和加载选项卡式窗格。在应用程序的顶层,有两个部分,每个部分分配给一个选项卡:General和Stream,两者都基于JPanels。 Stream拥有自己的JTabbedPane,因为它由三个从属部分组成:元数据,输入和实例,所有这些都基于JPanels。编写在JFrame组件中的所有代码都按预期工作。选项卡已初始化,JFrames已分配给选项卡,并且所有选项卡都可正确显示到某个点。以下是我的JFrame组件中的代码:
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class AppFrame extends JFrame {
private static final long serialVersionUID = 5294140238998011273L;
private GeneralPanel generalPanel;
private JTabbedPane appTabs;
private JTabbedPane streamTabs;
private MetadataPanel metadataPanel;
private InputPanel inputPanel;
public AppFrame() {
super("Sourcecast");
setIconImage(Toolkit.getDefaultToolkit()
.getImage(getClass()
.getResource("/images/Apple-icon-64.png")));
setLayout(new BorderLayout());
appTabs = new JTabbedPane(JTabbedPane.TOP);
generalPanel = new GeneralPanel();
appTabs.add("General", generalPanel);
streamTabs = new JTabbedPane(JTabbedPane.TOP);
metadataPanel = new MetadataPanel();
streamTabs.add("Metadata", metadataPanel);
inputPanel = new InputPanel();
streamTabs.add("Input", inputPanel);
appTabs.add(streamTabs, "Stream");
add(appTabs);
setMinimumSize(new Dimension(500, 400));
setSize(600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
Notice that I added the Input JPanel to the tabs, and its tab showed up. I put a JLabel in my Input JPanel to test it and it was visible. However, my Input JPanel is also loading a set of tabs, and those tabs are not visible when I run the app. Here is the Input JPanel:
请注意,我将Input JPanel添加到选项卡,其选项卡显示出来。我在我的输入JPanel中放了一个JLabel来测试它,它是可见的。但是,我的输入JPanel也加载了一组选项卡,当我运行应用程序时,这些选项卡不可见。这是输入JPanel:
package gui;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class InputPanel extends JPanel {
private static final long serialVersionUID = 7603090258673682140L;
private JTabbedPane inputTabs;
private StdinPcmPanel stdinPcmPanel;
public InputPanel() {
setLayout(null);
inputTabs = new JTabbedPane(JTabbedPane.TOP);
stdinPcmPanel = new StdinPcmPanel();
inputTabs.add("StdinPCM", stdinPcmPanel);
add(inputTabs);
}
}
Below is one of the JPanels I am trying to load in the Input panel, and I have removed the other tabs in Input, just for simplicity:
下面是我试图在Input面板中加载的JPanel之一,我已经删除了Input中的其他选项卡,只是为了简单起见:
package gui;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
public class StdinPcmPanel extends JPanel {
private static final long serialVersionUID = -916207698149110923L;
private JComboBox<CbOption> samplerate;
private JComboBox<CbOption> channels;
private JComboBox<CbOption> metadata;
private JTextField metadatafilename;
public StdinPcmPanel() {
setLayout(null);
JLabel lblSampleRate = new JLabel("Sample Rate");
lblSampleRate.setBounds(12, 12, 230, 15);
add(lblSampleRate);
samplerate = new JComboBox<CbOption>();
samplerate.addItem(new CbOption(48000, "48000 Hz (DAT)"));
samplerate.addItem(new CbOption(44100, "44100 Hz (CD)"));
samplerate.addItem(new CbOption(22050, "22500 Hz (FM)"));
samplerate.addItem(new CbOption(16000, "16000 Hz"));
samplerate.addItem(new CbOption(11025, "11025 Hz"));
samplerate.addItem(new CbOption(8000, "8000 Hz"));
samplerate.setSelectedIndex(1);
samplerate.setBounds(12, 39, 156, 24);
add(samplerate);
JLabel lblChannels = new JLabel("Channels");
lblChannels.setBounds(12, 75, 230, 15);
add(lblChannels);
channels = new JComboBox<CbOption>();
channels.addItem(new CbOption(1, "Mono"));
channels.addItem(new CbOption(2, "Stereo"));
channels.setSelectedIndex(1);
channels.setBounds(12, 102, 156, 24);
add(channels);
JLabel lblMetadata = new JLabel("Metadata");
lblMetadata.setBounds(12, 138, 230, 15);
add(lblMetadata);
metadata = new JComboBox<CbOption>();
metadata.addItem(new CbOption(0, "None"));
metadata.addItem(new CbOption(1, "File"));
metadata.setSelectedIndex(0);
metadata.setBounds(12, 165, 156, 24);
add(metadata);
JLabel lblMetadataFile = new JLabel("Metadata File");
lblMetadataFile.setBounds(12, 201, 230, 15);
add(lblMetadataFile);
metadatafilename = new JTextField();
metadatafilename.setBounds(12, 228, 251, 24);
add(metadatafilename);
metadatafilename.setColumns(10);
}
}
I realize that using all JTabbedPanes is not visually pleasing, and can be confusing. I actually intend to change that. However, I still need to know why this isn't working as expected. Below are two screen shots showing the top level tabs working ok, and then you see when Input is clicked, there is nothing visible.
我意识到使用所有JTabbedPanes在视觉上并不令人愉悦,并且可能令人困惑。我其实打算改变它。但是,我仍然需要知道为什么这不能按预期工作。下面是两个屏幕截图,显示*选项卡正常工作,然后您看到单击输入时,没有任何可见。
Imported JPanel shown in nested tab
嵌套选项卡中显示导入的JPanel
Imported JPanel with its own JTabbedPane not displayed
导入的JPanel没有显示自己的JTabbedPane
1 个解决方案
#1
1
However, my Input JPanel is also loading a set of tabs, and those tabs are not visible when I run the app.
但是,我的输入JPanel也加载了一组选项卡,当我运行应用程序时,这些选项卡不可见。
Don't use a null layout. All panels should use an appropriate layout manager.
不要使用null布局。所有面板都应使用适当的布局管理器。
Probably use a BorderLayout
, then the added tab will have access to all the space available to the input panel.
可能使用BorderLayout,然后添加的选项卡将可以访问输入面板可用的所有空间。
In the future (and if you need more help) post a proper MCVE that demonstrates the problem when you post code. That is post minimal code that compiles and demonstrates the problem.
在将来(如果您需要更多帮助)发布一个适当的MCVE,在您发布代码时演示该问题。这是发布编译和演示问题的最小代码。
For example, we don't have access to your panel classes, so just use a JPanel to add to the tabbed pane.
例如,我们无权访问您的面板类,因此只需使用JPanel添加到选项卡式窗格即可。
#1
1
However, my Input JPanel is also loading a set of tabs, and those tabs are not visible when I run the app.
但是,我的输入JPanel也加载了一组选项卡,当我运行应用程序时,这些选项卡不可见。
Don't use a null layout. All panels should use an appropriate layout manager.
不要使用null布局。所有面板都应使用适当的布局管理器。
Probably use a BorderLayout
, then the added tab will have access to all the space available to the input panel.
可能使用BorderLayout,然后添加的选项卡将可以访问输入面板可用的所有空间。
In the future (and if you need more help) post a proper MCVE that demonstrates the problem when you post code. That is post minimal code that compiles and demonstrates the problem.
在将来(如果您需要更多帮助)发布一个适当的MCVE,在您发布代码时演示该问题。这是发布编译和演示问题的最小代码。
For example, we don't have access to your panel classes, so just use a JPanel to add to the tabbed pane.
例如,我们无权访问您的面板类,因此只需使用JPanel添加到选项卡式窗格即可。