1. 程式人生 > 其它 >關於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中,要側重於面向物件,而不再是以往的面向過程。