栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java

GUI编程 java.awt

窗口frame
public class frame02 {
    public void window(){
        frame frame =new frame("java窗口");

        //设置可见性
        frame.setVisible(true);

        //设置窗口大小
        frame.setSize(400,400);

        //设置背景颜色
        frame.setBackground(Color.YELLOW);

        //弹出初始位置
        frame.setLocation(1000,200);

        //设置大小固定
        frame.setResizable(false);
    }
}
面板panel

可以看成一个空间,但是不能单独存在,需要放在frame上

布局管理器
    流式布局
public class frameStrame {
    public static void main(String[] args) {
        frame frame =new frame();
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));//设置布局为流式布局,居中
        frame.setSize(200,200);
        frame.setVisible(true);//设置可见性

        Button button1 =new Button("按钮1");
        Button button2 =new Button("按钮2");
        Button button3 =new Button("按钮3");


        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
    }
}
    盒式布局
public class frameBorder {
    public static void main(String[] args) {
        frame frame = new frame();

        Button left=new Button("left");
        Button right=new Button("right");
        Button top=new Button("top");
        Button center=new Button("center");
        Button under=new Button("under");


        frame.add(left,BorderLayout.EAST);
        frame.add(right,BorderLayout.WEST);
        frame.add(under,BorderLayout.SOUTH);
        frame.add(top,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);

        frame.setVisible(true);
        frame.setSize(200,200);

    }
}
    表格式布局
public class frameGrid {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setVisible(true);
        frame.setSize(200,200);

        Button button1=new Button("north");
        Button button2=new Button("south");
        Button button3=new Button("center");
        Button button4=new Button("east");
        Button button5=new Button("west");

        frame.setLayout(new GridLayout(3,2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
    }
}
    窗口嵌套面板
public class frameLayoutTest {
    public static void main(String[] args) {
        frame frame = new frame();
        frame.setVisible(true);
        frame.setSize(500, 500);
        frame.setLayout(new GridLayout(2,1));


        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));

        //上半部分p1
        p1.add(new Button("east1"),BorderLayout.EAST);
        p1.add(new Button("west1"),BorderLayout.WEST);
        p1.add(p2,BorderLayout.CENTER);
        p2.add(new Button("1"));
        p2.add(new Button("2"));
        //下半部分p3
        p3.add(new Button("east2"),BorderLayout.EAST);
        p3.add(new Button("west2"),BorderLayout.WEST);
        p3.add(p4,BorderLayout.CENTER);
        p4.add(new Button("3"));
        p4.add(new Button("4"));
        p4.add(new Button("5"));
        p4.add(new Button("6"));

        frame.add(p1);
        frame.add(p3);

    }
}

事件监听

关闭窗口和建立一个按钮交互

public class ActionListener {
    public static void main(String[] args) {
        frame frame = new frame();
        Button button =new Button();
        Action1 one = new Action1();
        button.addActionListener(one);
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.add(button);
        WindowClose(frame);
    }
    private static void WindowClose(frame fr){
        fr.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

class Action1 implements java.awt.event.ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("我被点了");
    }
}

多个按钮共享一个事件

public class ActionLister1 {
    public static void main(String[] args) {
        frame  frame = new frame();
        Button button1=new Button("num1");
        Button button2=new Button("num2");
        Action2 action2 = new Action2();


        button2.addActionListener(action2);
        button1.addActionListener(action2);
        button1.setActionCommand("有点小爽");//显式地定义触发后返回的值

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.setVisible(true);
        frame.setSize(500,500);
    }
}
class Action2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //e.() 获得按钮上的信息
        System.out.println("我被点了"+e.getActionCommand());
    }
}
模拟密码框中的窗体行为
package Test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Text {
    public static void main(String[] args) {
    new Fr();
    }
}

class Fr extends frame{//继承frame后不用再创建对象可以直接调用方法布局窗口
    public Fr(){
        TextField textField=new TextField();//新建文本框对象
        add(textField);     //窗体中加入文本框
        Action3 action3 =new Action3();
        textField.addActionListener(action3);//将Action3类中写的事件方法引入文本框中

        setVisible(true);
        setSize(500,500);//这行代码没有生效?
        pack();
    }
}
class Action3 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field =(TextField)e.getSource();//getsource方法返回的是一个Object对象,可以下转型为任意类型的对象
        System.out.println(field.getText());
    }
}
简易计算器实现(只实现加法)
package Test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Calculator {
    public static void main(String[] args) {
    new Cal();
    }
}
class Cal extends frame{
    public Cal(){
        TextField f1 = new TextField(10);
        TextField f2 = new TextField(10);
        TextField f3 = new TextField(20);
        Button button = new Button("=");
        Label l1 = new Label("+");

        button.addActionListener(new Action4(f1,f2,f3));
        setLayout(new FlowLayout());


        add(f1);
        add(l1);
        add(f2);
        add(button);
        add(f3);

        setVisible(true);
        pack();
    }
}

class Action4 implements ActionListener{
    private TextField f1,f2,f3;//传进来的是三个TextField类型的对象,可以控制文本框,对文本框的内容进行操作
    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(f1.getText()); //getText()后得到的是String类型的变量
        int n2 = Integer.parseInt(f2.getText());
        int n3 = Integer.parseInt(f3.getText());

        f3.setText(""+(n1+n2));

        f1.setText("");
        f2.setText("");
    }

    public Action4(TextField f1,TextField f2,TextField f3) {
        this.f1=f1;
        this.f2=f2;
        this.f3=f3;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/756049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号