Java-GUI

java图形界面开发
1、JFrame:顶层窗口容器

import javax.swing.JFrame;
public class Test1 {
    public static void main(String[] args) {
        JFrame jFrame=new JFrame();
        jFrame.setVisible(true);//是否显示该面板
        jFrame.setSize(300, 300);//设置JFrame窗口的大小
        jFrame.setLocation(300, 250);//设置JFrame窗口初始化的位置
        //关闭JFrame窗口的时候,关闭应用程序
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

2、创建一个子类来继承JFrame,然后在构造方法当中,进行一些初始化的操作

import javax.swing.JFrame;
public class Test1 {
    public static void main(String[] args) {
        new MyFrame1();//调用构造方法
    }
}
class MyFrame1 extends JFrame{
    public MyFrame1(){
        this.setSize(300,300);//初始化窗体大小
        this.setVisible(true);//窗体可见
        this.setLocation(300, 250);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

3、JButton:按钮组件,可以放到JFrame去显示

import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
    public static void main(String[] args) {
        new myFrame2();
    }
}
class myFrame2 extends JFrame{
//按钮放到myFrame2的上面,这个就是myFrame2的一部分,可以把该按钮,当成myFrame2的一个属性
    JButton jbutton;//按钮组件
//jbutton作为myFrame2的一个属性,当myFrame2实例化,才创建jbutton对象
    public myFrame2(){
        jbutton=new JButton("确认");//创建组件(按钮)
        this.add(jbutton);//把jbutton添加到myFrame2上面
        //展现窗体myframe2
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

4、设计布局管理器
1. FlowLayout:流式布局(先上后下,先左后右),把组件都显示出来

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
    public static void main(String[] args) {
        new myFrame3();
    }
}
class myFrame3 extends JFrame{
    JButton jbutton1,jbutton2,jbutton3,jbutton4,jbutton5,jbutton6;
    public myFrame3(){
//创建组件
        jbutton1=new JButton("按钮1");
        jbutton2=new JButton("按钮2");
        jbutton3=new JButton("按钮3");
        jbutton4=new JButton("按钮4");
        jbutton5=new JButton("按钮5");
        jbutton6=new JButton("按钮6");
//设置布局(流式布局)
        this.setLayout(new FlowLayout());
//添加组件
        this.add(jbutton1);
        this.add(jbutton2);
        this.add(jbutton3);
        this.add(jbutton4);
        this.add(jbutton5);
        this.add(jbutton6);
//展现窗体
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

2. BorderLayout:边界布局(把整个窗体划分5个部分,eg:很像麻将)

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
    public static void main(String[] args) {
        new myFrame4();
    }
}
class myFrame4 extends JFrame{
    JButton jbutton1,jbutton2,jbutton3,jbutton4,jbutton5;
    public myFrame4(){
//创建组件
        jbutton1=new JButton("东邪");
        jbutton2=new JButton("西毒");
        jbutton3=new JButton("南帝");
        jbutton4=new JButton("北丐");
        jbutton5=new JButton("中神通");
//设置布局(边界布局)
        this.setLayout(new BorderLayout());
//添加组件(必须要指定组件所在的位置)
        this.add(jbutton1,BorderLayout.EAST);
        this.add(jbutton2,BorderLayout.WEST);
        this.add(jbutton3,BorderLayout.SOUTH);
        this.add(jbutton4,BorderLayout.NORTH);
        this.add(jbutton5,BorderLayout.CENTER);
//展现窗体
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("边界布局");//设置一个标题
    }
}

3.    GridLayout:网格布局

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
    public static void main(String[] args) {
        new myFrame5();
    }
}
class myFrame5 extends JFrame{
//利用数组简化,创建对象的过程,更像标准程序员
    JButton[] jbuttons=new JButton[9];
    public myFrame5(){
//设置布局(网格布局)
        this.setLayout(new GridLayout(3,3,5,5));
//创建按钮,添加按钮
        for(int i=0;i<jbuttons.length;i++){
//String.valueOf(数字):    把数字转化为字符串
            jbuttons[i]=new JButton(String.valueOf(i+1));
            this.add(jbuttons[i]);
        }
//展现窗体
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

5、JPanel:介于组件和JFrame之间一个窗体
可以把组件放到JPanel上面,然后把JPanel放到JFrame上面,可以让我们布局美观

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test1 {
    public static void main(String[] args) {
        new MyFrame1();
    }
}
//JFrame要添加2个JPanel,第1个JPanel要添加2个按钮,第2个JPanel要添加6个按钮
class MyFrame1 extends JFrame{
    JButton[] buttons=new JButton[8];
    JPanel jpanel1,jpanel2;
    public MyFrame1(){
//创建JPanel
        jpanel1=new JPanel();//上面,流式
        jpanel2=new JPanel();//下面,网格
//设置布局
        jpanel1.setLayout(new FlowLayout());
        jpanel2.setLayout(new GridLayout(2,3,5,5));
        this.setLayout(new BorderLayout());
//创建按钮
        for(int i=0;i<buttons.length;i++){
            buttons[i]=new JButton(String.valueOf(i+1));
        }
//添加按钮到JPanel
        jpanel1.add(buttons[0]);
        jpanel1.add(buttons[1]);
        jpanel2.add(buttons[2]);
        jpanel2.add(buttons[3]);
        jpanel2.add(buttons[4]);
        jpanel2.add(buttons[5]);
        jpanel2.add(buttons[6]);
        jpanel2.add(buttons[7]);
//jpanel添加到JFrame
        this.add(jpanel1,BorderLayout.NORTH);
        this.add(jpanel2, BorderLayout.CENTER);
//展现窗体
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

6、JTextField:文本框组件
JPasswordField:密码框组件
Jlabel:标题组件

import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Test1 {
    public static void main(String[] args) {
        new Login();
    }
}
class Login extends JFrame{
    JTextField jtf;
    JPasswordField jpd;
    JLabel jl1,jl2;
    JButton button1,button2;
    JPanel jp1,jp2,jp3;
    public Login(){
//创建组件
        jtf=new JTextField(10);
        jpd=new JPasswordField(10);
        jl1=new JLabel("用户名:");
        jl2=new JLabel("密码:");
        button1=new JButton("登录");
        button2=new JButton("注册");
        jp1=new JPanel();
        jp2=new JPanel();
        jp3=new JPanel();
//设置布局
        jp1.setLayout(new FlowLayout());
        jp2.setLayout(new FlowLayout());
        jp3.setLayout(new FlowLayout());
        this.setLayout(new GridLayout(3,1));
//添加组件到JPanel
        jp1.add(jl1);
        jp1.add(jtf);
        jp2.add(jl2);
        jp2.add(jpd);
        jp3.add(button1);
        jp3.add(button2);
//添加JPanel到Login
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
//展现窗体
        this.pack();//自动跳转JFrame大小
        this.setVisible(true);
//        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

事件机制
GUI基于事件:必须实现一个接口ActionListener,包含处理事件的方法
点击一个按钮(事件源),点击这个过程(事件),触发一个方法来处理(事件处理方法)
内部类:在一个类的内部,又嵌套一个类
外边类继承JFrame,里面类实现ActionListener接口

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
    public static void main(String[] args) {
        new SanGuo1();
    }
}
class SanGuo1 extends JFrame{
    JButton liubei;
    public SanGuo1(){
        liubei=new JButton("刘备");//事件源
        this.setLayout(new FlowLayout());
        this.add(liubei);
//监听器:在事件源添加一个监听器,如果事件发生了,立刻触发方法
//addActionListener(实现ActionListener接口类的对象),添加监听器
        liubei.addActionListener(new ZhaoYun());
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
//如果要处理事件,必须要实现一个接口ActionListener
//jdk会根据发生的事件,自动的调用ActionListener里面接口中的方法
//在SabGuo1类内部,设计一个内部类,专门用来实现该接口
    class ZhaoYun implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
//ActionEvent:参数,哪个事件被触发
            System.out.println("哥被揍了。。。赵云快来。。。");
        }
    }
} 

GUI    企业开发的时候,一般模式都是
class 类名 extends JFrame implements ActionListener{
  public void actionPerformed(ActionEvent arg0){
  }
}

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
    public static void main(String[] args) {
        new SanGuo2();
    }
}
class SanGuo2 extends JFrame implements ActionListener{
    JButton liubei;
    public SanGuo2(){
        liubei=new JButton("刘备");
//addActionListener(必须要有一个实现了ActionListener接口实现类的对象)
//给事件源添加监听器
        liubei.addActionListener(this);
        this.setLayout(new FlowLayout());
        this.add(liubei);
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("单击事件");
    }
}
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test1 {
//多个事件源
    public static void main(String[] args) {
        new SanGuo3();
    }
}
class SanGuo3 extends JFrame implements ActionListener{
    JButton button1,button2,button3;
    public SanGuo3(){
        button1=new JButton("曹操");
        button2=new JButton("貂蝉");
        button3=new JButton("吕布");
//每个事件源(按钮),添加监听器
        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);
        this.setLayout(new FlowLayout());
        this.add(button1);
        this.add(button2);
        this.add(button3);
        this.setVisible(true);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent e) {
//ActionEvent:指代当前事件源所触发的事件
//e:哪一个事件源触发事件
//e.getActionCommand():该方法返回的是,触发事件的按钮的名字
        String comm=e.getActionCommand();
//        System.out.println("点击按钮的名字"+comm);
        if("曹操".equals(comm)){
            System.out.println("曹操,修改了孙子兵法");
        }else if("貂蝉".equals(comm)){
            System.out.println("三国美女");
        }else if("吕布".equals(comm)){
            System.out.println("三国猛将");
        }
    }    
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Test1 {
    public static void main(String[] args) {
        new MyFrame5();
    }
}
class MyFrame5 extends JFrame implements ActionListener{
    JTextField jtf1;
    JTextField jtf2;
    JButton button;
    public MyFrame5(){
        jtf1=new JTextField(10);
        jtf2=new JTextField(10);
        button=new JButton("复制");
        button.addActionListener(this);
        this.setLayout(new FlowLayout());
        this.add(jtf1);
        this.add(jtf2);
        this.add(button);
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String comm=e.getActionCommand();
//追加的功能,如果jtf2中没有任何内容,点击复制,直接把jtf1中的内容,复制到jtf2中,反正jtf2中如果存在内容,把jtf1中内容追加到jtf2后面
        if("复制".equals(comm)){
            String str1=jtf1.getText();
            String str2=jtf2.getText();
            if(str2==null){
                jtf2.setText(str1);
            }else{
                jtf2.setText(str2+str1);
            }
        }
//        if("复制".equals(comm)){
//接收到所有的数据,全部是String类型
//            String str=jtf1.getText();//获得文本框中的内容,返回值就是String类型
//            jtf2.setText(str);//把str添加到jtf2作用域
//        }
    }
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test1 {
    public static void main(String[] args) {
        new Computer();
    }
}
class Computer extends JFrame implements ActionListener{
    JTextField jtf;
    JButton[] buttons=new JButton[20];
    JPanel jp;
    String s1;//第1次输入数字
    String opt;//运算符
    public Computer(){
        jtf=new JTextField(20);
        jp=new JPanel();
//设置jp布局:网格五行四列
        jp.setLayout(new GridLayout(5,4));
//设计一个String数组,用来保存20个按钮的名字
        String[] strs={"back","CE","C","+","7","8","9","-",
                       "4","5","6","*","1","2","3","/",
                       "0","+/-",".","="};
//利用for循环,来创建按钮数组:把strs元素,作为按钮的名字
        for(int i=0;i<buttons.length;i++){
            buttons[i]=new JButton(strs[i]);
            jp.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
//设置布局
        this.setLayout(new BorderLayout());
        this.add(jtf,BorderLayout.NORTH);
        this.add(jp,BorderLayout.CENTER);
//展现窗体
        this.setVisible(true);
        this.pack();;
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    @Override
    public void actionPerformed(ActionEvent e) {
//分类:20个按钮:0~9数字按钮,.小数点,+-*/运算符,=结果
//如何判断点击按钮是数字?
//"0123456789".indexOf(点击按钮的名字),如果结果是-1,按钮不是数字,如果不是-1,就是数字,作为判断的方式
        String comm=e.getActionCommand();
        if("0123456789".indexOf(comm)!=-1){
//当前按钮上名字是数字
//获得当前jtf中的内容:如果jtf中有数字,追加,如果jtf中没有任何内容,把comm赋值到jtf中
            String temp=jtf.getText();
            if(temp!=null){
                jtf.setText(temp+comm);
            }else{
                jtf.setText(comm);
            }
        }
//.小数点
        else if(".".equals(comm)){
            String temp=jtf.getText();
            jtf.setText(temp+".");
        }
//+-*/运算符,如果输入的是运算符,表示保存前一个输入的数字,保留当前操作的运算符,只有当我们点击"="的时候,才真正参与运算
        else if("+-*/".indexOf(comm)!=-1){
            s1=jtf.getText();//获得第1个数字
            opt=comm;//保存运算符
            jtf.setText(null);//清空jtf
        }
//=结果:真正进行运算,把结果显示到jtf上
//获得最近一次输入的数字,把第1次输入数字和第2次输入数字,由String转化为int,取得运算符opt,进行计算
        else if("=".equals(comm)){
            String s2=jtf.getText();//最近一次输入的数字
//parseDouble(String):String-->double
            double d1=Double.parseDouble(s1);
            double d2=Double.parseDouble(s2);
            double result=0;
            if("+".equals(opt)){
                result=d1+d2;
            }else if("-".equals(opt)){
                result=d1-d2;
            }else if("*".equals(opt)){
                result=d1*d2;
            }else{
                result=d1/d2;
            }
            String s3=result+"";//数字+"":把任何的一个数字,转为String类型
            jtf.setText(s3);
        }
    }    
}
原文地址:https://www.cnblogs.com/lilyo/p/12792933.html