Java.Swing包学习

一、常用类

JFrame:窗口

JLabel: 标签

JPanel:面板

JDialog:弹窗

JButton:按钮

Icon:图标

ImageIcon:图片JScrollPane:滚动面板

JTextArea:文本域

JRadioButton:单选框(使用ButtonGroup分组)

JCheckBox:多选框

JComboBox:下拉框

JList:列表框

JTextField:文本框

JPasswordField:密码框(使用setEchoChar替换字符)

二、使用示例

package com.SwingTest;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

public class JFrameTest {
    public static void main(String[] args) {
        new MYJFrame().init();
    }
}

class MYJFrame extends JFrame{
    MYJFrame frame = this;
    void init(){
        //窗口设置
        setBounds(100,100,700,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setBackground(Color.GRAY);
        contentPane.setLayout(new GridLayout(3,4));

        //创建组件:文本、ICON、图片、按钮
        MyLabel label1 = new MyLabel("文本", null, SwingConstants.CENTER);
        MyLabel label2 = new MyLabel("paint绘制Icon", new MyIcon(),SwingConstants.CENTER);
        ImageIcon imageIcon = new ImageIcon(JFrameTest.class.getResource("龙眼.jpg"));
        MyLabel label3 = new MyLabel("图片Icon",imageIcon,SwingConstants.CENTER);
        Btn_Dialog btn1 = new Btn_Dialog("点击弹窗按钮");
        JScrollPane panel1 = new JScrollPane(new JTextArea("滚动窗口",30,80));

        //单选框
        ButtonGroup group = new ButtonGroup();
        JRadioButton btn2 = new JRadioButton("男");
        JRadioButton btn3 = new JRadioButton("女");
        group.add(btn2);
        group.add(btn3);
        JPanel panel2 = new JPanel();
        panel2.add(new JLabel("单选框"));
        panel2.add(btn2);
        panel2.add(btn3);

        //多选框
        JPanel panel3 = new JPanel(new GridLayout(3,1));
        for (int i = 1; i <= 3; i++) {
            panel3.add(new JCheckBox("多选框" + i));
        }

        //下拉框
        JComboBox comboBox = new JComboBox();
        comboBox.addItem("(下拉框)");
        comboBox.addItem("选项1");
        comboBox.addItem("选项2");
        comboBox.addItem("选项3");

        //列表框
        Vector<String> contents = new Vector<>();
        JList list = new JList(contents);
        contents.add("列表框1");
        contents.add("列表框2");
        contents.add("列表框3");

        //密码框
        JPanel panel4 = new JPanel(new GridLayout(2,1));
        JPasswordField password = new JPasswordField();
        password.setEchoChar('*');
        panel4.add(new Label("密码框"));
        panel4.add(password);
        
        //插入组件
        contentPane.add(label1);
        contentPane.add(label2);
        contentPane.add(label3);
        contentPane.add(btn1);
        contentPane.add(panel1);
        contentPane.add(panel2);
        contentPane.add(panel3);
        contentPane.add(comboBox);
        contentPane.add(list);
        contentPane.add(panel4);

        setVisible(true);
    }

    class MyLabel extends JLabel{
        public MyLabel(String text, Icon icon, int horizontalAlignment) {
            super(text, icon, horizontalAlignment);
        }
    }

    class Btn_Dialog extends Button{
        public Btn_Dialog(String label) throws HeadlessException {
            super(label);
            this.addActionListener((e)->{
                new MyDialog();
            });
        }
    }

    class MyDialog extends JDialog{
        public MyDialog() {
            init();
        }
        void init(){
            this.setVisible(true);
            this.setBounds(100,100,800,600);
        }
    }

    class MyIcon extends Container implements Icon{
        private int width = 30;
        private int height = 30;

        public MyIcon() {
            init();
        }

        void init(){
            this.setBounds(60,60,width,height);
        }

        public MyIcon(int width, int height) {
            this.width = width;
            this.height = height;
            init();
        }
        
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.fillOval(x,y,width,height);
        }

        @Override
        public int getIconWidth() {
            return this.width;
        }

        @Override
        public int getIconHeight() {
            return this.height;
        }
    }
    
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值