• Java的Swing基本控件


    一、JLabel

    案例运行结果

     MyFrame.java

    package swingkongjian;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class MyFrame extends JFrame {
    
        public MyFrame(String title){
            super(title);
            //内容面板
            Container contentPane=getContentPane();
            contentPane.setLayout(new FlowLayout());
    
            //向内容面板添加控件
            JLabel label= new JLabel();
            contentPane.add(label);
    
            label.setText("百度官网");
            label.setFont(new Font("微软雅黑",0,14));
            label.setForeground(Color.BLUE);
            label.setToolTipText("https://www.baidu.com");
        }
    
    }

    SwingDemo.java

    package swingkongjian;
    
    import javax.swing.*;
    
    public class SwingDemo {
        private static void createGUI(){
    
            //JFrame指一个窗口,构造方法的参数为窗口标题
            MyFrame frame=new MyFrame("swing demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //设置窗口的其它参数,如窗口大小
            frame.setSize(400,300);
    
            //显示窗口
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createGUI();
                }
            });
        }
    }

    二、JButton与点击事件的实现

    案例运行结果

     MyFrame2.java

    package swingkongjian;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MyFrame2 extends JFrame {
        JLabel label= new JLabel("姓名");
        //16表示16列,计算宽度显示,而不是输入字符的个数限制
        JTextField jTextField=new JTextField(16);
        JButton button=new JButton("确定");
        public MyFrame2(String title){
            super(title);
            //内容面板
            Container contentPane=getContentPane();
            contentPane.setLayout(new FlowLayout());
    
            //向内容面板添加控件
            contentPane.add(label);
            contentPane.add(jTextField);
            contentPane.add(button);
    
            //添加点击事件
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    onButtonOK();
                }
            });
        }
    
        //事件处理
        private void onButtonOK() {
            String str=jTextField.getText();
            JOptionPane.showMessageDialog(this,"输入了"+str);
        }
    }

    SwingDemo02.java

    package swingkongjian;
    
    import javax.swing.*;
    
    public class SwingDemo02 {
        private static void createGUI(){
    
            //JFrame指一个窗口,构造方法的参数为窗口标题
            MyFrame2 frame=new MyFrame2("swing demo02");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //设置窗口的其它参数,如窗口大小
            frame.setSize(600,400);
    
            //显示窗口
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createGUI();
                }
            });
        }
    }

    三、JCheckBox

    案例运行结果

     

     MyFrame3.java

    package swingkongjian;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MyFrame3 extends JFrame {
        JCheckBox cbx=new JCheckBox("我想订阅邮件通知");
        JTextField email=new JTextField(16);
        public MyFrame3(String title){
            super(title);
            //内容面板
            Container contentPane =getContentPane();
            contentPane.setLayout(new FlowLayout());
            //添加控件
            contentPane.add(cbx);
            contentPane.add(email);
            //默认选中
            cbx.setSelected(true);
            email.setToolTipText("请输入邮箱地址");
    
            cbx.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //如果未选中,则禁用文本框
                    if (cbx.isSelected()){
                        email.setEnabled(true);
                    }else {
                        email.setEnabled(false);
                    }
                }
            });
        }
    }

    SwingDemo03.java

    package swingkongjian;
    
    import javax.swing.*;
    
    public class SwingDemo03 {
        private static void createGUI(){
            MyFrame3 frame=new MyFrame3("swing demo03");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(600,400);
    
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createGUI();
                }
            });
        }
    }

    四、JComboBox

    案例运行结果

     

     MyFrame4.java

    package swingkongjian;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MyFrame4 extends JFrame {
        //JComboBox是一个泛型,参数类型是数据项的类型
        JComboBox<String> colorList =new JComboBox<>();
        JLabel sampleText=new JLabel("文本样式,this is a simple");
        public MyFrame4(String title){
            super(title);
            //内容面板
            Container contentPane=getContentPane();
            contentPane.setLayout(new FlowLayout());
            //添加控件
            contentPane.add(colorList);
            contentPane.add(sampleText);
    
            //向JComboBox中添加数据
            colorList.addItem("红色");
            colorList.addItem("蓝色");
            colorList.addItem("绿色");
            
            UpdateTextColor();
    
            //下拉列表的事件处理
            colorList.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    UpdateTextColor();
                }
            });
        }
    
        private void UpdateTextColor() {
            //更新JLabel的颜色显示
    
            //获取选中的颜色
            String item=(String) colorList.getSelectedItem();
            //根据选中的颜色,设置JLabel的文字颜色
            Color color=null;
            if (item.equals("红色")){
                color=Color.RED;
            }else if (item.equals("蓝色")){
                color=Color.BLUE;
            }else if (item.equals("绿色")){
                color=Color.GREEN;
            }
            sampleText.setForeground(color);
        }
    }

    对其进行优化

    MyFrame5.java

    package swingkongjian;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class MyFrame5  extends JFrame {
        JComboBox<ListOption> colorList=new JComboBox<>();
    
        JLabel sampleText=new JLabel("文本样式,this is a simple");
        public MyFrame5(String title){
            super(title);
            //内容面板
            Container contentPane=getContentPane();
            contentPane.setLayout(new FlowLayout());
            //添加控件
            contentPane.add(colorList);
            contentPane.add(sampleText);
    
            //向JComboBox中添加数据
            colorList.addItem(new ListOption("红色",Color.RED));
            colorList.addItem(new ListOption("蓝色",Color.BLUE));
            colorList.addItem(new ListOption("绿色",Color.GREEN));
    
            UpdateTextColor();
    
            //下拉列表的事件处理
            colorList.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    UpdateTextColor();
                }
            });
        }
    
        private void UpdateTextColor() {
            //更新JLabel的颜色显示
    
            //获取选中的颜色
            ListOption item=(ListOption) colorList.getSelectedItem();
            //根据选中的颜色,设置JLabel的文字颜色
            sampleText.setForeground(item.color);
        }
        private static class ListOption{
            /**
             * 这个类是用来表示列表项
             */
            public String text;
            public Color color;
            public ListOption(String text,Color color){
                this.text=text;
                this.color=color;
            }
    
            @Override
            public String toString() {
                return this.text;
            }
        }
    }

    SwingDemo04.java

    package swingkongjian;
    
    import javax.swing.*;
    
    public class SwingDemo04 {
        private static void CreateGUI(){
            MyFrame4 frame=new MyFrame4("SwingDemo04");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(600,400);
    
            frame.setVisible(true);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    CreateGUI();
                }
            });
        }
    }
    View Code

    SwingDemo05.java

    package swingkongjian;
    
    import javax.swing.*;
    
    public class SwingDemo05 {
        private static void CreateGUI(){
            MyFrame5 frame=new MyFrame5("SwingDemo05");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(600,400);
    
            frame.setVisible(true);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    CreateGUI();
                }
            });
        }
    }
    SwingDemo05.java

    五、彩色标签(案例)

    运行结果

     MyFrame6.java

    package swingkongjian;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class MyFrame6 extends JFrame {
    
        public MyFrame6(String title){
            super(title);
            Container contentPane=getContentPane();
            contentPane.setLayout(new FlowLayout());
    
            JLabel a1=new JLabel("1");
            //设置背景为不透明(默认透明)
            a1.setOpaque(true);
            //设置背景色
            a1.setBackground(Color.YELLOW);
            //设置最佳尺寸
            a1.setPreferredSize(new Dimension(60,30));
            //设置水平对齐
            a1.setHorizontalAlignment(SwingConstants.CENTER);
    
            JLabel a2=new JLabel("2");
            a2.setOpaque(true);
            a2.setBackground(Color.GREEN);
            a2.setPreferredSize(new Dimension(60,30));
            a2.setHorizontalAlignment(SwingConstants.CENTER);
    
            JLabel a3=new JLabel("3");
            a3.setOpaque(true);
            a3.setBackground(Color.LIGHT_GRAY);
            a3.setPreferredSize(new Dimension(60,30));
            a3.setHorizontalAlignment(SwingConstants.CENTER);
    
            JLabel a4=new JLabel("4");
            a4.setOpaque(true);
            a4.setBackground(Color.CYAN);
            a4.setPreferredSize(new Dimension(60,30));
            a4.setHorizontalAlignment(SwingConstants.CENTER);
    
            contentPane.add(a1);
            contentPane.add(a2);
            contentPane.add(a3);
            contentPane.add(a4);
        }
    }

    优化

    MyFrame7.java

    package swingkongjian;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class MyFrame7 extends JFrame {
        public MyFrame7(String title){
            super(title);
            Container contentPane=getContentPane();
            contentPane.setLayout(new FlowLayout());
    
            JLabel a1=new ColorfulLabel("1",Color.YELLOW);
    
            JLabel a2=new ColorfulLabel("2",Color.GREEN);
    
            JLabel a3=new ColorfulLabel("3",Color.LIGHT_GRAY);
    
            JLabel a4=new ColorfulLabel("4",Color.CYAN);
    
            contentPane.add(a1);
            contentPane.add(a2);
            contentPane.add(a3);
            contentPane.add(a4);
        }
    
        private static class ColorfulLabel extends JLabel{
            public ColorfulLabel(String text,Color bgColor){
                super(text);
                setOpaque(true);
                setBackground(bgColor);
                setPreferredSize(new Dimension(60,30));
                setHorizontalAlignment(SwingConstants.CENTER);
            }
        }
    }

    SwingDemo06.java

    package swingkongjian;
    
    import javax.swing.*;
    
    public class SwingDemo06 {
        private static void CreateGUI(){
            MyFrame6 frame=new MyFrame6("SwingDemo06");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(600,400);
    
            frame.setVisible(true);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    CreateGUI();
                }
            });
        }
    }
    SwingDemo06.java

    SwingDemo07.java

    package swingkongjian;
    
    import javax.swing.*;
    
    public class SwingDemo07 {
        private static void CreateGUI(){
            MyFrame7 frame=new MyFrame7("SwingDemo07");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(600,400);
    
            frame.setVisible(true);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    CreateGUI();
                }
            });
        }
    }
    SwingDemo07.java
  • 相关阅读:
    MVC路由配置例
    js制作烟花效果
    限制帐号同时两处以上登录-ASP.NET
    地图按范围查找-经纬度计算
    多种JSON格式及遍历方式
    .net错误日志记录(log4)
    .net WebServer示例及调用(接口WSDL动态调用 JAVA)
    关于大型网站技术演进的思考
    优化网站加载速度的14个技巧
    大型网站架构改进历程
  • 原文地址:https://www.cnblogs.com/yeyueweiliang/p/13647752.html
Copyright © 2020-2023  润新知