---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
//22-3
import java.awt.*;
class AwtDemo
{
public static void main(String[] args)
{
Frame f = new Frame("Frame-show");
f.setSize(500, 400);
f.setLocation(300,300);
f.setLayout(new FlowLayout());
Button b = new Button("a button");
f.add(b);
f.setVisible(true);
System.out.println("Hello World!");
}
}
/*
sum:
步骤:
1.创建Frame窗体。
2.对窗体基本设置,如大小,位置,布局。
3.定义组件。
4.将组件通过窗体的add方法加到窗体中。
5.让窗体显示,通过setVisible(true);
Frame f = new Frame("Frame-show");//1
f.setSize(500, 400);//2
f.setLocation(300,300);//2
f.setLayout(new FlowLayout());//2
Button b = new Button("a button");//3
f.add(b);//4
f.setVisible(true);//5
*/
/*
事件监听机制的特点:
1.事件源。
2.事件
3.监听器
4.事件处理。
事件源:组件
事件:事件源有对应的事件
监听器:将可以触发某一事件的动作(可以有多个动作)已经封装到监听器中了。
*///22-6
import java.awt.*;
import java.awt.event.*;
class FrameDemo
{
//定义该图形属性的组件引用
private Frame f;
private Button b;
FrameDemo(){
init();
}
public void init(){
f = new Frame("Frame");
f.setBounds(300,300,500,500);
f.setLayout(new FlowLayout());
b = new Button("button");
f.add(b);
myEvent();//
myButtonEvent();//
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(new WindowAdapter(){//
public void windowClosing(WindowEvent e){//
System.exit(0);//
}
});
}
private void myButtonEvent(){
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){//
System.out.println("exit");
f.setVisible(false);
//System.exit(0);
}
});
}
public static void main(String[] args)
{
new FrameDemo();//
new FrameDemo();
System.out.println("Hello World!");
}
}
/*
sum:
private void myEvent(){
f.addWindowListener(new WindowAdapter(){//
public void windowClosing(WindowEvent e){//
System.exit(0);//
}
});
}
new ActionListener();
public void actionPerformed(ActionEvent e)
*/
//22-7_8
import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent
{
private Frame f;
private Button b;
private TextField tf;
MouseAndKeyEvent(){
init();
}
public void init(){
f = new Frame("Frame");
f.setBounds(300,300,500,500);
f.setLayout(new FlowLayout());
tf = new TextField(25);
b = new Button("button");
f.add(tf);
f.add(b);
myEvent();//
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(new WindowAdapter(){//
public void windowClosing(WindowEvent e){//
System.exit(0);//
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Action performed");
}
});
b.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if (e.getClickCount() == 2)
{
System.out.println("Mouse clicked");
}
}
});
b.addMouseListener(new MouseAdapter(){
private int count =0;
public void mouseEntered(MouseEvent e){
System.out.println("count="+count++);
}
});
b.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//组合键
System.out.println("c+e");
System.out.println(e.getKeyCode()+"::"+e.getKeyChar()+"::"+e.getKeyText(e.getKeyCode()));
}
});
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9))
{
System.out.println("please enter numbers");
e.consume();
}
}
});
}
public static void main(String[] args)
{
new MouseAndKeyEvent();
System.out.println("Hello World!");
}
}
/*
sum:
e.getClickCount();
MouseEvent 类;
actionPerformed 类在 mouse 后动后,但任何输入设备都会产生actionPerformed;
*/
/*
sum:
组合键;
e.getKeyText(e.getKeyCode());
e.consume();//取消默认的事件处理方式。
e.isControlDown();
*/
//22-9_10
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{
private Frame f;
private TextField tf;
private Button b;
private TextArea ta;
private Dialog d;
private Button okb;
private Label l;
MyWindowDemo(){
init();
}
public void init(){
f = new Frame("my window");
f.setBounds(300,300,600,500);
f.setLayout(new FlowLayout());
d = new Dialog(f,"show",true);
l = new Label();
okb = new Button("OK");
d.setBounds(350,350,200,100);
d.setLayout(new FlowLayout());
tf = new TextField(40);
b = new Button("button");
ta = new TextArea(50,40);
f.add(tf);
f.add(b);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent(){
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
showDialog();
}
});
okb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDialog();
}
});
}
public void showDialog(){
//Dialog d = new Dialog(f,"show",true);
//Button okb = new Button("OK");
//Label l = new Label();
String dirPath = tf.getText();
String s = "路径"+dirPath+"错误。Input again";
d.add(l);
//d.add(okb);
//l.setText(s);
File dirs = new File(dirPath);//
if(dirs.exists()&&dirs.isDirectory()){//
String[] names = dirs.list();
ta.setText("");//
for (String name : names)
{
ta.append(name+"\r\n");
}
}else{
d.add(okb);
l.setText(s);
d.setVisible(true);
}
}
public static void main(String[] args)
{
new MyWindowDemo();
System.out.println("Hello World!");
}
}
/*
sum:
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String dirPath = tf.getText();
File dirs = new File(dirPath);//
if(dirs.exists()&&dirs.isDirectory()){//
String[] names = dirs.list();
ta.setText("");//
for (String name : names)
{
ta.append(name+"\r\n");
}
}
}
});
*/
//22-11_13
package mymenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuDemo
{
private Frame f;
private MenuBar mb;
private Menu m, si;
private MenuItem closeItem,subItem,openItem,saveItem;
private FileDialog fdo,fds;
private TextArea ta;
private File file;
MyMenuDemo(){
init();
}
public void init(){
f = new Frame("my window");
f.setBounds(200, 200, 500, 400);
//f.setLayout(new FlowLayout());
ta = new TextArea();
fdo = new FileDialog(f,"open",FileDialog.LOAD);//
fds = new FileDialog(f,"open",FileDialog.SAVE);//
mb = new MenuBar();//
m = new Menu("文件");//
closeItem = new MenuItem("exit");//
subItem = new MenuItem("subItem");
openItem = new MenuItem("open");
saveItem = new MenuItem("save");
si = new Menu("s");
m.add(closeItem);
m.add(openItem);
m.add(saveItem);
mb.add(m);
f.setMenuBar(mb);
f.add(ta);
m.add(si);
si.add(subItem);
myEvent();
f.setVisible(true);
}
private void myEvent(){
openItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s = null;
fdo.setVisible(true);
String dir = fdo.getDirectory();
String fileName = fdo.getFile();
if(dir==null||fileName==null)
return;
file = new File(dir,fileName);
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(file));
while ((s = br.readLine())!=null)
{
ta.append(s+"\r\n");//
}
}
catch (Exception ex)
{throw new RuntimeException ("读取失败");
}
finally{
try
{
if(br!=null)
br.close();
}
catch (Exception excp)
{throw new RuntimeException("文件关闭失败");
}
}
}
});
saveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s = null;
if(file==null){//
fds.setVisible(true);//
String dir = fds.getDirectory();//
String fileName = fds.getFile();//
if(dir==null||fileName==null)//
return;
file = new File(dir,fileName);//
}
BufferedWriter bw = null;
try
{
String ss = ta.getText();
bw = new BufferedWriter(new FileWriter(file));
bw.write(ss);
}
catch (Exception ex)
{throw new RuntimeException ("读取失败");
}
finally{
try
{
if(bw!=null)
bw.close();
}
catch (Exception excp)
{throw new RuntimeException("文件关闭失败");
}
}
}
});
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args)
{
new MyMenuDemo();
System.out.println("Hello World!");
}
}
/*
sum:
mb = new MenuBar();
m = new Menu("文件");
closeItem = new MenuItem("exit");
*/
/*
sum:
fdo = new FileDialog(f,"open",FileDialog.LOAD);//
fds = new FileDialog(f,"open",FileDialog.SAVE);//
String dir = fds.getDirectory();//
String fileName = fds.getFile();//
if(dir==null||fileName==null)//
return;
File file = new File(dir,fileName);//
ta.append(s+"\r\n");//
*/
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------