javaoo.day12
package com.lovo.frame;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestFrame extends JFrame{
private Container con;
private JLabel usernameLab;//文字标签
private JLabel imageLab;//图片标签
private JTextField usernameTxt;//文本框
private JPasswordField pwdTxt;//密码框
private JButton okBtn;//按钮
private JButton imageBtn;//图片按钮
private JComboBox stateComb;//下拉框
private JRadioButton maleRad;//单选框
private JRadioButton femaleRad;
private JCheckBox readChc;//复选框
private JCheckBox movieChc;
private JCheckBox musicChc;
private JTextArea inputArea;//文本域(多行文本输入框)
public TestFrame(){
Toolkit tk = Toolkit.getDefaultToolkit();//工具箱类
this.setSize(500, 300);//设置窗体大小
this.setResizable(false);//设置窗体大小不可变
this.setLocation(((int)tk.getScreenSize().getWidth()-500)/2,
((int)tk.getScreenSize().getHeight()-300)/2);//设置窗体位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭,程序即结束
this.setTitle("我的第一个窗体");//设置标题
this.setIconImage(tk.createImage("img/hp.JPG"));//设置图标
this.addContent();
this.setVisible(true);//设置窗体可见(放最后)
}
private void addContent(){
this.con = this.getContentPane();//获取内容面板(JFrame自带)
this.con.setBackground(Color.WHITE);//改变内容面板背景色(改JFrame背景没有效果)
this.con.setLayout(null);//把容器自带的布局管理器设为空,使用空布局(绝对定位)
//完成一个组件的放入分4步:
//1、new出组件对象;
this.usernameLab = new JLabel();
//2、调整组件细节属性(这个动作可以最后边看效果边调整)
this.usernameLab.setText("成都朗沃教育网站的用户名:");
this.usernameLab.setFont(new Font("隶书",Font.ITALIC,20));
this.usernameLab.setForeground(Color.BLUE);
// this.usernameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));//边框设置
//3、在空布局的情况下,必须自定义位置和大小
this.usernameLab.setBounds(100, 5, 300, 30);//设置组件的位置/大小,必须保证容器的布局管理器为空,这句代码才起作用。
//4、把组件添加到容器中
this.con.add(this.usernameLab);//放入
this.imageLab = new JLabel();
this.imageLab.setIcon(new ImageIcon("img/tx.JPG"));
this.imageLab.setBounds(5, 30, 88, 86);
this.con.add(this.imageLab);
this.usernameTxt = new JTextField();
this.usernameTxt.setFont(new Font("宋体",Font.PLAIN,30));
this.usernameTxt.setBounds(100, 40, 200, 30);
this.con.add(this.usernameTxt);
this.pwdTxt = new JPasswordField();
this.pwdTxt.setFont(new Font("宋体",Font.PLAIN,30));
this.pwdTxt.setEchoChar('*');
this.pwdTxt.setBounds(100, 80, 200, 30);
this.con.add(this.pwdTxt);
this.okBtn = new JButton("确定");
this.okBtn.setIcon(new ImageIcon("img/hp.JPG"));
this.okBtn.setRolloverIcon(new ImageIcon("img/logo.gif"));
this.okBtn.setFont(new Font("宋体",Font.PLAIN,24));
this.okBtn.setForeground(Color.GREEN);
this.okBtn.setBounds(320, 40, 140, 30);
this.con.add(this.okBtn);
this.imageBtn = new JButton(new ImageIcon("img/buttonClear.jpg"));
this.imageBtn.setBounds(320, 80, 140, 50);
this.con.add(this.imageBtn);
this.stateComb = new JComboBox(new String[]{"山东","河南","湖北"});
this.stateComb.addItem("四川");
this.stateComb.setEditable(true);//设置可编辑
// this.stateComb.setEnabled(false);//设置禁用
this.stateComb.setBounds(10, 150, 100, 30);
this.con.add(this.stateComb);
this.maleRad = new JRadioButton("男");
this.femaleRad = new JRadioButton("女");
ButtonGroup bg = new ButtonGroup();
bg.add(this.maleRad);
bg.add(this.femaleRad);
this.maleRad.setSelected(true);
this.maleRad.setBounds(10, 190, 50, 30);
this.femaleRad.setBounds(100, 190, 50, 30);
this.con.add(this.maleRad);
this.con.add(this.femaleRad);
this.readChc = new JCheckBox("阅读");
this.movieChc = new JCheckBox("电影");
this.musicChc = new JCheckBox("音乐");
ButtonGroup bg0 = new ButtonGroup();
bg0.add(this.readChc);
bg0.add(this.movieChc);
bg0.add(this.musicChc);
this.movieChc.setSelected(true);
this.readChc.setBounds(10, 220, 60, 30);
this.movieChc.setBounds(80, 220, 60, 30);
this.musicChc.setBounds(150, 220, 60, 30);
this.con.add(this.readChc);
this.con.add(this.movieChc);
this.con.add(this.musicChc);
//文本域默认不带外边框、滚动条
this.inputArea = new JTextArea();
JScrollPane sp = new JScrollPane(this.inputArea);
sp.setBounds(220, 150, 270, 100);
this.con.add(sp);
}
}