import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
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.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class MyJFrame extends JFrame{
public MyJFrame(){
//设置布局管理器,null表示绝对布局,//可以通过组件的位置和大小定位组件this.setLayout(null);//创建按钮对象JButton b = new JButton("添加");//b.setText("添加");//设置按钮位置和大小b.setBounds(100, 50, 80, 25);//将组件加入窗体this.add(b);//创建标签组件JLabel jl = new JLabel("姓名");//设置字体,字体,类型,大小jl.setFont(new Font("黑体",Font.BOLD,30));//设置前景色jl.setForeground(new Color(255,0,120));jl.setBounds(50, 80, 80, 30);this.add(jl);//文本框JTextField jtxt = new JTextField();jtxt.setBounds(200, 50, 100, 20);this.add(jtxt);//单选框JRadioButton jr1 = new JRadioButton("男");jr1.setBounds(50, 200, 80, 20);this.add(jr1);JRadioButton jr2 = new JRadioButton("女");jr2.setBounds(130, 200, 80, 20);this.add(jr2);//设置单选框互斥ButtonGroup bu = new ButtonGroup();bu.add(jr1);bu.add(jr2);//复选框JCheckBox jc = new JCheckBox("体育");jc.setBounds(50, 230, 80, 20);this.add(jc);//文本域JTextArea jta = new JTextArea();//创建线性边框,颜色为兰色Border border = BorderFactory.createLineBorder(Color.blue);jta.setBorder(border);//文本域自动换行jta.setLineWrap(true);jta.setBounds(240, 200, 200, 200);this.add(jta);//创建图片标签Image i = new ImageIcon("c:/4.jpg").getImage();//得到压缩后的图像i = i.getScaledInstance(600, 400, 1);JLabel img = new JLabel(new ImageIcon(i));img.setBounds(0, 0, 600, 400);//组件的显示遵循,先进先出,后进后出的原则this.add(img);//设置窗体大小this.setSize(600,400);//设置窗体显示this.setVisible(true);//设置窗体居中this.setLocationRelativeTo(null);//设置关闭窗体,结束程序this.setDefaultCloseOperation(3);//设置窗体标题this.setTitle("我的窗体");
}
public static void main(String[] args) {
MyJFrame m = new MyJFrame();
}
}
写这个一定要注意,你最先写的代码,运行时是会出现在最前面,如果你想让一张图片当背景,那你写入这张图片的代码一定要写在最后面,当然要在窗体之前,不然显不出来
如果你用的是eclipse编译,如果你不知道要导入哪个类,那你先只管写代码,如果报错,就用Ctrl+Shift+O,这个快捷键,它会自动导入你用到的类,但你一定要继承JFrame这个类,不然导不进来的
我实在不知道怎么才能把代码用这个好好的写出来,只能这样了,大家将就
如有意见,请多提出,互相多多交流