Swing学习笔记(窗口,弹窗,标签,图标,图片,面板,滚动条,图片按钮,单选多选按钮,下拉框,列表框,密码框,文本框)

窗口

public class JFrameDemo {
//    inint();初始化
    private void inint(){
//        顶级窗口
        JFrame jf = new JFrame("这是一个JFrame窗口");
        jf.setVisible(true);
        jf.setBackground(Color.black);
        jf.setBounds(100,100,200,200);
//        设置文字 Jlabel
        JLabel label = new JLabel("欢迎来到王者荣耀");
        jf.add(label);
//        容器实例化
//        关闭事件
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
//        建议一个窗口
        new JFrameDemo().inint();
    }
}

弹窗(Dialog)

//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo() {
        this.setVisible(true);
        this.setSize(700, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭
//        JFrame放东西  容器this.getContentPane();
        Container container = this.getContentPane();
//            布局setLayout
//                绝对布局
        container.setLayout(null);
//        按钮
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(30,30,200,50);//按钮大小
//        点击这个按钮的时候弹出一个弹窗
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                弹窗
            new MyDialogDemo();
            }
        });
        container.add(button);//把按钮放到容器中

    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
// 弹窗窗口
class  MyDialogDemo extends JDialog{
    public MyDialogDemo() {
        this.setVisible(true);
        this.setBounds(100,100,500,500);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//默认关闭
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new Label("学习java"));
    }
}

标签(lable)

图标ICON

//图标是一个接口,需要实现类  JFrame继承
public class IconDemo extends JFrame implements Icon {
    private  int width;
    private  int height;
    public IconDemo(){};//无参构造
    public IconDemo(int width,int height){
        this.width = width;
        this.height = height;
    };//有参构造
    // 初始化
    public void inint(){
        IconDemo iconDemo = new IconDemo(15,15);
        //图片放在标签上,也可以放在按钮上
        JLabel label = new JLabel("icontext", iconDemo, SwingConstants.CENTER);
        Container container = getContentPane();//容器
        container.add(label);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new IconDemo().inint();

    }
    @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;
    }
}

在这里插入图片描述

图片 ICON

class ImageIconDemo extends JFrame {
    public ImageIconDemo()  {
        JLabel label = new JLabel("ImageIcon");
//        获取图片的地址
        URL url = ImageIconDemo.class.getResource("tx.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);//设置图片
        label.setHorizontalAlignment(SwingConstants.CENTER);//居中
        Container container = getContentPane();//获得container 容器
        container.add(label);//标签放到容器中
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 800);


    }

    public static void main(String[] args) {
        new ImageIconDemo();
    }
}

在这里插入图片描述

面板

public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();//容器
        container.setLayout(new GridLayout(2, 1, 10, 10));//后面参数的意思是间距
        JPanel panel = new JPanel(new GridLayout(1,3));
        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        container.add(panel);


    }

    public static void main(String[] args) {
        new JPanelDemo();

    }
}

在这里插入图片描述

JScrollPanel 滚动条

public class JScrollDemo extends JFrame {
    public JScrollDemo()  {
        Container container = this.getContentPane();
//        文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("欢迎学习java");
//        SCROLL面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,300);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}

在这里插入图片描述

按钮

图片按钮

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01()  {
        Container container = this.getContentPane();
//        获取图片路径
//        把一个图片变成一个图片
        URL resource = JButtonDemo01.class.getResource("tx.jpg");
        ImageIcon icon = new ImageIcon(resource);
//        把这个图标放在按钮上
        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");
        container.add(button);//把按钮加到容器中
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,500);
    }

    public static void main(String[] args) {
        new JButtonDemo01();
    }
}

在这里插入图片描述

单选按钮(JRadioButton)

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02()  {
        Container container = this.getContentPane();
//        单选框
        JRadioButton  radioButton1 = new JRadioButton("JRadioButton1");
        JRadioButton  radioButton2 = new JRadioButton("JRadioButton2");
        JRadioButton  radioButton3 = new JRadioButton("JRadioButton3");
//由于单选框只能选择一个,分组,一个组中只能选择一个
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);
        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,500);
    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}

在这里插入图片描述

复现按钮JCheckBox

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03()  {
        Container container = this.getContentPane();
//多选框
        JCheckBox jCheckBox1 = new JCheckBox("JCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("JCheckBox2");
//      放到容器里
        container.add(jCheckBox1,BorderLayout.NORTH);
        container.add(jCheckBox2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,500);
    }

    public static void main(String[] args) {
        new JButtonDemo03();
    }
}

在这里插入图片描述

列表

下拉框 JComboBox

选择地区,或者一些单个选项

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01()  {
        Container container = this.getContentPane();
//       status状态
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("正在热映");
        status.addItem("已经下架");
        status.addItem("即将上映");
        container.add(status);
        this.setSize(500, 300);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TestComboboxDemo01();
    }
}

在这里插入图片描述

列表框

列表,展示信息,一般是动态扩容的

public class TestComboboxDemo02 extends JFrame {
    public TestComboboxDemo02()  {
        Container container = this.getContentPane();
//        生成列表的内容  
        String [] cotents = {"1","2","3"};
//列表中需要放内容  直接丢到构造器 
        JList jList = new JList(cotents);
        container.add(jList);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500, 300);


    }

    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
}

在这里插入图片描述

文本框

文本框JTextField

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01()  {

        Container container= this.getContentPane();
//        文本框
        JTextField textField1 = new JTextField("hello");
        JTextField textField2 = new JTextField("World",20);//默认20个
        container.add(textField1,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500, 300) ;

    }

    public static void main(String[] args) {
        new TestTextDemo01();
    }
}

在这里插入图片描述

密码框JPasswordField

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02()  {

        Container container= this.getContentPane();
// 密码框
        JPasswordField passwordField = new JPasswordField();//***
        passwordField.setEchoChar('*');//替换
        container.add(passwordField);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭
        this.setSize(500, 300) ;

    }

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}

在这里插入图片描述

文本域

public class TestTextDemo03 extends JFrame {
    public TestTextDemo03() {
        Container container = this.getContentPane();
        //        文本域
        JTextArea textArea = new JTextArea(10,20);
        textArea.setText("欢迎123123123");
//        Scroll面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setSize(500,300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new TestTextDemo03();
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值