Java面板中的组件不显示,帮忙找出原因?
importjava.awt.*;importjavax.swing.*;publicclassJCheckBoxDemoextendsJPanel{publicstat...
import java.awt.*;
import javax.swing.*;
public class JCheckBoxDemo extends JPanel {
public static void main(String[] args) {
JFrame frame=new JFrame("窗体");
Container contentPanel=frame.getContentPane();
frame.setSize(500, 400);
frame.setVisible(true);
}
private static JPanel createMainPanel()
{
JPanel panel=new JPanel();
JPanel part1=new JPanel();
part1.add(new JLabel("姓名"));
JTextField text=new JTextField();
text.setColumns(10);
part1.add(text);
part1.setVisible(true);
panel.add(part1);
JPanel part2=new JPanel();
part2.add(new JLabel("性别:"));
ButtonGroup group=new ButtonGroup();
JRadioButton radioButton1=new JRadioButton("男",true);
//radioButton1.setSelected(true);
JRadioButton radioButton2=new JRadioButton("女");
group.add(radioButton1);
group.add(radioButton2);
part2.add(radioButton1);
part2.add(radioButton2);
part2.setVisible(true);
panel.add(part2);
JPanel part3=new JPanel();
part3.setLayout(new GridLayout(2,3));
JCheckBox musicBox=new JCheckBox("音乐");
musicBox.setSelected(true);
part3.add(musicBox);
part3.add(new JCheckBox("音乐"));
part3.add(new JCheckBox("篮球"));
part3.add(new JCheckBox("上网"));
part3.add(new JCheckBox("交际"));
part3.setVisible(true);
panel.add(part3);
JPanel part4=new JPanel();
part4.add(new JButton("重置"));
part4.add(new JButton("确认"));
part4.setVisible(true);
panel.setVisible(true);
return panel;
}
} 展开
import javax.swing.*;
public class JCheckBoxDemo extends JPanel {
public static void main(String[] args) {
JFrame frame=new JFrame("窗体");
Container contentPanel=frame.getContentPane();
frame.setSize(500, 400);
frame.setVisible(true);
}
private static JPanel createMainPanel()
{
JPanel panel=new JPanel();
JPanel part1=new JPanel();
part1.add(new JLabel("姓名"));
JTextField text=new JTextField();
text.setColumns(10);
part1.add(text);
part1.setVisible(true);
panel.add(part1);
JPanel part2=new JPanel();
part2.add(new JLabel("性别:"));
ButtonGroup group=new ButtonGroup();
JRadioButton radioButton1=new JRadioButton("男",true);
//radioButton1.setSelected(true);
JRadioButton radioButton2=new JRadioButton("女");
group.add(radioButton1);
group.add(radioButton2);
part2.add(radioButton1);
part2.add(radioButton2);
part2.setVisible(true);
panel.add(part2);
JPanel part3=new JPanel();
part3.setLayout(new GridLayout(2,3));
JCheckBox musicBox=new JCheckBox("音乐");
musicBox.setSelected(true);
part3.add(musicBox);
part3.add(new JCheckBox("音乐"));
part3.add(new JCheckBox("篮球"));
part3.add(new JCheckBox("上网"));
part3.add(new JCheckBox("交际"));
part3.setVisible(true);
panel.add(part3);
JPanel part4=new JPanel();
part4.add(new JButton("重置"));
part4.add(new JButton("确认"));
part4.setVisible(true);
panel.setVisible(true);
return panel;
}
} 展开
1个回答
展开全部
public static void main(String[] args) {
JFrame frame=new JFrame("窗体");
Container contentPanel=frame.getContentPane();
frame.add(createMainPanel());
frame.setSize(500, 400);
frame.setVisible(true);
}
你在主函数里没有添加控件当然不会显示了,像下面
public static void main(String[] args) {
JFrame frame=new JFrame("窗体");
Container contentPanel=frame.getContentPane();
frame.add(createMainPanel());
frame.setSize(500, 400);
frame.setVisible(true);
}
添加你自己创建的控件就可以显示了
JFrame frame=new JFrame("窗体");
Container contentPanel=frame.getContentPane();
frame.add(createMainPanel());
frame.setSize(500, 400);
frame.setVisible(true);
}
你在主函数里没有添加控件当然不会显示了,像下面
public static void main(String[] args) {
JFrame frame=new JFrame("窗体");
Container contentPanel=frame.getContentPane();
frame.add(createMainPanel());
frame.setSize(500, 400);
frame.setVisible(true);
}
添加你自己创建的控件就可以显示了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询