【JAVA------GUI基础知识】

发布时间:2024-01-17 23:19:46

GUI基础知识

注意
//这行代码需要放在第一行,不然会导致设置的文字或者其它的东西,不会根据你输入的数据来定位
this.setLayout(null);
更改背景图片
		//获取图片路径
        URL res = zhuceView.class.getResource("register.jpg");
        ImageIcon icon = new ImageIcon(res);
        //用JLable来装图片
        JLabel jLabel = new JLabel();
        jLabel.setBounds(0, 0, 500, 500);
        jLabel.setIcon(icon);
        this.add(jLabel);
修改图标
		//获取图片路径
		URL url = zhuceView.class.getResource("student.png");
        ImageIcon icon1 = new ImageIcon(url);
        Image image = icon1.getImage();
        //将图片设置成图标
        this.setIconImage(image);
修改图片大小
        URL res1 = zhuceView.class.getResource("time.png");
        ImageIcon icon1 = new ImageIcon(res1);
        icon1.setImage(icon1.getImage().getScaledInstance(40, 40, Image.SCALE_DEFAULT));
文字
		//文字
        JLabel jLabel1 = new JLabel("学生注册信息");
        //设置字体和大小
        Font font1 = new Font("微软雅黑", Font.BOLD,30);
        //文字颜色
        jLabel1.setForeground(Color.blue);
        //文字位置和大小
        jLabel1.setBounds(150, 80, 300, 50);
        //将设置的文字大小和字体设置到当中
        jLabel1.setFont(font1);
        将文字添加到GUIthis.add(jLabel1);
按钮
		//new一个按钮
        JButton jButton1 = new JButton("添加");
        //设置按钮字体
        jButton1.setFont(font3);
        //设置按钮颜色
        jButton1.setForeground(new Color(18, 50, 255));
        //设置按钮大小和位置
        jButton1.setBounds(190, 430, 50, 30);
        //需要设置这个不然可能会显示不出字,而是显示...
        jButton1.setBorder(null);
        this.add(jButton1);
        //设置窗口的关闭按钮
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                //给出友好提示
                int optin = JOptionPane.showConfirmDialog(null,"您确定要退出系统吗?","温馨提示",JOptionPane.YES_NO_OPTION );
                //判断用户的选择
                if(optin == JOptionPane.YES_OPTION){
                    //退出程序
                    System.exit(0);
                }else if(optin == JOptionPane.NO_OPTION){
                    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
                }
            }
        });

        //禁止放大
        this.setResizable(false);
输入框
        //给注册按钮绑定监听事件
        myButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //将登录页面隐藏,显示注册页面
                setVisible(false);
                //显示注册页面
                new zhuceView();
            }
        });
		//文本输入框
		JTextField jTextField1 = new JTextField();
        jTextField1.setBounds(170, 170, 200, 30);
        jTextField1.setFont(font2);
        this.add(jTextField1);
		//密码输入框
		JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setBounds(170, 220, 200, 30);
        this.add(jPasswordField);
技巧:当JLable需要用到多次时,可以通过自己定义一个类MyJLable来继承JLable,来生成构造器,最后通过new一个MyJLable来输入MyJLable所定义的参数。

例如:

public class MyText extends JTextField {
    private int x;
    private int y;
    private int width;
    private int height;
    private String fontName;
    private int fontSize;

    public MyText(int x, int y, int width, int height, String fontName, int fontSize) {
        //设置输入框的位置和宽高
        this.setBounds(x, y, width, height);
        //设置输入框的字体
        Font font = new Font(fontName,Font.BOLD, fontSize);
        this.setFont(font);
    }
}
 MyText myText = new MyText(0, 0, 100, 10, "微软雅黑", 20);
文章来源:https://blog.csdn.net/Thrive_LCX/article/details/135592827
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。