组件不会出现

was*_*san 1 java user-interface swing

我正在尝试输出这些组件,但它们不会出现.我无法弄清楚我做错了什么.

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

Mad*_*mer 5

你从来没有添加con任何东西

事实上,它并不是真的需要,只需添加form到applet ......

  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);
  }
Run Code Online (Sandbox Code Playgroud)

如果您只是在学习,我强烈建议您不要使用applet,他们有自己的问题,这可能会让学习成为一种真正的痛苦.相反,尝试从基于窗口的组件开始,例如JFrame