package com.lovo;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class MyFrame extends JFrame{
public MyFrame(int x,int y) {
init(x,y);
}
public void init(int x,int y) {
this.setSize(x, y);
this.setLocationRelativeTo(this);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setLayout(null);
JLabel jl = new JLabel();
Icon ic = newImageIcon("D:/image/fruit.gif");
jl.setIcon(ic);
jl.setBounds(100, 100,ic.getIconWidth(),ic.getIconHeight());
//按钮
JButton jb =createJbutton("登陆",50,20,60,20);
//文本框
JTextField jtf = newJTextField();
jtf.setBounds(130, 20, 100,20);
//单选按钮
JRadioButton jrb1 = newJRadioButton("男");
jrb1.setBounds(100, 130, 40,20);
JRadioButton jrb2 = newJRadioButton("女");
jrb2.setBounds(150, 130, 40,20);
//组对象,用来添加相同一组单选按钮的对象
ButtonGroup bg = newButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
//复选按钮
JCheckBox jcb1 = newJCheckBox("体育");
jcb1.setBounds(50, 160, 60,20);
JCheckBox jcb2 = newJCheckBox("音乐");
jcb2.setBounds(120, 160, 60,20);
JCheckBox jcb3 = newJCheckBox("游戏");
jcb3.setBounds(190, 160, 60,20);
Container c =this.getContentPane();
c.add(jl);
c.add(jb);
c.add(jtf);
c.add(jrb1);
c.add(jrb2);
c.add(jcb1);
c.add(jcb2);
c.add(jcb3);
this.setVisible(true);
}
//将单选框封装成为一个方法
public JButton createJbutton(String name,intx,int y,int width,int height) {
JButton jb = newJButton(name);
jb.setBounds(x, y, width,height);
return jb;
}
//将复选框封装成为一个方法
public JCheckBox createjcheckbutton(Stringname,int x,int y,int width,int height){
JCheckBox jcb = newJCheckBox(name);
return jcb;
}
public static void main(String[] args) {
new MyFrame(300,300);
}
}