您的位置:首页 > 产品设计 > UI/UE

GUI

2016-05-08 20:54 411 查看
public class MyFrame extends JFrame {

private Container contentP;//内容面板

private JLabel nameLab;//用户名标签

private JLabel imageLab;//图片标签

private JTextField nameTxt;//文本框

private JPasswordField pwdTxt;//密码框

private JComboBox genderCom;//下拉框

private JButton okBtn;//按钮

private JRadioButton maleRad;//单选框

private JRadioButton femaleRad;

private JTextArea contentArea;//文本域

private JScrollPane scrollP;//滚动条面板--配合文本域 

public MyFrame(){
//工具类--很常用,可以获取屏幕信息,还可以操作图标图片
Toolkit tk = Toolkit.getDefaultToolkit();
//设置标题
this.setTitle("我的第一个GUI程序。");
//设置图标
this.setIconImage(tk.createImage("pic" + File.separator + "1428562114541.jpg"));
//尺寸初始化
this.setSize(500, 400);
//位置设置
this.setLocation(((int)(tk.getScreenSize().getWidth())-500)/2, 
((int)(tk.getScreenSize().getHeight()) - 400)/2);
//设置窗体大小不可变
this.setResizable(false);
//关闭窗体即设置为关闭程序
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.addContent();

//设置窗体可见--一定放到构造方法的最后一句
this.setVisible(true);
}

//专门用来操作内容面板上的东西
private void addContent(){
//获取内容面板
this.contentP = this.getContentPane();

// this.contentP.setBackground(new Color(162,234,149));
this.contentP.setBackground(Color.WHITE);
//设置内容面板的布局管理器为null---使用绝对定位的方式放置组件
this.contentP.setLayout(null);

//放置文本标签
this.nameLab = new JLabel();//产生对象
this.nameLab.setText("用户名:");//设置文本
this.nameLab.setFont(new Font("华文楷体",Font.ITALIC,28));//设置字体
this.nameLab.setForeground(Color.GREEN);//设置前景色

// this.nameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));//设置边框--用于调试
this.nameLab.setBounds(30, 50, 130, 30);//设置位置和大小
this.contentP.add(this.nameLab);//放入容器

//放置图像标签
this.imageLab = new JLabel();//产生对象
this.imageLab.setIcon(new ImageIcon("pic/eye.JPG"));//放置图片
this.imageLab.setBounds(165, 50, 298, 50);
this.contentP.add(this.imageLab);

//文本框
this.nameTxt = new JTextField();
this.nameTxt.setFont(new Font("宋体",Font.BOLD,22));
this.nameTxt.setForeground(Color.BLUE);
this.nameTxt.setBounds(30, 105, 120, 30);
this.contentP.add(this.nameTxt);

//密码框
this.pwdTxt = new JPasswordField();
this.pwdTxt.setFont(new Font("宋体",Font.BOLD,22));
this.pwdTxt.setForeground(Color.BLUE);
this.pwdTxt.setEchoChar('*');//设置密码框的回显字符
this.pwdTxt.setBounds(160, 105, 120, 30);
this.contentP.add(this.pwdTxt);

//下拉框
this.genderCom = new JComboBox(new Object[]{"男","女"});
this.genderCom.addItem("太监");//添加选项
this.genderCom.setSelectedIndex(2);//指定默认被选中项
this.genderCom.setEditable(true);//设置是否可被编辑
this.genderCom.setBounds(300, 105, 120, 30);
this.contentP.add(this.genderCom);

//按钮
this.okBtn = new JButton();
this.okBtn.setText("确定");
this.okBtn.setIcon(new ImageIcon("pic/hp.JPG"));//设置默认图标 
this.okBtn.setRolloverIcon(new ImageIcon("pic/xiaoxin.GIF")); //设置鼠标移动进去的图标
this.okBtn.setBounds(30, 140, 100, 30);
this.contentP.add(this.okBtn);

//单选框
this.maleRad = new JRadioButton();
this.maleRad.setText("男");
this.femaleRad = new JRadioButton();
this.femaleRad.setText("女");
this.femaleRad.setSelected(true);//设置被选中为真
this.maleRad.setBounds(140, 140, 50, 20);
this.femaleRad.setBounds(200, 140, 50, 20);
this.contentP.add(this.maleRad);
this.contentP.add(this.femaleRad);
//按钮组--是一个逻辑概念,将放入其中的按钮在逻辑上进行分组
ButtonGroup bg = new ButtonGroup();
bg.add(this.maleRad);
bg.add(this.femaleRad);

//文本域
this.contentArea = new JTextArea();
this.scrollP = new JScrollPane(contentArea);
this.scrollP.setBounds(30, 180, 440, 180);
this.contentP.add(this.scrollP);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: