zoukankan      html  css  js  c++  java
  • java学习--GUI1

    JFrame

    JFrame类的常用操作方法

    import java.awt.Color;
    import javax.swing.JFrame;
    
    public class JFrameDemo1 {
        public static void main(String[] args) {
            JFrame f = new JFrame("第一个Swing窗体");
            f.setSize(200, 100);
            f.setBackground(Color.WHITE);
            f.setLocation(300, 200);
            f.setVisible(true);
        }
    }

    在JFrame类中使用Dimension类设置窗体的大小,使用Point类显示组件的位置

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Point;
    import javax.swing.JFrame;
    
    public class JFrameDemo2 {
        public static void main(String[] args) {
            JFrame f = new JFrame("第一个Swing窗体");
            Dimension d = new Dimension();
    
            d.setSize(200, 100);
            f.setSize(d);
            f.setBackground(Color.WHITE);
            Point p = new Point(300,200);
            f.setLocation(p);
            f.setVisible(true);
        }
    }

    JLabel

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Point;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class JLabelDemo1 {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Welcome to leaf's blog");
            JLabel lab = new JLabel("leaf's blog",JLabel.CENTER);
            frame.add(lab);
            Dimension dim = new Dimension();
            dim.setSize(200, 100);
            frame.setSize(dim);
            frame.setBackground(Color.WHITE);
            Point p = new Point(300,200);
            frame.setLocation(p);
            frame.setVisible(true);
        }
    }

    更改JLabel的文字样式

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Point;
    import java.awt.Font;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class JLabelDemo2 {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Welcome to leaf's blog");
            JLabel lab = new JLabel("leaf's blog",JLabel.CENTER);
            Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
            lab.setFont(fnt);
            lab.setForeground(Color.RED);
            frame.add(lab);
            Dimension dim = new Dimension();
            dim.setSize(200, 100);
            frame.setSize(dim);
            frame.setBackground(Color.WHITE);
            Point p = new Point(300,200);
            frame.setLocation(p);
            frame.setVisible(true);
        }
    }

    在JLabel中设置图片

    方法一: 从图像取得字节数组,使用InputStream类完成操作

    import java.awt.*;
    import java.awt.Point;
    import java.io.*;
    import javax.swing.*;
    
    public class JLabelDemo3 {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Welcome to leaf's blog");
            String picPath = "E:" + File.separator + "1.jpg";
            File file = new File(picPath);
            InputStream input = null;
            byte[] b = new byte[(int)file.length()];
    
            try {
                input = new FileInputStream(file);
                input.read(b);
                input.close();
            } catch (Exception e) {
                e.getStackTrace();
            }
    
            Icon icon = new ImageIcon(b);
            JLabel lab = new JLabel("leaf's blog", icon, JLabel.CENTER);
            Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
            lab.setFont(fnt);
            lab.setForeground(Color.RED);
            lab.setBackground(Color.YELLOW);
            frame.add(lab);
            Dimension dim = new Dimension();
            dim.setSize(300, 160);
            frame.setSize(dim);
            frame.setBackground(Color.WHITE);
            Point p = new Point(300,200);
            frame.setLocation(p);
            frame.setVisible(true);
    
        }    
    }

    方法二:直接将图片的路径传递到ImageIcon的实例化中,并使用JLabel类中的ImageIcon的构造方法

    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    
    public class JLabelDemo4 {
        public static void main(String[] args) {
            JFrame frame = new JFrame("Welcome to leaf's blog");
            String picPath = "E:" + File.separator + "1.jpg";
            Icon icon = new ImageIcon(picPath);
            JLabel lab = new JLabel("leaf's blog", icon, JLabel.CENTER);
            Font fnt = new Font("Serief", Font.ITALIC + Font.BOLD, 28);
            lab.setFont(fnt);
            lab.setForeground(Color.RED);
            lab.setBackground(Color.YELLOW);
            frame.add(lab);
            frame.setSize(300,160);
            frame.setBackground(Color.WHITE);
            frame.setLocation(300,200);
            frame.setVisible(true);
        }
    }

    如果此时图像是从一个不确定输入流中而来,则要使用第一中方法;如果图像是从文件中而来,则使用第二中方法方便。

    按钮组件JButton

    import java.awt.*;
    import javax.swing.*;
    
    public class JButtonDemo1 {
        public static void main(String[] args) {
            JFrame frame = new JFrame("leaf");
            JButton jb = new JButton("Hello");
            Font fnt = new Font("Serief", Font.BOLD,28);
            jb.setFont(fnt);
            frame.add(jb);
            frame.setSize(300, 160);
            frame.setLocation(300, 200);
            frame.setVisible(true);
        }
    }
  • 相关阅读:
    vim 的列操作
    关于 matplotlib
    freemarker的常用内建函数
    三次握手与四次挥手
    layui动态表格生成
    layui 解决浏览器自动填充form表单账号和密码输入框的问题
    手机验证 和身份证验证
    把动态查询出来的集合数据,横向展示在页面
    eclipse安装freemarker插件
    目录文件树jQuery Ztree基本用法
  • 原文地址:https://www.cnblogs.com/lea-fu/p/3253279.html
Copyright © 2011-2022 走看看