java GUI编程

java GUI编程,称为面向图形化编程,核心的技术是 AWT 和Swing ,主要就是写一些界面,不过这不是java的强项。

组件:窗口,弹窗,面板,按钮,文本框,文本域,图标,图片图标,标签等
可以进行事件监听,如,键盘监听,鼠标监听等

实现组件与事件监听都是各种类与接口,代码中都有标注,个人的学习笔记,有用的拿走。

package GUI;

import java.awt.*;

//gui的第一个界面 frame类
public class Demo{
    public Demo(String myFrame, int i) {
    }

    public static void main(String[] args){
        //Frame  类
        Frame frame = new Frame("我的第一个Java图形窗口");//在内存中,需要设置可见性
        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(1,1,1));
        //设置初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }
}

package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import static java.awt.BorderLayout.EAST;

//BorderLayout 东西南北中布局
public class Demo2 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setBounds(200,200,400,400);
        frame.setVisible(true);
        frame.setBackground(new Color(12, 23, 231));

        //为窗口增加一个监听事件
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//正常关闭

            }
        });
        Button button1 = new Button("center");
        Button button2 = new Button("East");
        Button button3 = new Button("south");
        Button button4 = new Button("north");

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.setLayout(new BorderLayout());


    }
}

package GUI;

import javax.xml.soap.Text;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;

public class Demo4 {
    public static void main(String[] args) {
        //启动!
        new MyFrame4();
    }
}
class MyFrame4 extends Frame{
    public MyFrame4(){//类含有的构造器
        TextField t1 = new TextField(); //文本框类
        add(t1);//文本框加入窗口
        //监听文本框输入的文字
        MyActionListener1 m2 = new MyActionListener1();
        t1.addActionListener(m2); //按下enter 键就会触发这个输入框的事件
        setResizable(true); //设置打小是否可变
        setVisible(true);
        setBackground(Color.MAGENTA);
        pack();

        //替换编码
        t1.setEchoChar('-');

    }
}
class MyActionListener1 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
       TextField file = (TextField)e.getSource();//获得一些资源 返回一个对象 将这个对象转为文本类型并输出
        System.out.println(file.getText());//获得输入框中的文本
        file.setText("");

    }
}
package GUI;

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

public class Demo5 {
    public static void main(String[] args) {
        new Calculator();

    }
}
//计算器
class Calculator extends Frame{
    public Calculator(){  //构造器
        //三个文本框
        TextField num1 = new TextField(10);//10 字符数
        TextField num2 = new TextField(10);//10 字符数
        TextField num3 = new TextField(20);//10 字符数

        // 一个标签
        Label label = new Label("+");


        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyActionListener2(num1,num2,num3));
        //实例化一个对象时,有无参构造器默认使用无参构造器,不用传参,当只有有参构造器时,实例化对象必须传参

        //布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        //窗口基本属性
        setVisible(true);
        setBackground(Color.magenta);
        setBounds(200,200,500,200);
        //添加一个关闭监听事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//监听器类
class MyActionListener2 implements ActionListener {  //ActionListener 监听的接口
    //获取三个变量
    private TextField num1,num2,num3;

    public MyActionListener2(TextField num1,TextField num2,TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //获得加数与被加数
       int a = Integer.parseInt(num1.getText());
       int b = Integer.parseInt(num2.getText());

       //将两个值相加后赋给第三个文本框
        num3.setText(""+(a+b));
        num2.setText("");
        num1.setText("");


    }
}

package GUI;

import java.awt.*;

public class Demo7 {
    public static void main(String[] args) {
        new Mypaint();

    }
}
class Mypaint extends Frame{
    public void LoadFraem(){
        setBounds(100,200,400,400);
        setVisible(true);
    }

    //画笔
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //画笔设置颜色,大小
        g.setColor(Color.red);
        g.fillOval(100,100,100,100);//实心的圆



    }
}
package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Dmeo3 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setVisible(true);
        frame.setBounds(100,200,400,400);

        frame.setLayout(new GridLayout(2,1));//表格布局  两行一列
        //四个面板
        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2,1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2,2));

        //大面板中加入左右两个按钮
        panel1.add(new Button("East-1"),BorderLayout.EAST);
        panel1.add(new Button("west-2"),BorderLayout.WEST);
        //小面板中加入两个按钮
        panel2.add(new Button("BUTTON1"));
        panel2.add(new Button("BUTTON2"));
        //将小的面板panel2 放入大面板Panel1的中心
        panel1.add(panel2,BorderLayout.CENTER);
        //下面的布局 大面板中加入两个按钮
        panel3.add(new Button("East-3"),BorderLayout.EAST);
        panel3.add(new Button("west-4"),BorderLayout.WEST);
        //小面板中加入四个按钮
        for (int i = 1; i <= 4; i++) {
            panel4.add(new Button("button"+i));
        }
        //小面板加入大面板
        panel3.add(panel4,BorderLayout.CENTER);
        //两个大面板加入窗口中
        frame.add(panel1);
        frame.add(panel3);

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

package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class PanelTest {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //布局的概念
        Panel panel = new Panel();//面板嵌入窗口
        //设置布局
        frame.setLayout(null);
        //窗口坐标大小背景
        frame.setBounds(400,400,500,500);
        frame.setBackground(new Color(245, 38, 38));
        //面板坐标大小背景
        panel.setBounds(100,100,200,200);
        panel.setBackground(new Color(109, 217, 82));


        //Panel 面板加入窗口
        frame.add(panel);
        //设置窗口可见性
        frame.setVisible(true);
        //监听事件,监听窗口关闭时间,System.exit(frame)
        frame.addWindowListener(new WindowAdapter() { //适配器模式  为窗口添加监听事件

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0); //0 代表正常退出
            }
        });





    }
}

package GUI;
//包含了更多的类 Swing
import javax.swing.*;
import java.awt.*;

public class Swing {
    // init() 初始化的方法
    public void init(){//初始化函数
        JFrame s1 = new JFrame("这是一个jframe 窗口");
        s1.setVisible(true);
        s1.setBounds(100,100,100,100);
        //设置一个文字
        new Label();

        //关闭事件
        s1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {

        new Swing().init();//调用
    }

}

package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test1 {
    public static void main(String[] args) {
        Frame f1 = new Frame();

        //组件-按钮
        Button b1 = new Button("button1");
        Button b2 = new Button("button2");
        Button b3 = new Button("button3");
        //设置为流式布局
        f1.setLayout(new FlowLayout(FlowLayout.RIGHT));//布局可设置位置 中心位置
        f1.setSize(200,200);
        //将按钮加入窗口、
        f1.add(b1);
        f1.add(b2);
        f1.add(b3);
        //设置可见性
        f1.setVisible(true);
        //事件监听
        f1.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//正常关闭
            }
        });
    }
}

package GUI;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test1 {
    public static void main(String[] args) {
        Frame f1 = new Frame();

        //组件-按钮
        Button b1 = new Button("button1");
        Button b2 = new Button("button2");
        Button b3 = new Button("button3");
        //设置为流式布局
        f1.setLayout(new FlowLayout(FlowLayout.RIGHT));//布局可设置位置 中心位置
        f1.setSize(200,200);
        //将按钮加入窗口、
        f1.add(b1);
        f1.add(b2);
        f1.add(b3);
        //设置可见性
        f1.setVisible(true);
        //事件监听
        f1.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);//正常关闭
            }
        });
    }
}

package GUI;

import java.awt.*;
import java.awt.event.*;

public class TestAction {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button();
        //因为监听addActionListener() 需要一个ActionLitener, 所以构造一个ActionListener
        MyActionListener M1 = new MyActionListener();
        button.addActionListener(M1);//添加一个按钮的监听事件 当监听到什么之后反应呢


        frame.add(button,BorderLayout.CENTER);
        frame.setBounds(100,100,400,400);
        frame.setVisible(true);
        frame.setBackground(Color.MAGENTA);
        frame.pack();//窗口自适应
    }
}
//写一个监听事件
class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("hga");//当监听到点击按钮时,输入
        System.exit(0);

    }
    public void put(){
        System.out.println("bushigouzaoqi");
    }

}



package GUI;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

//鼠标监听
public class TestMouse {
    public static void main(String[] args) {
        new MouseTest("画图");

    }
}
class MouseTest extends Frame{


    @Override
    public void paint(Graphics g) {
        super.paint(g);
    }
    //画画需要画笔
    public MouseTest(String title){
        super(title);
        setBounds(100,100,200,200);
        this.addMouseListener(new MyMouselistener());//监听鼠标




    }

}
//适配器模式
class MyMouselistener extends MouseAdapter {//实现接口


    @Override
    public void mouseClicked(MouseEvent e) {
       //点击的时候,画下一个点
    }
}

package GUI;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

//键盘的监听事件
public class Windows {
    public static void main(String[] args) {
        new TestKeyListener();
    }

}
class TestKeyListener extends Frame{
    //构造器
    public TestKeyListener(){
        setBounds(100,100,400,400);
        setVisible(true);
        setBackground(new Color(182, 29, 187));

        //加一个键盘监听事件
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                char k1 = e.getKeyChar(); //监听键盘输入的字符并打印
                System.out.println(k1);
            }
        });
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javaGUI图形界面 public class login extends JFrame { private JComboBox nameJComboBox; private JPanel userJPanel; private JLabel pictureJLabel; private JButton okJButton,cancelJButton; private JLabel nameJLabel,passwordJLabel,note; private JPasswordField passwordJPasswordField; private String name1; private String password1; private String user; private ImageIcon myImageIcon; public login( ) { createUserInterface(); // 调用创建用户界面方法 } private void createUserInterface() { Container contentPane = getContentPane(); contentPane.setLayout( null ); userJPanel = new JPanel(); userJPanel.setBounds( 35, 120, 300, 96 ); userJPanel.setBorder(BorderFactory.createEtchedBorder() ); //显示一圈边儿 userJPanel.setLayout( null ); contentPane.add( userJPanel ); nameJComboBox = new JComboBox(); nameJComboBox.setBounds( 100, 12, 170, 25 ); nameJComboBox.addItem( "admin" ); nameJComboBox.addItem( "aloie" ); nameJComboBox.setSelectedIndex( 0 ); nameJComboBox.setEditable(true); userJPanel.add( nameJComboBox ); pictureJLabel=new JLabel(); pictureJLabel.setBounds(45,0,380,118); pictureJLabel.setIcon(new ImageIcon("pic.gif")); contentPane.add(pictureJLabel); nameJLabel=new JLabel("姓 名:"); nameJLabel.setBounds(20,12,80,25); userJPanel.add(nameJLabel); passwordJPasswordField=new JPasswordField(); passwordJPasswordField.setBounds(100,60,170,25); userJPanel.add(passwordJPasswordField); passwordJLabel=new JLabel("密 码:"); passwordJLabel.setBounds(20,60,80,25); userJPanel.add(passwordJLabel); note=new JLabel("密码与用户名相同"); note.setBounds(0,295,180,25); add(note); okJButton=new JButton("登 陆"); okJButton.setBounds(60,250,80,25); contentPane.add(okJButton); okJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { okJButtonActionPerformed(event); } } ); cancelJButton=new JButton("取 消"); cancelJButton.setBounds(210,250,80,25); contentPane.add(cancelJButton); cancelJButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { System.exit(0); //退出登陆 } } ); setTitle( "登陆窗口" ); setSize( 380, 350 ); setResizable( false ); //将最大化按钮设置为不可用 } private void okJButtonActionPerformed( ActionEvent event ) { //okJButton响应事件,检查用户名和密码的匹配 name1= nameJComboBox.getSelectedItem().toString(); if (name1.equals("admin") ) { if (passwordJPasswordField.getText().equals("admin")) { showNewWindow(); setVisible( false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } else if (name1.equals("aloie")) { if ( passwordJPasswordField.getText().equals("aloie") ) { showNewWindow(); setVisible(false); } else { JOptionPane.showMessageDialog( this,"密码错误,拒绝登陆", "密码错误 !", JOptionPane.ERROR_MESSAGE ); } } } public void showNewWindow() { JFrame jf=new JFrame("main Frame"); jf.setSize(500,400); jf.setVisible(true); jf.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } public static void main( String[] args ) { JFrame.setDefaultLookAndFeelDecorated(true); login mylogin = new login( ); mylogin.setVisible( true ); mylogin.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值