学习制作GUI界面

//制作界面的模板


public class MyFrame extends JFrame{

private Container contentP;//内容面板

private JLabel usernameLab;//标签

private JLabel imgLab;//标签

private JButton okBtn;//按钮

private JTextField inputTxt;//文本框

private JPasswordField passwordTxt;//密码框

private JComboBox classComb;//下拉框

private JRadioButton maleRad;//男性单选框

private JRadioButton femaleRad;//女性单选框

private JCheckBox  swimCheck;//复选框

private JTextArea msgTxtArea;//文本域

public MyFrame(){
Toolkit tk = Toolkit.getDefaultToolkit();//得到工具类
this.setSize(500, 400);//设置大小
this.setLocation((int)(tk.getScreenSize().getWidth()-500)/2, 
(int)(tk.getScreenSize().getHeight()-400)/2);//设置位置
this.setTitle("我的第一个窗体");//设置标题
this.setIconImage(tk.createImage("img/wtp_icon_x16.gif"));//设置图标文件
this.setResizable(false);//窗体大小不可变
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);//设置窗体关闭即程序结束

this.addContent();

this.setVisible(true);//设置窗体可见---写在最后
}

//本方法专门完成对内容面板及其里面组件的操作
private void addContent(){
this.contentP = this.getContentPane();//得到内容面板
this.contentP.setBackground(Color.WHITE);//设置内容面板的背景色
this.contentP.setLayout(null);//设置内容面板的布局管理器为空,这样我们给的组件位置、大小才会起作用

//组件的使用
//文字标签
this.usernameLab = new JLabel();//产生组件对象
this.usernameLab.setText("用户名:");//设置标签文本
this.usernameLab.setFont(new Font("隶书", Font.BOLD, 24));//设置文本字体
this.usernameLab.setForeground(new Color(203,86,220));//设置文本颜色
this.usernameLab.setSize(120, 50);
this.usernameLab.setLocation(100, 0);
//调试标签大小位置的特有代码--调完以后注释掉
// this.usernameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.contentP.add(this.usernameLab);//代表把标签放入它所在的容器中

//图片标签
this.imgLab = new JLabel();
this.imgLab.setIcon(new ImageIcon("img/mypic.JPG"));//设置图片
this.imgLab.setSize(161,34);
this.imgLab.setLocation(230, 5);
this.contentP.add(this.imgLab);

//按钮
this.okBtn = new JButton();
this.okBtn.setText("确定");
this.okBtn.setEnabled(false);//设置组件不可用
this.okBtn.setFont(new Font("微软雅黑",Font.PLAIN,18));
this.okBtn.setForeground(new Color(203,86,220));
this.okBtn.setBounds(0,60,100,40);
this.contentP.add(this.okBtn);

//文本框
this.inputTxt = new JTextField();
this.inputTxt.setFont(new Font("微软雅黑",Font.PLAIN,16));
this.inputTxt.setForeground(new Color(203,86,220));
this.inputTxt.setBounds(110, 60, 120, 30);
this.contentP.add(this.inputTxt);

//密码框
this.passwordTxt = new JPasswordField();
this.passwordTxt.setEchoChar('*');
this.passwordTxt.setFont(new Font("微软雅黑",Font.PLAIN,16));
this.passwordTxt.setForeground(new Color(203,86,220));
this.passwordTxt.setBounds(240, 60, 120, 30);
this.contentP.add(this.passwordTxt);

//下拉框
this.classComb = new JComboBox();
this.classComb.addItem("J130");
this.classComb.addItem("J131");
this.classComb.addItem("J132");
this.classComb.setSelectedIndex(2);
this.classComb.setEditable(true);//设置可编辑
this.classComb.setBounds(2, 110, 120, 30);
this.contentP.add(this.classComb);

//单选框
this.maleRad = new JRadioButton("男");
this.femaleRad = new JRadioButton("女");
this.maleRad.setSelected(true);//设置单选框被默认选中
this.maleRad.setBackground(Color.WHITE);
this.femaleRad.setBackground(Color.WHITE);
this.maleRad.setBounds(125, 110, 50, 30);
this.femaleRad.setBounds(180, 110, 50, 30);
this.contentP.add(this.maleRad);
this.contentP.add(this.femaleRad);
ButtonGroup bg = new ButtonGroup();
bg.add(this.maleRad);
bg.add(this.femaleRad);


//复选框
this.swimCheck = new JCheckBox("游泳");
this.swimCheck.setSelected(true);
this.swimCheck.setBounds(240, 110, 80, 30);
this.contentP.add(this.swimCheck);

//文本域--默认情况下没有外边框
this.msgTxtArea = new JTextArea();
this.msgTxtArea.setFont(new Font("微软雅黑",Font.PLAIN,16));
JScrollPane sp = new JScrollPane(this.msgTxtArea);
sp.setBounds(5, 150, 200, 200);
this.contentP.add(sp);

}


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值