GUI

這篇來寫與GUI相關的內容

1.窗口

public class demo3 {
    public static void main(String[] args) {
        Frame frame = new Frame("昨晚夢到了那個她");

        Button CENTER = new Button("CENTER");
        Button EAST = new Button("EAST");
        Button WEST = new Button("WEST");
        Button NORTH = new Button("NORTH");
        Button SOUTH = new Button("SOUTH");

        frame.add(CENTER,BorderLayout.CENTER);
        frame.add(EAST,BorderLayout.EAST);
        frame.add(WEST,BorderLayout.WEST);
        frame.add(NORTH,BorderLayout.NORTH);
        frame.add(SOUTH,BorderLayout.SOUTH);

        frame.setSize(400,400);
        frame.setLocation(200,200);
        frame.setVisible(true);
        frame.setResizable(false);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

在這裏插入圖片描述

2.彈窗,面板

public class demo1 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setLocation(100,100);
        frame.setSize(400,400);
        //frame.setLayout(new GridLayout(2,2));//設置佈局 網格佈局
        Panel panel = new Panel(new GridLayout(2,2));

        panel.add(new Button("shiyan"),BorderLayout.CENTER);
        panel.add(new Button("shiyan"),BorderLayout.WEST);
        panel.add(new Button("shiyan"),BorderLayout.EAST);
        panel.add(new Button("shiyan"),BorderLayout.NORTH);

        frame.add(panel);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//0正常結束程序  1異常結束程序
            }
        });
    }
}

在這裏插入圖片描述

3.按鈕

public class demo3 {
    public static void main(String[] args) {
        Frame frame = new Frame("昨晚夢到了那個她");

        Button CENTER = new Button("CENTER");
        Button EAST = new Button("EAST");
        Button WEST = new Button("WEST");
        Button NORTH = new Button("NORTH");
        Button SOUTH = new Button("SOUTH");

        frame.add(CENTER,BorderLayout.CENTER);
        frame.add(EAST,BorderLayout.EAST);
        frame.add(WEST,BorderLayout.WEST);
        frame.add(NORTH,BorderLayout.NORTH);
        frame.add(SOUTH,BorderLayout.SOUTH);

        frame.setSize(400,400);
        frame.setLocation(200,200);
        frame.setVisible(true);
        frame.setResizable(false);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }
}

在這裏插入圖片描述

4.監聽器

按一次按鈕運行一次,實現監聽


public class demo {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button();
        MyActionListener actionListener = new MyActionListener();
        button.addActionListener(actionListener);

        frame.setBounds(400,400,400,400);
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
        windowclose(frame);
    }
    public static void windowclose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}

5.簡易計算器(加法)

public class demo3 {
    public static void main(String[] args) {

        new calcular().loadframe();

    }
}
class calcular extends Frame{

    TextField num1,num2,num3;

    public void loadframe(){
        num1=new TextField(10);
        num2=new TextField(10);
        num3=new TextField(10);

        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new mycalculatorlisener());

        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);


    }

    private class mycalculatorlisener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            int i = Integer.parseInt(num1.getText());
            int i1 = Integer.parseInt(num2.getText());
            num3.setText(""+(i+i1));
            num1.setText("");
            num2.setText("");
        }
    }
}

在這裏插入圖片描述

6.鍵盤事件


public class demo {
    public static void main(String[] args) {
        new key();
    }
}
class key extends Frame{
    public key(){
        setBounds(1,2,300,400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                System.out.println(keyCode);
                if(keyCode==KeyEvent.VK_UP){
                    System.out.println("你按了上鍵");
                }
            }
        });
    }
}

在這裏插入圖片描述

7.畫圖

public class demo2 {
    public static void main(String[] args) {

        new huahua("畫圖");
    }
}
class huahua extends Frame{
    ArrayList points;
    public huahua(String title){
        super(title);
        setBounds(200,200,300,400);

        points=new ArrayList();
        setVisible(true);

        this.addMouseListener(new MyMouselistener());


    }
    public void paint(Graphics g){//繪圖
        Iterator iterator = points.iterator();//迭代器
        while(iterator.hasNext()){
           Point point = (Point) iterator.next();
           g.setColor(Color.BLUE);
           g.fillOval(point.x,point.y,10,10);

        }
    }
    /*@Override
    public void paint(Graphics g) {
        //畫畫,監聽鼠標的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x,point.y,10,10);
        }
    }*/

    public void addPaint(Point point){
        points.add(point);
    }
    private class MyMouselistener extends MouseAdapter{

        @Override
        public void mousePressed(MouseEvent e) {
            huahua huahua = (huahua) e.getSource();
            huahua.addPaint(new Point(e.getX(),e.getY()));


            huahua.repaint();
        }
    }
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章