GUI编程
- 窗口
- 弹窗
- 面板
- 文本框
- 列表栏
- 按钮
- 图片
- 监听
- 鼠标键盘
1、简介
Gui的核心技术:swing AWT
- 因为界面不美观
- 需要jre环境
为什么要学习 - 可以写出自己心中想要一些小工具
- 工作的时候,也可能维护到swing界面,概率较小
- 了解MVC架构,了解监听
AWT
AWT介绍 active windows tools
new类!
包含了很多的类和接口!
Gui:图形用户编程
Eclipse:Java写的
Frame
封装frame
public class TestFrame1 {
public static void main(String[] args) {
//展示多个窗口 new
MyFrame myFrame= new MyFrame(100,100,200,200,Color.black);
}
}
class MyFrame extends Frame{
static int id=0;//可能存在多个窗口 我们需要一个计数器
public MyFrame(int x,int y,int w,int h,Color color){
super("MyFrame"+(++id));
setVisible(true);
setBounds(x,y,w,h);
}
}
2、pannel
//Panel 可以看成一个空间,但是不能单独存在
public class TestPanel {
public static void main(String[] args) {
Frame frame=new Frame();
//布局的概念
Panel panel=new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,300,500,500);
//panel设置坐标,相对于frame
panel.setBounds(50,50,400,400);
panel.setBackground(new Color(99, 128, 59));
//将组件加入进去
frame.add(panel);
frame.setVisible(true);
//监听事件,监听窗口关闭事件
frame.addWindowListener(new WindowAdapter() {//这个是windowlistener的子类
//窗口点击关闭是要做的事情
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
监听事件
自我总结重点
监听器定义和js基本一致
原件.addActionListener(对象)
只不过正常对象喜欢写在底下class并且继承系统自带方法的对象并加以使用
也可以这么理解
原件.addActionListener(new 系统自带方法的对象{
@Override
public void 想要监听的事件(监听事件 e) {
监听到了执行的代码;
}
})
退出程序和点击按钮
public class TestActionEvent {
public static void main(String[] args) {
Frame frame=new Frame();
Button button=new Button();
//因为actionliserner()需要一个ActionListener,所以我们需要构造一个ActionListener
MyActionListener myActionListener=new MyActionListener();
button.addActionListener(myActionListener);
frame.add(button);
frame.pack();
frame.setVisible(true);
}
//关闭窗体事件
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("111");
}
}
计算机监听事件
public class TestText01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField=new TextField();
this.setBounds(50,50,400,400);
add(textField);
//监听这个文本框输入的文字
// MyActionListener myActionListener=new MyActionListener();
// textField.addActionListener(myActionListener);
//按下enter就会触发输入框事件
textField.addActionListener(new ActionListener() {
@Override
//事件触发是回车键
public void actionPerformed(ActionEvent e) {
TextField filed=(TextField) e.getSource();
filed.getText();//获得输入框中的文本
System.out.println(filed.getText());
//清空代码
filed.setText("");
}
});
//设置编码
//将所有的输入变成*
textField.setEchoChar('*');
setVisible(true);
pack();
//底下这段是退出程序
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
e.getSource();//获得资源返回对象
}
}
窗口监听
public class TestWindow {
public static void main(String[] args) {
new WindowFrame();
}
}
class WindowFrame extends Frame{
public WindowFrame(){
setVisible(true);
setBackground(Color.blue);
setBounds(100,100,100,100);
// addWindowListener(new MyWindowLiserner());
this.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口已打卡");
}
@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) {
WindowFrame s=(WindowFrame) e.getSource();
s.setTitle("激活");
System.out.println("窗口被激活");
}
});
}
}
键盘监听
public class TestKey {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("key的type");
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("key的press");
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按了上键");
}
}
@Override
public void keyReleased(KeyEvent e) {
System.out.println("key的release");
}
});
}
}