Java GUI的基本使用

布局,窗口,按钮监听事件

package Test1;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Frame f=new Frame("我的第一个窗口");
		
		f.setSize(500, 500);//
		f.setLocation(600, 500);//
		f.setBounds(500, 500, 500, 500);
		f.setLayout(new FlowLayout());//	设置流式布局
		Color bgColor=new Color(546854);
		f.setBackground(bgColor);
		Button but=new Button("我是一个按钮");
		f.add(but);
		f.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.out.println("closing..."+e);
				System.exit(0);
			}
			
		});
		but.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("button run...");
				System.exit(0);
			}
		});
		f.setVisible(true);
		
	}

}

鼠标键盘监听事件

package Test1;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MouseAndKey {

		private Frame f;
		private TextField tf;
		private Button but;
		public MouseAndKey() {
			init();
		}
		private void init() {
			// TODO Auto-generated method stub
			f=new Frame("演示鼠标和键盘监听");
			f.setBounds(500, 500, 500, 500);
			f.setLayout(new FlowLayout());
			tf=new TextField(15);
			but=new Button("一个按钮");
			f.add(tf);
			f.add(but);
			myEvent();
			f.setVisible(true);
		}
		private void myEvent() {
			// TODO Auto-generated method stub
			tf.addKeyListener(new KeyAdapter() {
				StringBuilder s=new StringBuilder();
				@Override
				public void keyPressed(KeyEvent e) {
					// TODO Auto-generated method stub
					s.append(e.getKeyText(e.getKeyCode()));
					System.out.println("jianru"+e.getKeyText(e.getKeyCode())+":"+e.getKeyCode()+s);
				}
				
			});
			f.addWindowListener(new WindowAdapter() {

				@Override
				public void windowClosing(WindowEvent e) {
					// TODO Auto-generated method stub
					System.exit(0);
				}
				
			});	
			//在按钮上添加一个鼠标监听
			but.addMouseListener(new MouseAdapter() {
				private int count;
				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					tf.setText(count+"");
					System.out.println("mouse enter:"+count++);
				}
				@Override
				public void mouseClicked(MouseEvent arg0) {
					// TODO Auto-generated method stub
					if(arg0.getClickCount()==2)
					{
						tf.setText("mouse double click"+count++);
					}
				}
				
				
			});
			but.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					System.out.println("enter");
				}
			});
			
		}
		
		public static void main(String[] args) {
			new MouseAndKey();
		}
		
}

 

你可能感兴趣的:(Java基础)