Java初学者问题-向屏幕添加多个项目

发布于 2024-11-27 06:14:38 字数 779 浏览 1 评论 0原文

我是一个完全的初学者,正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离笑几人歌 2024-12-04 06:14:38

您忘记添加

setLayout(layout);

构造函数。

You forgot to add

setLayout(layout);

in the constructor.

べ映画 2024-12-04 06:14:38

您忘记设置已定义的布局管理器 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 add this.setLayout(layout); inside your constructor.

寻找一个思念的角度 2024-12-04 06:14:38

这不是对你的问题的直接答案,但如果你刚刚开始学习 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文