编程语言
首页 > 编程语言> > 关于GUI编程初步了解与运用,以及组合和内部类的思想

关于GUI编程初步了解与运用,以及组合和内部类的思想

作者:互联网

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);
}
}

布局

setLayout(new flowLayout(flowLayout.Right))流式布局,此时设置了靠右,可以更改为LEFT,CENTER等

frame.add(xxx,borderLayout.EAST)东西南北中式布局,其中的EAST可以更改为WEST,NORTH等等

setLayout(new GridLayout(rows,cols))表格式布局,可以分成rows行,cols列


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("");
        }
    }
}

上面就很好的优化了代码,使我们的代码看起来简洁方便。

在我们的学习java中,要侧重于面向对象,而不再是以往的面向过程。

标签:组合,GUI,编程,t2,class,add,new,TextField,public
来源: https://www.cnblogs.com/dazhao-home/p/16088017.html