我正在尝试输出这些组件,但它们不会出现。我无法弄清楚我做错了什么。
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);
}
}
答案 0 :(得分: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);
}
如果您只是在学习,我会强烈建议您不要使用applet,他们有自己的问题可能会让学习成为一种真正的痛苦。相反,请尝试从基于窗口的组件开始,例如JFrame