简体   繁体   English

组件不会出现

[英]Components won't appear

I'm trying to output these components but they won't appear. 我正在尝试输出这些组件,但它们不会出现。 I can't figure out what I did wrong. 我不知道我做错了什么。

import java.awt.*;
import javax.swing.*;

public class Buttons extends JApplet {
  Container con;
  JPanel form;
  JButton oneB, twoB, threeB;
  public void init() {
    con = new Container();
    form = new JPanel();
    form.setLayout(new GridLayout(2, 2));
    oneB = new JButton("1B");
    form.add(oneB);
    twoB = new JButton("2B");
    form.add(twoB);
    threeB = new JButton("3B");
    form.add(threeB);
    con.add(form);
  }
}

You never added con to anything 你从来没有添加con

In fact, it's not really needed, just add form to the applet... 实际上,它并不是真正需要的,只需将form添加到小程序中即可。

  public void init() {
    form = new JPanel();
    form.setLayout(new GridLayout(2, 2));
    oneB = new JButton("1B");
    form.add(oneB);
    twoB = new JButton("2B");
    form.add(twoB);
    threeB = new JButton("3B");
    form.add(threeB);
    add(form);
  }

If you're just learning, I would highly encourage you NOT to use applets, they have their own issues which can make learning a real pain. 如果您只是学习,我强烈建议您不要使用applet,因为applet有其自身的问题,这些问题会使学习变得非常痛苦。 Instead, try starting with window based components, like JFrame 而是尝试从基于窗口的组件开始,例如JFrame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM