[英]GUI Disappearing when I add JComboBox
Alright I'm relatively new to programming and it may be just something simple that I'm missing but the other threads related to this topic the poster didn't give adequate information relative to their issue for others to provide quality answers so I will give it a shot. 好吧,我对编程比较陌生,可能只是一些我想念的简单但是其他线索与这个主题有关,海报没有提供足够的信息,相对于他们的问题,其他人提供高质量的答案,所以我会给它一枪。
public BenchUI(JFrame j){
jf = j;
init();
add(mainPanel);
topPanelButtons();
selectedCustomer();
rentalOptions();
clientListBox();
}
At this point i can point out that everything works perfectly until I add the clientListBox() method. 在这一点上,我可以指出,在我添加clientListBox()方法之前,一切都运行良好。 (below)
(下面)
public void clientListBox(){
clientList = new JComboBox(moo);
clientList.setPreferredSize(new Dimension(460,30));
gbc.gridx = 0;
gbc.gridy = 0;
leftSide.add(clientList,gbc);
}
i can comment it out and get my whole GUI back working perfectly but without a JComboBox. 我可以将它评论出来,让我的整个GUI恢复正常但没有JComboBox。
moo is String [] moo = {"Fish","Goat", "Monkey"}; moo是String [] moo = {“Fish”,“Goat”,“Monkey”}; a dummy string just for testing purposes and initialized at the start.
一个虚拟字符串,仅用于测试目的并在开始时初始化。
So any idea why my GUI completely disappears when I place in the clientList? 所以当我放在clientList中时,为什么我的GUI完全消失了?
If anything else is necessary I'll be watching this thread and can provide additional information. 如果还有其他需要,我将会关注此主题并提供更多信息。
As a side note I keep getting warnings for "Raw Types" but it works without specifiying, could I potentially run into trouble by not specifying my JComboBox? 作为旁注,我不断收到“原始类型”的警告,但它没有指定工作,我可能因为没有指定我的JComboBox而遇到麻烦吗?
EDIT: 编辑:
ok I believe I've duplicated whatever the issue is in this code 好吧,我相信我已经复制了此代码中的任何问题
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
public class main {
public static void main(String[] args){
JFrame jf = new JFrame();
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
BenchUI bu = new BenchUI(jf);
jf.add(bu);
}
}
public class BenchUI extends JPanel{
JPanel one;
JFrame jf;
JComboBox<String> clientList;
String[] moo = {"Goat", "Fish", "Donkey"};
public BenchUI(JFrame j){
jf = j;
one = new JPanel(new GridBagLayout());
one.setBackground(Color.blue);
one.setPreferredSize(new Dimension(300,300));
clientList = new JComboBox<String>(moo);
one.add(clientList);
add(one);
}
}
with the clientList stuff commented out I get my silly little blue panel and once it is added I lose the blue panel and the combobox doesnt show up as well...betting on this is a facepalm issue at this point >.< 随着clientList的东西被注释掉,我得到了我的愚蠢的小蓝色面板,一旦它被添加,我失去了蓝色面板,并且组合框也没有显示出来......在这一点投注这是一个facepalm问题>。
EDIT: to include the main class. 编辑:包括主类。
EDIT: took out the comment marks for the JComboBox constructor and implementer 编辑:取出JComboBox构造函数和实现者的注释标记
Your posted sort of sscce-like (not a real SSCCE by the way since we can't run it) code doesn't add any such as the JComboBox to the JPanel and adds no components such as the current JPanel to the JFrame. 您发布的类似sscce(不是真正的SSCCE,因为我们无法运行它)代码不会将任何诸如JComboBox添加到JPanel,并且不会向JFrame添加诸如当前JPanel之类的组件。
public class BenchUI extends JPanel{
JPanel one;
JFrame jf;
JComboBox<String> clientList;
String[] moo = {"Goat", "Fish", "Donkey"};
public BenchUI(JFrame j){
jf = j;
one = new JPanel(new GridBagLayout());
one.setBackground(Color.blue);
one.setPreferredSize(new Dimension(300,300));
//clientList = new JComboBox<String>(moo);
//one.add(clientList);
add(one);
}
}
and so it makes sense that none of the components will show up on any JFrame. 所以没有任何组件会显示在任何JFrame上是有道理的。 You will want to read the Swing tutorials on how to add components to other components (or containers) and how to create and show a JFrame.
您将需要阅读有关如何将组件添加到其他组件(或容器)以及如何创建和显示JFrame的Swing教程。 Have a look at How to Use Swing Components .
看看如何使用Swing组件 。
Edit 编辑
Your latest code now does in fact add the BenchUI JPanel to the JFrame, but still you add no components to the BenchUI JPanel, and in fact you don't even construct your JComboBox but only create a JComboBox variable. 您的最新代码实际上确实将BenchUI JPanel添加到JFrame,但是您仍然没有向BenchUI JPanel添加任何组件,实际上您甚至不构造JComboBox而只创建JComboBox变量。 Again, I strongly urge you to read the Swing tutorials which I've linked to above as well as the general Java tutorials.
同样,我强烈建议您阅读我上面链接的Swing教程以及常规Java教程。
Edit 2 编辑2
Some general advice: 一些一般建议:
pack()
on the top level window -- which tells all the layout managers to lay out all the components they hold. pack()
之前将组件添加到顶级窗口 - 这会告诉所有布局管理器布置它们所持有的所有组件。 setVisible(true)
. setVisible(true)
。 One problem with your code (other than not creating important components and not adding them to the GUI!) is that you're calling setVisible(true)
on your JFrame way too early before adding anything to the GUI. setVisible(true)
。 Edit 3 编辑3
OK, now you're creating your JComboBox, but you still are adding all components to your JFrame after setting it visible. 好的,现在您正在创建JComboBox,但是在将其设置为可见之后 ,您仍然会将所有组件添加到JFrame中。 Please re-check my 3rd and 4th bullets in the bullet list above.
请重新检查上面子弹列表中的第3和第4个子弹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.