GUI图形界面

来源:互联网 发布:redis存储数据大小 编辑:程序博客网 时间:2024/05/01 12:43


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.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;


import com.lovo.bean.ClassBean;


public class MyFrame extends JFrame{
//内容面板---找窗体获取,不能new
private Container contentP;

//标签---JLabel---两种:放文字、放图像
private JLabel usernameLab;
private JLabel imgLab;

//文本框---JTextField
private JTextField inputTxt;

//密码框---JPasswordField
private JPasswordField pwdTxt;

//按钮
private JButton confirmBtn;
private JButton imageBtn;
private JButton txtImgeBtn;

//单选框---JRadionButton
//复选框---JCheckBox---可以自学
private JRadioButton maleBtn;
private JRadioButton femaleBtn;


//下拉列表---JComboBox
private JComboBox<String> comBox;
private JComboBox<ClassBean> classBox;

//文本域---JTextArea配合滚动条JScollBar
private JScrollPane jsp;
private JTextArea intrArea;


public MyFrame(){
this.setSize(500, 400);//设置窗体大小
Toolkit tk = Toolkit.getDefaultToolkit();//工具类
int screenW = (int)tk.getScreenSize().getWidth();//得到屏幕的宽
int screenH = (int)tk.getScreenSize().getHeight();//得到屏幕的高
this.setLocation((screenW - (int)this.getSize().getWidth())/2, 
(screenH - (int)this.getSize().getHeight()) / 2);//设置窗体的位置
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认的关闭操作
this.setResizable(false);//设置不能改变窗体尺寸

this.setTitle("我的第一个窗体");//设置标题
this.setIconImage(tk.createImage("img/xiaoxin.GIF"));//设置标题图标

this.addContent();

this.setVisible(true);//把窗体绘制在屏幕上
}

//添加内容
private void addContent(){
//首先获取到内容面板
this.contentP = this.getContentPane();
//其次把布局管理器设置为空
this.contentP.setLayout(null);
this.contentP.setBackground(Color.WHITE);

this.usernameLab =new JLabel();
this.usernameLab.setText("用户名不能为空");
this.usernameLab.setFont(new Font("微软雅黑", Font.ITALIC, 16));
this.usernameLab.setForeground(new Color(174,159,232));
this.usernameLab.setBorder(BorderFactory.createLineBorder(Color.BLACK));//添加线边框
this.usernameLab.setBounds(20, 20, 120, 25);
this.contentP.add(this.usernameLab);


this.imgLab = new JLabel();
this.imgLab.setIcon(new ImageIcon("img/eye.JPG"));
this.imgLab.setBounds(150, 5, 298, 111);
this.contentP.add(this.imgLab);


this.inputTxt = new JTextField();
this.inputTxt.setText("J129");
this.inputTxt.setEditable(false);//设置是否可编辑
this.inputTxt.setFont(new Font("微软雅黑", Font.ITALIC, 20));
this.inputTxt.setForeground(new Color(174,159,232));
this.inputTxt.setBounds(50, 125, 150, 35);
this.contentP.add(this.inputTxt);


this.pwdTxt = new JPasswordField();
this.pwdTxt.setEchoChar('*');
this.pwdTxt.setFont(new Font("微软雅黑", Font.ITALIC, 20));
this.pwdTxt.setForeground(new Color(174,159,232));
this.pwdTxt.setBounds(250, 125, 150, 35);
this.contentP.add(this.pwdTxt);


this.confirmBtn = new JButton();//产生按钮对象
this.confirmBtn.setEnabled(true);//设置按钮是否被点击
this.confirmBtn.setText("确定");//对按钮对象的属性进行设置
this.confirmBtn.setBounds(50,165, 80, 30);//对按钮的大小位置进行设置
this.contentP.add(this.confirmBtn);//内容面板添加按钮


this.imageBtn = new JButton();
this.imageBtn.setIcon(new ImageIcon("img/button.JPG"));
this.imageBtn.setBounds(150, 165, 74, 21);
this.contentP.add(this.imageBtn);


this.txtImgeBtn = new JButton();
this.txtImgeBtn.setText("确定");
this.txtImgeBtn.setIcon(new ImageIcon("img/xiaoxin.GIF"));
this.txtImgeBtn.setRolloverIcon(new ImageIcon("img/hp.JPG"));
this.txtImgeBtn.setBounds(235, 165, 120, 30);
this.contentP.add(this.txtImgeBtn);

this.maleBtn = new JRadioButton();
this.femaleBtn = new JRadioButton();
ButtonGroup bg = new ButtonGroup();//按钮组--没有外观,其作用是把多个按钮在逻辑上进行分组
bg.add(this.maleBtn);
bg.add(this.femaleBtn);
this.maleBtn.setText("男");
this.maleBtn.setSelected(true);
this.femaleBtn.setText("女");
this.maleBtn.setBounds(360, 165, 50, 30);
this.femaleBtn.setBounds(430, 165, 50, 30);
this.contentP.add(this.maleBtn);
this.contentP.add(this.femaleBtn);


this.comBox = new JComboBox<String>();
this.comBox.addItem("J125");
this.comBox.addItem("J126");
this.comBox.addItem("J127");
this.comBox.addItem("J128");
this.comBox.addItem("J129");
this.comBox.setEditable(true);
this.comBox.setBounds(20, 205, 100, 25);
this.contentP.add(this.comBox);

this.classBox = new JComboBox<ClassBean>();
this.classBox.addItem(new ClassBean("J125",28));
this.classBox.addItem(new ClassBean("J126",30));
this.classBox.addItem(new ClassBean("J127",31));
this.classBox.addItem(new ClassBean("J128",29));
this.classBox.addItem(new ClassBean("J129",32));
this.classBox.setBounds(130, 205, 100, 25);
this.contentP.add(this.classBox);


this.intrArea = new JTextArea();
this.jsp = new JScrollPane(this.intrArea);
this.jsp.setBounds(100, 240, 300, 130);
this.contentP.add(this.jsp);

}

}








测试代码

package com.lovo.test;


import java.awt.Toolkit;
import javax.swing.JFrame;


import com.lovo.frame.MyFrame;
import com.lovo.jpanel.GuessFrame;


public class TestMain {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame frame = new MyFrame();//产生窗体对象

}


}

0 0
原创粉丝点击