GUI编程入门到游戏实战

GUI编程入门到游戏实战,第1张

GUI编程入门到游戏实战 P2: awt 介绍
package gui.awt;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class PanelTest {
    public static void main(String[] args) {

        frame f1 = new frame("窗口 1");
        Panel p1 = new Panel();
        f1.setLayout(null);
        f1.setBounds(300,300,500,500);
        f1.setBackground(new Color(1,1,1));

        p1.setBounds(50,50,400,400);
        p1.setBackground(new Color(208, 37, 122));

        f1.add(p1);
        f1.setVisible(true);

        // 关闭窗口
        f1.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
}

P5: 三种布局管理器
  1. 流形布局 FlowLayout
  2. 东西南北中
  3. 表格布局
P7: 事件监听
package gui.actionlistener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ActionListen {

    public static void main(String[] args) {

        frame f1 = new frame("start--stop");
        Button b1 = new Button("start");
        Button b2 = new Button("stop");
        
        b2.setActionCommand("这里可以自定义");
        MyMoniter mm = new MyMoniter();
        b1.addActionListener(mm);
        b2.addActionListener(mm);
        
        f1.add(b1,BorderLayout.EAST);
        f1.add(b2,BorderLayout.WEST);
        f1.pack();
        
        f1.setVisible(true);

        f1.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

class MyMoniter implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("msg===> "+e.getActionCommand());
    }
}

package gui.actionlistener;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestText01 {
    public static void main(String[] args) {

        Myframe mf = new Myframe();
    }
}

class Myframe extends frame {
    public Myframe(){
        
        TextField tf = new TextField();     //文本框组件
        this.add(tf);                       //组件添加到窗上

        MyListener ml = new MyListener();   //创事件建监听器对象
        tf.addActionListener(ml);           //监听文本框

        tf.setEchoChar('*');                //加密

        this.setVisible(true);
        this.pack();

        //窗口监听器,关闭窗口
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//事件监听器
class MyListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField tf = (TextField) e.getSource();
        System.out.println(tf.getText());
        tf.setText("");
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5677028.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存