package com.company; import java.awt.*; class myFrame extends Frame { static int id = 1; public myFrame (int x, int y, int w, int h ,Color color) { super("第" +(id ++ )+"个窗口"); setBounds(x, y, w, h); setVisible(true); setBackground(color); } } public class Main { public static void main(String []args){ myFrame frame01 = new myFrame(100,100,200,200,Color.red); frame01.setResizable(false); myFrame frame02 = new myFrame(300,100,200,200,Color.blue); myFrame frame03 = new myFrame(100,300,200,200,Color.black); myFrame frame04 = new myFrame(300,300,200,200,Color.pink); } }
public class lesson01 { public static void main(String []args){ new MyFrame(); } } class MyFrame extends Frame { public MyFrame(){ setVisible(true); TextField textfield = new TextField(); add(textfield); MyActionListener2 myActionListener2 = new MyActionListener2(); //按下enter键,就会触发事件 textfield.addActionListener(myActionListener2); //将文本框中的内容设置为* textfield.setEchoChar('*'); pack(); } } class MyActionListener2 implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { TextField textField = (TextField) e.getSource(); System.out.println(textField.getText()); textField.setText(""); } }
package com.company; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class lesson01 { public static void main(String []args){ new Calculator(); } } class Calculator extends Frame{ TextField t1,t2,t3; public Calculator(){ setLayout(new FlowLayout()); setVisible(true); Button b1 = new Button("="); Label label = new Label("+"); t1 = new TextField(10);//设置列数 t2 = new TextField(10); t3 = new TextField(20); b1.addActionListener(new MyCalculatorListener(this));//将自己代入实例 add(t1); add(label); add(t2); add(b1); add(t3); pack(); } } class MyCalculatorListener implements ActionListener{ Calculator calculator = null;//先将自己设置为空 public MyCalculatorListener(Calculator calculator){ this.calculator = calculator;//将引入的替换掉自己创建的 } @Override public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(calculator.t1.getText());//下面都是采用面向对象的方法 int b = Integer.parseInt(calculator.t2.getText()); calculator.t3.setText(""+(a+b)); calculator.t2.setText(""); calculator.t1.setText(""); } }
没错,就是用内部类的方法,他的最大好处就是可以畅通无阻的访问外部的属性和方法;
代码改进后如下
package com.company; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class lesson01 { public static void main(String []args){ new Calculator(); } } class Calculator extends Frame{ TextField t1,t2,t3; public Calculator(){ setLayout(new FlowLayout()); setVisible(true); Button b1 = new Button("="); Label label = new Label("+"); t1 = new TextField(10); t2 = new TextField(10); t3 = new TextField(20); b1.addActionListener(new MyCalculatorListener()); add(t1); add(label); add(t2); add(b1); add(t3); pack(); } private class MyCalculatorListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); t3.setText(""+(a+b)); t2.setText(""); t1.setText(""); } } }