第十六章

来源:互联网 发布:网络摄像头ping不通 编辑:程序博客网 时间:2024/04/27 20:28

第十六章 Java图形界面编辑

Java.awt(抽象、窗体、工具toolkit):

1.容器:Frame

2.组件:Button

3.颜色:Colour

4.字体:Font

Javax swing(加强兼容性):

1.容器: JFrame。

2.组件:JButton。

swt的开发方式为awt+swing。

首层容器有四种:

1.JWindows:只有框。

2.JFrame:有最大,最小化和关闭等。是一种常用的。

3.JOptionPan:弹出框

4.JApplet:已基本被淘汰了。

使用方法:

public class MyFrame extends JFrame{

private Container contentP;//内容面板private JLabel nameLab;//用户名标签private JLabel imageLab;//图片标签private JTextField nameText;//文本框private JPasswordField pwdTxt;//密码框private JComboBox genderCom;//下拉框private JButton okBtn;//按钮private JRadioButton maleRad;//单选框private JRadioButton femaleRad;private JTextArea contentArea;//文本域private JScrollPane scrollP;//滚动条面板--配合文本域 

其中增加了MyFrame的带参构造。

public MyFrame()
{
首先使用了一个工具类:toolkit

可以获取屏幕信息,还可以操作图标图片。1.工具类:Toolkit tk = Toolkit.getDefaultToolkit();2.设置标题:this.setTitle("标题名字");3.设置图片:this.setIconImage(tk.createImage("文件夹名"+\(File.separator)+"文件名字.格式"));4.设置窗体大小:this.setSize(x,y)先水平后垂直。5.设置位置:this.setLocation((int)(tk.getScreenSize().getWidth()-500)/2,        (int)(tk.getScreenSize().getHeight()-300)/2);6.设置窗体大小不可变:this.setResizable(false);7.设置关闭:this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);8.放置内容样本:this.addContentP();9.设置可见:this.setVisible(true);注意一定要在最后!!!}

}

Frame窗体中组件及使用方法(部分作为属性):

private void addConcentP(){

    专门用来操作内容面板上的内容}

一、内容样板Container

private Container contentP;

1.获取内容面板:this.ContentP=this.getContentPane(); 2.设置颜色:this.ContentP.setBackgroud(Colour.WHITE);或者new Colour(1,1,1);三原色3.设置内容布置的管理器为-null,使用绝对的定位来放置组件。this.ContentP.setLayout(null);

二、放置内容标签JLabel

1.产生对象:this.nameLab=new JLabel();2.设置文本:this.nameLab.setText("名字");3.设置字体:this.nameLab.setFont(new setFont("字体格式",Font.ITALIC,多少号));4.设置前景色:this.nameLab.Foreground(Colour.BLACK);5.设置边框:this.nameLab.setBorder(边境)(BorderFactory.createLineBorder            (Colour.BLACK));用于调试6.设置位置和大小:this.nameLab.setBounds(x,y,w,h);7.放入容器中: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);
0 0
原创粉丝点击