Java初学者问题-向屏幕添加多个项目
我是一个完全的初学者,正在尝试学习 Java 作为我的第一语言。当我向窗口添加“类型”和“技能”时,仅显示最后添加的一项(在本例中为技能)。我怎样才能让两者都出现?
谢谢, 猎食
public class Funclass extends JFrame{
FlowLayout layout = new FlowLayout();
String[] Skillz = {"Analytical", "Numerical", "Leadership",
"Communication", "Organisation", "Interpersonal"};
public Funclass(){
super("Title Bar");
JTextField Company = new JTextField("Company Name");
JComboBox TYPE = new JComboBox();
JList Skills = new JList(Skillz);
TYPE.addItem("Choose which type of firm");
TYPE.addItem("Consultancy");
TYPE.addItem("Tech");
Skills.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(TYPE);
add(Skills);
}
}
I'm a complete beginner trying to learn Java as my first language. When I'm adding TYPE and Skills to the window, only the one added last (in this case, skills) appears. How do I get both to appear?
Thanks,
Ravin
public class Funclass extends JFrame{
FlowLayout layout = new FlowLayout();
String[] Skillz = {"Analytical", "Numerical", "Leadership",
"Communication", "Organisation", "Interpersonal"};
public Funclass(){
super("Title Bar");
JTextField Company = new JTextField("Company Name");
JComboBox TYPE = new JComboBox();
JList Skills = new JList(Skillz);
TYPE.addItem("Choose which type of firm");
TYPE.addItem("Consultancy");
TYPE.addItem("Tech");
Skills.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(TYPE);
add(Skills);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您忘记添加
构造函数。
You forgot to add
in the constructor.
您忘记设置已定义的布局管理器
FlowLayout layout = new FlowLayout();
,因此您只需在构造函数中添加this.setLayout(layout);
。You forget to set the layout manager that you have delcared
FlowLayout layout = new FlowLayout();
, so you just need to addthis.setLayout(layout);
inside your constructor.这不是对你的问题的直接答案,但如果你刚刚开始学习 Java,我认为你可能不应该开始尝试使用 Swing 构建用户界面(“Swing”是你在这里用来构建的 Java 的一部分) GUI,而且相当复杂)。你真的是跳进了深渊。
关于你的代码的一个小问题。 Java 命名变量和方法的约定是使用“驼峰式”并以小写字符“somethingLikeThis”开头。因此,您的变量名称应该全部小写,因为它们都只是单个单词。 IE。 “技能”、“类型”、“公司”。
我向 Java 初学者强烈推荐 Ken Arnold 所著的《Java 编程语言》一书。
This isn't a direct answer to your question but if you are just beginning to learn Java, I think you should probably not start with trying to build user interfaces with Swing ("Swing" is the part of Java you are using here to build a GUI, and it is fairly complicated). You are really jumping in at the deep-end.
One minor nit-pick about your code. Java convention for naming variables and methods is to use "camel-case" and begin with a lower-case character, "somethingLikeThis". So your variable names should all be lower-case since they are all just single-words. ie. "skills", "type", "company".
I highly recommend the book "The Java Programming Language" by Ken Arnold for Java beginners.