public class E{
public static void main(String args[]) {
ComponentInWindow win = new ComponentInWindow();
win.setBounds(100,100,450,260);
win.setTitle("常用组件");
}
}
import java.awt.*;
import javax.swing.*;
public class ComponentInWindow extends JFrame{
JCheckBox checkBox1,checkBox2;
JRadioButton radioM,radioF;
ButtonGroup group;
JComboBox<String> comBox;
public ComponentInWindow() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout(new FlowLayout());
comBox = new JComboBox<String>();
checkBox1 = new JCheckBox("喜欢音乐");
checkBox1 = new JCheckBox("喜欢旅游");
group = new ButtonGroup();
radioM = new JRadioButton("男");
radioF = new JRadioButton("女");
group.add(radioM);
group.add(radioF);
add(checkBox1);
add(checkBox2);
add(radioM);
add(radioF);
comBox.addItem("音乐天地");
comBox.addItem("武术天地");
add(comBox);
}
}
异常:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1095)
at java.awt.Container.add(Container.java:1007)
at javax.swing.JFrame.addImpl(JFrame.java:567)
at java.awt.Container.add(Container.java:419)
at asa.ComponentInWindow.init(ComponentInWindow.java:25)
at asa.ComponentInWindow.<init>(ComponentInWindow.java:10)
at asa.E.main(E.java:5)