简短版
总而言之。我想创建4个tabbedpane并在tabbedpanes上放置几个jpanels。我询问有关如何执行此操作的堆栈溢出问题,并被定向到某个oracle网站:https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html 我发现代码非常方便并且使用它......但它似乎没有工作。首先应该有4个窗格,但它只显示3.甚至对于3个标签窗格它没有显示我放置的许多jpanels在每一个上,但它只是显示实际工作的三个tabbedpane的空白页。 如果您已了解我的问题,则可以跳过长版本故事并转到代码部分。 我没有包含其他3个选项卡式窗格的代码,因为我相信如果有人能够找到第一个窗格的解决方案以及如何使其显示我的组件,我将轻松弄清楚如何修复其他人。 下面的版本较长,但如果您已经知道问题所在并找到解决方案,则可以跳过它。
LONG VERSION
这是我想制作的GIUS之一的图片。(其中一个标签应如何显示。)
A more detailed image of the GIU
现在至少你得到它:一个jpanel上的5个jpanels是Jpanel1,也是第一个窗格。嵌套在Jpanel1上的所有Jpanel都有自己的组件,例如buttons,labbels和jtextarea。 我把这些照片放在这里,这样你就可以清楚地了解我在做什么,这样我可以更清楚地解释我遇到的问题。 我的选项卡式窗格(panel1,panel2,panel3和panel4)似乎没有显示我连接到它们的jpanel。我使用了以下代码: 创建tabbedpanes。我没有包含其他人的代码:2,3,4因为我相信如果我们只解决了panel1那么很容易找出其他的。窗格应该是4个,如我所示代码,这里:
tabbedPane.addTab("Tab 1", icon, panel1, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JComponent panel2=makeTextPanel("Panel #2");
tabbedPane.addTab("Tab 2", icon, panel2, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
JComponent panel3=makeTextPanel("Panel #3");
tabbedPane.addTab("Tab 3", icon, panel3, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
JComponent panel4=makeTextPanel("Panel #4");
panel4.setPreferredSize(new Dimension(450,50));
tabbedPane.addTab("Tab 4", icon, panel4, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
对于每个4panes我添加了5个窗格,然后在那些jpanels上我添加了其他jpanels然后添加到jpanells我添加了jlabels,jbuttons,jtext区域等。
编辑:由于我被告知缩短我的代码并且我会缩短它,但如果有人想要澄清一下我会发布更长版本的代码
*代码短篇
代码
public class StayConnected {
public static void main(String[] args) {
//We have created a jframe
JFrame frame=new JFrame ("StayConnect");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane= frame.getContentPane();
frame.setVisible(true);
//Now we are creating tabbed panes which will have the different Jpanel screens.
// ImageIcon icon =new ImageIcon("images-10.jpeg");
JTabbedPane tabbedPane = new JTabbedPane();
ImageIcon icon =new ImageIcon("images-17.jpeg");
contentPane.add(tabbedPane);
//Now we are adding the jpanels to the tabbed panes.
JComponent panel1=makeTextPanel("Panel #1");
tabbedPane.addTab("Tab 1", icon, panel1, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
//This are other panes whose codes I have not included so you can just remove them because the issue here is with the first pane above( but if possible please include answers that are flexible enough to allow additions of more panes like the one's below.)
JComponent panel2=makeTextPanel("Panel #2");
tabbedPane.addTab("Tab 2", icon, panel2, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
JComponent panel3=makeTextPanel("Panel #3");
tabbedPane.addTab("Tab 3", icon, panel3, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
JComponent panel4=makeTextPanel("Panel #4");
panel4.setPreferredSize(new Dimension(450,50));
tabbedPane.addTab("Tab 4", icon, panel4, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
contentPane.add(tabbedPane);
//Now we are going to add jpanels to our jpanels above.
//Now we are going to add jpanels to our panes above
// **This are the Jpanels for the first pane which is "panel1" **
JPanel panellogo= new JPanel();
panellogo.setLayout(new FlowLayout());
panel1.add(panellogo);
JLabel label1= new JLabel("Dance");
label1.setIcon(new ImageIcon("images.png"));
panellogo.add(label1);
//I'll stop my code here since I've been told to shorten it.But just like I said in the edit :if you want more clarification just tell me
//That code Is through for the first tab, and every other jpanel you have seen in this code has been placed on it.The tab is:"panel1
protected Component makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
frame.pack();
}}