GUI图形界面续

来源:互联网 发布:国内软件开发公司 编辑:程序博客网 时间:2024/05/22 10:24



事件监听的代码

package com.lovo.event;


import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;


//监听器实现方式1-------单独书写一个类,来实现监听器
//特点:1、即可以为不同的事件源书写独立的监听器(设计上满足单一职责),也可以为多个事件源书写一个监听器(这多个事件源有非常强的逻辑一致性)
//    2、若要操作除事件源之外的容器或组件对象,必须要传参!
public class RedBtnListener implements ActionListener {

private Container contentP;
private JButton redBtn;
private JButton greenBtn;

public RedBtnListener(Container contentP,JButton redBtn,JButton greenBtn){
this.contentP = contentP;
this.redBtn = redBtn;
this.greenBtn = greenBtn;
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton btn = (JButton)e.getSource();
String txt = btn.getText();
if(txt.equals("红色")){
this.contentP.setBackground(Color.RED);
this.redBtn.setEnabled(false);
this.greenBtn.setEnabled(true);
}else if(txt.equals("绿色")){
this.contentP.setBackground(Color.GREEN);
this.redBtn.setEnabled(true);
this.greenBtn.setEnabled(false);

}
}


}






package com.lovo.event;


import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;


//监听器实现方式2---容器充当监听器
//特点 :1、只能书写一个监听方法监听所有的事件源对象(不满足单一职责)
//    2、访问当前容器的组件或子容器,不传参
//public class ColorFrame extends JFrame implements ActionListener{
public class ColorFrame extends JFrame{
private Container contentP;

private JButton redBtn;

private JButton greenBtn;

public ColorFrame() {
// TODO Auto-generated constructor stub
this.setSize(200, 300);//设置窗体大小
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() {
// TODO Auto-generated method stub
this.contentP = this.getContentPane();
//FlowLayout--流布局
this.contentP.setLayout(new FlowLayout());//设置它的布局管理器

this.redBtn = new JButton("红色");
this.greenBtn = new JButton("绿色");

this.contentP.add(this.redBtn);
this.contentP.add(this.greenBtn);

//监听器对象与事件源对象的绑定
//监听器实现方式1的绑定代码
// RedBtnListener rbL = new RedBtnListener(this.contentP,this.redBtn,this.greenBtn);
// this.redBtn.addActionListener(rbL);
// this.greenBtn.addActionListener(rbL);

//监听器实现方式2的绑定代码
// this.redBtn.addActionListener(this);
// this.greenBtn.addActionListener(this);


//监听器实现方式3---监听器绑定的时候,同时实现监听器接口
//特点:1、不同组件各自独立实现监听器(单一职责)
//    2、利用内部类与外部类的关系,访问当前容器的组件或子容器,也不需要传参 
this.redBtn.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ColorFrame.this.contentP.setBackground(Color.RED);
ColorFrame.this.redBtn.setEnabled(false);
ColorFrame.this.greenBtn.setEnabled(true);
}

});


this.greenBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
ColorFrame.this.contentP.setBackground(Color.GREEN);
ColorFrame.this.redBtn.setEnabled(true);
ColorFrame.this.greenBtn.setEnabled(false);
}
});




}


// @Override
// public void actionPerformed(ActionEvent e) {
// // TODO Auto-generated method stub
// JButton btn = (JButton)e.getSource();
// String txt = btn.getText();
// if(txt.equals("红色")){
// this.contentP.setBackground(Color.RED);
// this.redBtn.setEnabled(false);
// this.greenBtn.setEnabled(true);
// }else if(txt.equals("绿色")){
// this.contentP.setBackground(Color.GREEN);
// this.redBtn.setEnabled(true);
// this.greenBtn.setEnabled(false);
// }
// }

}





布局管理器



package com.lovo.layout;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;


import javax.swing.JButton;
import javax.swing.JFrame;


//BorderLayout---边界布局管理器
//主要用于放置中间容器---特别适合分配外部布局的
//口诀--东西南北中,南北要贯通,中间最大(位置最大,权利最大 )
public class BorderFrame extends JFrame {

private Container contentP;

private JButton jbtn1;
private JButton jbtn2;
private JButton jbtn3;
private JButton jbtn4;
private JButton jbtn5;

public BorderFrame(){
this.setSize(447, 480);//设置窗体大小
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(true);//设置不能改变窗体尺寸

this.setTitle("边界布局管理器");//设置标题
this.setIconImage(tk.createImage("img/xiaoxin.GIF"));//设置标题图标

this.addContent();

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


private void addContent() {
// TODO Auto-generated method stub
this.contentP = this.getContentPane();

//设置布局管理器
this.contentP.setLayout(new BorderLayout());

this.jbtn1 = new JButton("按钮1");
this.jbtn2 = new JButton("按钮2");
this.jbtn3 = new JButton("按钮3");
this.jbtn4 = new JButton("按钮4");
this.jbtn5 = new JButton("按钮5");

this.contentP.add(this.jbtn1, BorderLayout.CENTER);
this.contentP.add(this.jbtn2, BorderLayout.EAST);
this.contentP.add(this.jbtn3, BorderLayout.WEST);
this.contentP.add(this.jbtn4, BorderLayout.SOUTH);
this.contentP.add(this.jbtn5, BorderLayout.NORTH);
}

}



package com.lovo.layout;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Toolkit;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;




//流布局主要用于放置组件---特别适合于放置单行的,按顺序排列的组件
//位置--按从左往右,从上往下,从中间开始放置
//大小--根据文本内容自动调整
public class FlowFrame extends JFrame {

private Container contentP;

private JButton jbtn1;
private JButton jbtn2;
private JButton jbtn3;
private JButton jbtn4;
private JButton jbtn5;
private JTextField inputTxt;

public FlowFrame(){
this.setSize(300, 300);//设置窗体大小
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(true);//设置不能改变窗体尺寸

this.setTitle("流布局管理器");//设置标题
this.setIconImage(tk.createImage("img/xiaoxin.GIF"));//设置标题图标

this.addContent();

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


private void addContent() {
// TODO Auto-generated method stub
this.contentP = this.getContentPane();

//设置布局管理器
this.contentP.setLayout(new FlowLayout());

this.jbtn1 = new JButton("按钮1");
this.jbtn2 = new JButton("按钮2");
this.jbtn3 = new JButton("按钮3");
this.jbtn4 = new JButton("按钮4");
this.jbtn5 = new JButton("按钮5是一个很长的按钮,有很长的文本");
this.inputTxt = new JTextField();
this.inputTxt.setColumns(10);


this.contentP.add(this.jbtn1);
this.contentP.add(this.jbtn2);
this.contentP.add(this.jbtn3);
this.contentP.add(this.jbtn4);
this.contentP.add(this.jbtn5);
this.contentP.add(this.inputTxt);

}

}




package com.lovo.layout;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Toolkit;


import javax.swing.JButton;
import javax.swing.JFrame;


//网格布局管理器--GridLayout
//按行列,均分外部容器
public class GridFrame extends JFrame {

private Container contentP;

private JButton jbtn1;
private JButton jbtn2;
private JButton jbtn3;
private JButton jbtn4;
private JButton jbtn5;

public GridFrame(){
this.setSize(447, 480);//设置窗体大小
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(true);//设置不能改变窗体尺寸

this.setTitle("网格布局管理器");//设置标题
this.setIconImage(tk.createImage("img/xiaoxin.GIF"));//设置标题图标

this.addContent();

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


private void addContent() {
// TODO Auto-generated method stub
this.contentP = this.getContentPane();

//设置布局管理器
this.contentP.setLayout(new GridLayout(3, 1));

this.jbtn1 = new JButton("按钮1");
this.jbtn2 = new JButton("按钮2");
this.jbtn3 = new JButton("按钮3");
this.jbtn4 = new JButton("按钮4");
this.jbtn5 = new JButton("按钮5");

this.contentP.add(this.jbtn1);
this.contentP.add(this.jbtn2);
this.contentP.add(this.jbtn3);
// this.contentP.add(this.jbtn4);
// this.contentP.add(this.jbtn5);

}

}



测试代码


package com.lovo.test;


import com.lovo.event.ColorFrame;
import com.lovo.layout.BorderFrame;
import com.lovo.layout.FlowFrame;
import com.lovo.layout.GridFrame;
import com.lovo.layout.card.CardFrame;


public class TestMain {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

// new BorderFrame();
// new FlowFrame();
new GridFrame();


}


}

0 0
原创粉丝点击