Java Jtabbed窗格不起作用(在溢出时查看问题-其中没有人帮助)
短版 长话短说。我想创建4个选项卡窗格,并在选项卡窗格上放置几个JPanel。我问了一个关于堆栈溢出的问题,询问了如何执行此操作,并被定向到某个oracle网站: 我发现代码非常方便并使用了它……但它似乎不起作用。首先应该有4个选项卡窗格,但它只显示3个。即使对于3个选项卡窗格,它也没有显示我在每个选项卡窗格上放置的许多JPanel,而是为实际起作用的三个选项卡窗格显示一个空白页。 如果您理解了我的问题,您可以跳过冗长的版本故事,转到代码部分。 我没有包括其他3个选项卡式窗格的代码,只是因为我相信如果有人能找到第一个窗格的解决方案,以及如何使其显示其组件,我将很容易找到修复其他窗格的方法。 下面是较长的版本,但如果您已经知道问题所在并找到了解决方案,则可以跳过该版本 长版本 这是我想制作的一个GIU的图片(这是其中一个标签的外观) 现在你至少可以看到:一个jpanel上有5个jpanel,这也是第一个窗格Jpanel1。所有嵌套在Jpanel1上的jpanel都有自己的组件,例如按钮、标签和jtextarea。 我把这些图片放在这里,这样你就可以非常清楚地了解我在做什么,这样我遇到的问题就可以得到更清楚的解释。 我的选项卡式窗格(即panel1、panel2、panel3和panel4)似乎没有显示我连接到它们的JPanel。我使用了以下代码: 创建选项卡窗格。我没有包括其他窗格的代码:2,3,4,因为我相信如果我们只为panel1求解,则很容易找到其他窗格。窗格应该是其中的4个,如我的代码所示,如下所示:Java Jtabbed窗格不起作用(在溢出时查看问题-其中没有人帮助),java,swing,Java,Swing,短版 长话短说。我想创建4个选项卡窗格,并在选项卡窗格上放置几个JPanel。我问了一个关于堆栈溢出的问题,询问了如何执行此操作,并被定向到某个oracle网站: 我发现代码非常方便并使用了它……但它似乎不起作用。首先应该有4个选项卡窗格,但它只显示3个。即使对于3个选项卡窗格,它也没有显示我在每个选项卡窗格上放置的许多JPanel,而是为实际起作用的三个选项卡窗格显示一个空白页。 如果您理解了我的问题,您可以跳过冗长的版本故事,转到代码部分。 我没有包括其他3个选项卡式窗格的代码,只是因为我相
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);
我在这4个面板中添加了5个JP面板,然后在这些JP面板上添加了其他JP面板,然后在这些JP面板上添加了JLabel、jbuttons、jtext area等
编辑:因为我被告知要缩短我的代码,我会缩短它,但如果有人可能需要一些澄清,我会发布更长版本的代码
*代码的简短版本
代码
我不知道有什么显示不符合您的要求,因为您没有对此进行解释,但我修复了一些琐碎的编译错误,现在看起来是这样的: 代码如下:
public class StayConnected {
public static void main(String[] args) {
JFrame frame = new JFrame("StayConnect");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JTabbedPane tabbedPane = new JTabbedPane();
ImageIcon icon = new ImageIcon("images-17.jpeg");
contentPane.add(tabbedPane);
JPanel panel1 = makeTextPanel("Panel #1");
tabbedPane.addTab("Tab 1", icon, panel1, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JPanel panel2 = makeTextPanel("Panel #2");
tabbedPane.addTab("Tab 2", icon, panel2, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
JPanel panel3 = makeTextPanel("Panel #3");
tabbedPane.addTab("Tab 3", icon, panel3, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
JPanel panel4 = makeTextPanel("Panel #4");
tabbedPane.addTab("Tab 4", icon, panel4, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
JPanel panellogo = new JPanel();
panellogo.setBackground(Color.ORANGE);
panellogo.setLayout(new FlowLayout());
panel1.add(panellogo);
JLabel label1 = new JLabel("Dance");
label1.setIcon(new ImageIcon("images.png"));
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
panellogo.add(label1);
frame.pack();
frame.setVisible(true);
}
protected static JPanel makeTextPanel(String text) {
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
JLabel filler = new JLabel(text);
filler.setOpaque(true);
filler.setBackground(Color.BLUE);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
}
注意事项:
应该最后调用,因为您不能在GUI显示后简单地更改它setVisible
- 我添加了颜色,以便您可以看到每个组件的位置。使用GUI通常是很有用的
- 我指定选项卡组件为
s,因为这是您在JPanel
方法中创建的makeTextPanel
makeTextPanel
方法的代码。如果我用一个新的JPanel()
来替换它,它对我来说是有效的-嵌套的组件被添加到面板1
。但是,如前所述,a将是最好的。我链接到的网站:没有提供关于“makeTextPanel方法”的一点信息。你说的“用新的JPanel()替换它”是什么意思?。没有提供关于“makeTextPanel方法”的一点信息。
-当然有。您可以从教程中下载完整的代码。本教程是一个简单的例子。我们将为您创建每个选项卡所需的自定义面板。谢谢,但是如何处理我在上一篇文章中提到的“类型不匹配:无法将组件转换为JComponent”错误comment@james23我没有它,因为正如我所说,我修复了一些微不足道的编译错误。您应该真正阅读有关Swing组件的文档。
public class StayConnected {
public static void main(String[] args) {
JFrame frame = new JFrame("StayConnect");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JTabbedPane tabbedPane = new JTabbedPane();
ImageIcon icon = new ImageIcon("images-17.jpeg");
contentPane.add(tabbedPane);
JPanel panel1 = makeTextPanel("Panel #1");
tabbedPane.addTab("Tab 1", icon, panel1, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JPanel panel2 = makeTextPanel("Panel #2");
tabbedPane.addTab("Tab 2", icon, panel2, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
JPanel panel3 = makeTextPanel("Panel #3");
tabbedPane.addTab("Tab 3", icon, panel3, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
JPanel panel4 = makeTextPanel("Panel #4");
tabbedPane.addTab("Tab 4", icon, panel4, "Put in all the necessary information, thankyou");
tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
JPanel panellogo = new JPanel();
panellogo.setBackground(Color.ORANGE);
panellogo.setLayout(new FlowLayout());
panel1.add(panellogo);
JLabel label1 = new JLabel("Dance");
label1.setIcon(new ImageIcon("images.png"));
label1.setOpaque(true);
label1.setBackground(Color.YELLOW);
panellogo.add(label1);
frame.pack();
frame.setVisible(true);
}
protected static JPanel makeTextPanel(String text) {
JPanel panel = new JPanel();
panel.setBackground(Color.RED);
JLabel filler = new JLabel(text);
filler.setOpaque(true);
filler.setBackground(Color.BLUE);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
}