学习GUI编程第二天笔记
文本框TextArea学习笔记
package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextArea01 {
public static void main(String[] args) {
//启动
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame() {
TextField textField = new TextField();
add(textField);
//监听这个文本框输入的文字
MyActionListener myactionListener = new MyActionListener();
//按下Enter,就会触发这个输入框的事件
textField.addActionListener(myactionListener);
//设置替换编码,提高安全性
textField.setEchoChar('*');
setVisible(true);
pack();
}
class MyActionListener implements ActionListener{
简易计算器
class A extends B{
}
class A{
public B b;
}
初始版
package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EasyCalc {
public static void main(String[] args) {
//启动
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
Frame frame = new Frame();
//三个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//一个Label
Label label = new Label("+");
//一个Button
Button button = new Button("=");
button.addActionListener(new MyCalcaculatorListenr(num1,num2,num3));
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
class MyCalcaculatorListenr implements ActionListener{
//获取三个变量
private TextField num1,num2,num3;
public MyCalcaculatorListenr(TextField num1,TextField num2,TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
完全改造为面向对象写法(减少继承的耦合性)
public class EasyCalc02 {
public static void main(String[] args) {
//启动
new Calculator1().loadFrame();
}
}
//计算器类
class Calculator1 extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
setLayout(new FlowLayout());
button.addActionListener(new MyCalcaculatorListenr(this));
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
class MyCalcaculatorListenr implements ActionListener{
//获取计算器这个类,在一个类中组合另外一个类
Calculator1 calculator1 = null;
public MyCalcaculatorListenr(Calculator1 calculator){
//1.获得加数和被加数
//2.加法
//3.清空前两个框
this.calculator1 = calculator;
}
内部类
-
更好的包装(可以直接调用类里的属性,无需传参)
public class EasyCalc02 {
public static void main(String[] args) {
//启动
new Calculator1().loadFrame();
}
}
//计算器类
class Calculator1 extends Frame{
//属性
TextField num1,num2,num3;
//方法
public void loadFrame(){
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
setLayout(new FlowLayout());
button.addActionListener(new MyCalcaculatorListenr());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
private class MyCalcaculatorListenr implements ActionListener{
画笔
public class PaintTest {
public static void main(String[] args) {
new Mypaint().loadFrame();
}
}
class Mypaint extends Frame {
public void loadFrame(){
setBounds(200,200,600,500);
setVisible(true);
}
//画笔
public void paint(Graphics g){
//画笔,需要颜色,画笔可以画画
g.setColor(Color.RED);
g.drawOval(100,100,50,50);
g.setColor(Color.BLUE);
g.drawRect(200,200,50,50);
g.setColor(Color.BLACK);
}
}