创新路
我们一直在努力

JAVA学习笔记(五)

AWT组件


练习目标:

1、生成一个可点叉能关闭的窗口;

2、窗口标题和图标自定义;

3、窗口背景色自定义;

4、窗口内含文本输入框、生成按钮和文本显示框;

5、按钮功能将文本输入框的内容提取到文本显示框;

6、提取内容后,输入框内容清空;

7、学习一些自动插入代码的快捷方法。

练习代码:

image.png

完整代码:

package explorer;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.WindowListener;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {

public static void main(String[] args) {

Frame f = new Frame("HDK_AWT_TEST");

f.setBounds(400, 200, 400, 300);

f.setLayout(new FlowLayout());

f.setAlwaysOnTop(true);

f.setBackground(Color.LIGHT_GRAY);

Toolkit tool = Toolkit.getDefaultToolkit();

Image image = tool.getImage("src/img/baidu.jpg");

f.setIconImage(image);

TextField tf = new TextField(20);

TextArea ta = new TextArea(10, 40);

Button bu = new Button("按钮");

bu.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String tf_str = tf.getText().trim();

tf.setText("");

ta.append(tf_str+"\r\n");

tf.requestFocus();

}

});

f.add(tf);

f.add(bu);

f.add(ta);

f.addWindowListener(new WindowListener() {

@Override

public void windowOpened(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowIconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowDeiconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowDeactivated(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowClosing(WindowEvent e) {

// TODO Auto-generated method stub

System.exit(0);

}

@Override

public void windowClosed(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowActivated(WindowEvent e) {

// TODO Auto-generated method stub

}

});

f.setVisible(true);

}

}


练习结果展示:

image.png

未经允许不得转载:天府数据港官方信息博客 » JAVA学习笔记(五)

客官点个赞呗! (0)
分享到:

评论 抢沙发

评论前必须登录!

天府云博 - 做有态度的开发&运维&设计学习分享平台!

联系我们百度云主机