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);
        }
    }
  • 相关阅读:
    SpringBoot整合dubbo2.7.12
    linux安装zookeeper
    javaassist创建对象
    jmeter websocket
    jmeter使用
    jmeter返回值乱码
    HTTP 头 Connection:close 作用 和 解决服务器产生大量close_wait问题
    服务器TCP连接中 TIME_WAIT 状态过多
    Chrome 浏览器远程调试 【转】
    拼多多聊天记录监控、拼多多客服机器人代码、拼多多智能机器人代码、拼多多自动发货、拼多多虚拟卡号自动发货
  • 原文地址:https://www.cnblogs.com/lea-fu/p/3253279.html
Copyright © 2011-2022 走看看