Java基础
GUI编程
核心技术:Swing、AWT
现在GUI并不流行 因为其界面不美观、需要依赖jre环境
事件监听
意为:当某个事情发生的时候,要干什么
/**
* 事件监听
* @author xue
*/
public class Demo5 {
public static void main(String[] args) {
Frame frame = new Frame();
Button but = new Button("but1");
//按钮事件 addActionListener需要一个ActionListener对象 需要构造出来
MyActionListener myActionListener = new MyActionListener();
but.addActionListener(myActionListener);
frame.add(but,BorderLayout.CENTER);
frame.setSize(500,500);
frame.setVisible(true);
windowClose(frame);
}
//关闭窗口
//你猜这里为什么用private
private static void windowClose(Frame frame) {
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("nihao");
}
}
/**
* 实现一个监听事件多个按钮使用
* @author xue
*/
public class Demo6 {
public static void main(String[] args) {
Frame frame = new Frame();
Button but1 = new Button("start");
Button but2 = new Button("stop");
but2.setActionCommand("bye");//原有的标签信息将会被覆盖为bye
MyMonitor myMonitor = new MyMonitor();
but1.addActionListener(myMonitor);
but2.addActionListener(myMonitor);
frame.add(but1,BorderLayout.EAST);
frame.add(but2,BorderLayout.WEST);
frame.setVisible(true);
frame.setSize(500,500);
}
}
class MyMonitor implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//可以获取按钮的信息
System.out.println(e.getActionCommand());
if (e.getActionCommand().equals("start")) {
System.out.println("是可以开始的时候了");
}
if (e.getActionCommand().equals("stop")) {
System.out.println("是该结束了");
}
}
}
输入框
public class Demo7 {
public static void main(String[] args) {
MyFrame2 myFrame2 = new MyFrame2();
}
}
class MyFrame2 extends Frame {
public MyFrame2() {
TextField tf = new TextField();
add(tf);
MyActionListener2 myActionListener2 = new MyActionListener2();
//监听文本框输入的内容 enter为触发条件
tf.addActionListener(myActionListener2);
tf.setEchoChar('*');//设置输入的内容形式替换 输入内容全显示为* 但后台获取的实际输入的内容
setVisible(true);
pack();//窗口自适应
}
}
class MyActionListener2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField tf = (TextField) e.getSource();
System.out.println(tf.getText());//获取输入框中的所有内容包括转义字符
tf.setText(""); //回车即清空内容
}
}
练习简易计算器
public class Caulator {
public static void main(String[] args) {
new NewFrame();
}
}
class NewFrame extends Frame{
public NewFrame() {
//三个文本框
TextField tf1 = new TextField(10);
TextField tf2 = new TextField(10);
TextField tf3 = new TextField(20);
//一个按钮
Button but = new Button("=");
//一个标签
Label label = new Label("+");
//设置为流布局
setLayout(new FlowLayout());
add(tf1);
add(label);
add(tf2);
add(but);
add(tf3);
setVisible(true);
setSize(600,200);
//按钮的监听事件
but.addActionListener(new NewActionListener(tf1,tf2,tf3));
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class NewActionListener implements ActionListener{
//通过构造方法传递文本属性
private TextField tf1,tf2,tf3;
public NewActionListener(TextField tf1,TextField tf2,TextField tf3){
this.tf1=tf1;
this.tf2=tf2;
this.tf3=tf3;
}
@Override
public void actionPerformed(ActionEvent e) {
//获取文本的值
int num1 = Integer.parseInt(tf1.getText());
int num2 = Integer.parseInt(tf2.getText());
tf3.setText(""+(num1+num2));
//清空文本框
tf1.setText("");
tf2.setText("");
}
}
/**
优化1.1 减少代码用于
使用组合
*/
public class Caulator {
public static void main(String[] args) {
new NewFrame().loadCaul();
}
}
class NewFrame extends Frame{
//对象-属性
TextField tf1,tf2,tf3;
//对象-方法
public void loadCaul(){
//三个文本框
tf1 = new TextField(10);
tf2 = new TextField(10);
tf3 = new TextField(20);
//一个按钮
Button but = new Button("=");
//一个标签
Label label = new Label("+");
//设置为流布局
setLayout(new FlowLayout());
add(tf1);
add(label);
add(tf2);
add(but);
add(tf3);
setVisible(true);
setSize(600,200);
//按钮的监听事件
but.addActionListener(new NewActionListener(this));
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class NewActionListener implements ActionListener{
//通过创建NewFrame对象获取其属性
NewFrame nf = null;
public NewActionListener(NewFrame nf){
this.nf=nf;
}
@Override
public void actionPerformed(ActionEvent e) {
//获取文本的值
int num1 = Integer.parseInt(nf.tf1.getText());
int num2 = Integer.parseInt(nf.tf2.getText());
nf.tf3.setText(""+(num1+num2));
//清空文本框
nf.tf1.setText("");
nf.tf2.setText("");
}
}
/**
*优化1.2 内部类减少代码量
* 内部类的最大好处是可以直接使用外部类的属性和方法
*/
public class Caulator {
public static void main(String[] args) {
new NewFrame().loadCaul();
}
}
class NewFrame extends Frame{
//对象-属性
TextField tf1,tf2,tf3;
//对象-方法
public void loadCaul(){
//三个文本框
tf1 = new TextField(10);
tf2 = new TextField(10);
tf3 = new TextField(20);
//一个按钮
Button but = new Button("=");
//一个标签
Label label = new Label("+");
//设置为流布局
setLayout(new FlowLayout());
add(tf1);
add(label);
add(tf2);
add(but);
add(tf3);
setVisible(true);
setSize(600,200);
//按钮的监听事件
but.addActionListener(new NewActionListener());
//关闭窗口
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//私有化 监听事件自用
private class NewActionListener implements ActionListener{
//可以直接获取NewFrame对象的属性
@Override
public void actionPerformed(ActionEvent e) {
//获取文本的值
int num1 = Integer.parseInt(tf1.getText());
int num2 = Integer.parseInt(tf2.getText());
tf3.setText(""+(num1+num2));
//清空文本框
tf1.setText("");
tf2.setText("");
}
}
}
画笔
/**
* 画笔的使用Paint
* @author xue
*/
public class Demo8 {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame() {
setBounds(200,200,600,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//画笔方法 同toString 自动隐式调用
@Override
public void paint(Graphics g) {
// g.setColor(Color.red); //需要又颜色才能花花
g.drawOval(100,100,100,100);
g.fillOval(250,100,100,100);//实心的圆
g.fillRect(400,100,100,100); //实心矩形
//这里注意用完最好将颜色还原为最初的颜色
}
}
鼠标监听
/**
* 简易画图上画点
*/
public class Practice2 {
public static void main(String[] args) {
new NewPaint("画画");
}
}
//需要一个画板画画
class NewPaint extends Frame{
ArrayList points;
public NewPaint(String str){
super(str);
setBounds(500,500,800,600);//画板的位置和大小
setVisible(true);
points = new ArrayList();
addMouseListener(new MouseLister());
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
@Override
public void paint(Graphics g) {
Iterator it = points.iterator();
while (it.hasNext()){
Point p = (Point) it.next();
g.setColor(Color.cyan);
g.fillOval(p.x,p.y,5,5);
}
}
//把点存入集合
public void addPoint(Point p) {
points.add(p);
}
//监听内部类
private class MouseLister extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
NewPaint mouPre = (NewPaint) e.getSource();
mouPre.points.add(new Point(e.getX(),e.getY()));
// mouPre.addPoint(new Point(e.getX(),e.getY()));
//画完一次就刷新重新开始
mouPre.repaint();
}
}
}
窗口监听
public class Demo9 {
public static void main(String[] args) {
new NewFrame2();
}
}
class NewFrame2 extends Frame{
public NewFrame2() {
setVisible(true);
setSize(600,400);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("正在关闭");
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("我关闭了");
}
@Override
public void windowActivated(WindowEvent e) {
e.getSource();//是获取事件的发起者
System.out.println("快来点我");
setTitle("快来点我");
}
});
}
}
键盘监听
public class Demo10 {
public static void main(String[] args) {
new KeyBord();
}
}
class KeyBord extends Frame{
public KeyBord() {
setVisible(true);
setSize(300,500);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//e.getKeyCode();获取键盘上键的码
String str = String.valueOf((char)e.getKeyCode());
System.out.println(str);
}
});
}
}
标签:java,22,void,day20,class,add,new,TextField,public
From: https://www.cnblogs.com/onlyxue/p/16843805.html