GUI起头

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 MyFrame extends JFrame{

private Container contentP;//内容面板

private JLabel msgLab;//文字标签

private JLabel imgLab;//图片标签

private JTextField usernameTxt;//文本框

private JPasswordField pwdTxt;//密码框

private JButton okBtn;//按钮

private JButton getMoentyBtn;//取钱按钮

private JComboBox<String> teacherCmb;//下拉列表

private JTextArea selfArea;//文本域

private JRadioButton maleRad;//单选框

private JRadioButton femaleRad;

private JCheckBox hobbitBox;//复选框


public MyFrame(){
Toolkit tk = Toolkit.getDefaultToolkit();//获取工具对象
int screenWidth = (int)tk.getScreenSize().getWidth();
int screenHeight = (int)tk.getScreenSize().getHeight();
this.setSize(500, 400);//设置窗体大小--像素
this.setLocation((screenWidth-500)/2, (screenHeight-400)/2);//设置窗体的位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭即退出程序
this.setTitle("我的第一个GUI窗体");//标题栏设置标题
this.setIconImage(tk.createImage("image/icon.png"));//设置标题栏图标
this.setResizable(false);//设置窗体改变大小的能力
this.addContent();
this.setVisible(true);//设置该窗体可见
}


private void addContent(){
this.contentP = this.getContentPane();//获取内容面板
this.contentP.setBackground(Color.WHITE);//设置窗体背景色
this.contentP.setLayout(null);//设置布局管理器为null---代表放入该容器的组件的大小位置全靠自定义

//文本标签
this.msgLab = new JLabel("用户名:");//产生对象
this.msgLab.setText("用户名:");
// this.msgLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));//给标签设置边框--调试用
this.msgLab.setFont(new Font("微软雅黑",Font.BOLD,16));//设置字体
this.msgLab.setForeground(new Color(82,254,211));//设置字体颜色
this.msgLab.setBounds(100, 20, 80, 30);//设置大小位置
this.contentP.add(this.msgLab);//放入容器

//图片标签
this.imgLab = new JLabel(new ImageIcon("image/fish.jpg"));
this.imgLab.setBounds(200, 20, 243, 167);
this.contentP.add(this.imgLab);

//文本框
this.usernameTxt = new JTextField();
this.usernameTxt.setBounds(20, 70, 100, 30);
this.usernameTxt.setFont(new Font("微软雅黑",Font.BOLD,16));//设置字体
this.usernameTxt.setForeground(new Color(82,254,211));//设置字体颜色
// this.usernameTxt.setEditable(false);//设置文本框不可编辑
this.contentP.add(this.usernameTxt);

//密码框
this.pwdTxt = new JPasswordField();
this.pwdTxt.setEchoChar('*');
this.pwdTxt.setFont(new Font("微软雅黑",Font.BOLD,16));//设置字体
this.pwdTxt.setForeground(new Color(82,254,211));//设置字体颜色
this.pwdTxt.setBounds(20, 120, 100, 30);
this.contentP.add(this.pwdTxt);

//按钮
this.okBtn = new JButton("确定");
this.okBtn.setText("确定");
this.okBtn.setFont(new Font("微软雅黑",Font.BOLD,16));//设置字体
this.okBtn.setForeground(new Color(82,254,211));//设置字体颜色
this.okBtn.setBounds(20, 160, 100, 30);
this.contentP.add(this.okBtn);

this.getMoentyBtn = new JButton(new ImageIcon("image/buttonGet.jpg"));
this.getMoentyBtn.setBounds(20, 200, 140, 50);
this.contentP.add(this.getMoentyBtn);

//下拉列表
this.teacherCmb = new JComboBox<String>();
this.teacherCmb.addItem("周春艳");
this.teacherCmb.addItem("刘弯弯");
this.teacherCmb.addItem("万洁");
this.teacherCmb.addItem("张欣");
this.teacherCmb.addItem("何茹薇");
this.teacherCmb.setEditable(true);//设置为可编辑为true
this.teacherCmb.setBounds(20, 260, 100, 20);
this.contentP.add(this.teacherCmb);

//文本域
this.selfArea = new JTextArea();
JScrollPane scrollP = new JScrollPane(this.selfArea);
scrollP.setBounds(200, 200, 280, 160);
this.contentP.add(scrollP);

//单选框
this.maleRad = new JRadioButton("男");
this.femaleRad = new JRadioButton("女");
this.maleRad.setBounds(20, 290, 50, 25);
this.femaleRad.setBounds(80, 290, 50, 25);
this.maleRad.setBackground(Color.WHITE);
this.femaleRad.setBackground(Color.WHITE);
this.maleRad.setSelected(true);//设置默认选中
this.contentP.add(this.maleRad);
this.contentP.add(this.femaleRad);
ButtonGroup bGroup = new ButtonGroup();//按钮分组
bGroup.add(this.maleRad);
bGroup.add(this.femaleRad);

//复选框
this.hobbitBox = new JCheckBox("兴趣爱好");
this.hobbitBox.setBounds(20, 325, 100, 25);
this.contentP.add(this.hobbitBox);
}

}

原文地址:https://www.cnblogs.com/fengshaolingyun/p/6785118.html