【Java GUI编程】004-Swing学习笔记(二):图片按钮、单选框、多选框、下拉框、列表框、文本框、密码框、文本域

目录

一、图片按钮

代码演示:

运行结果:

二、单选按钮

代码演示:

运行结果:

三、多选按钮

代码演示:

运行结果:

四、下拉框

代码演示:

运行结果:

五、列表框

代码演示:

运行结果:

备注代码:

六、文本框

代码演示:

运行结果:

七、密码框

代码演示:

运行结果:

八、文本域

代码演示:

运行结果:


一、图片按钮

代码演示:

package com.zibo.Lession05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestImageButton extends JFrame {
    public TestImageButton() {
        Container container = getContentPane();
        URL url = TestImageButton.class.getResource("tx.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        JButton jButton = new JButton();
        jButton.setIcon(imageIcon);
        jButton.setToolTipText("图片按钮");
        container.add(jButton);
        setVisible(true);
        setBounds(300,300,800,800);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

 

二、单选按钮

代码演示:

package com.zibo.Lession05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class TestJRadioButton extends JFrame {
    public TestJRadioButton() {
        Container container = getContentPane();
        //单选框
        JRadioButton rb1 = new JRadioButton("RB1");
        JRadioButton rb2 = new JRadioButton("RB2");
        JRadioButton rb3 = new JRadioButton("RB3");
        //由于单选框只能选一个,我们将他们成组
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(rb1);
        buttonGroup.add(rb2);
        buttonGroup.add(rb3);

        container.add(rb1,BorderLayout.NORTH);
        container.add(rb2,BorderLayout.CENTER);
        container.add(rb3,BorderLayout.SOUTH);

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

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

运行结果:

 

三、多选按钮

代码演示:

package com.zibo.Lession05;

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

public class TestJCheckBox extends JFrame {
    public TestJCheckBox() {
        Container container = getContentPane();
        //多选框
        JCheckBox cb1 = new JCheckBox("cb1");
        JCheckBox cb2 = new JCheckBox("cb2");
        JCheckBox cb3 = new JCheckBox("cb3");

        container.add(cb1,BorderLayout.NORTH);
        container.add(cb2,BorderLayout.CENTER);
        container.add(cb3,BorderLayout.SOUTH);

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

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

运行结果:

 

四、下拉框

代码演示:

package com.zibo.lession06;

import com.zibo.Lession05.TestJCheckBox;

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

public class TestJComboBox extends JFrame {
    public TestJComboBox(){
        Container container = getContentPane();

        JComboBox<String> jComboBox = new JComboBox<>();
        jComboBox.addItem("one");
        jComboBox.addItem("two");
        jComboBox.addItem("three");

        container.add(jComboBox);

        setVisible(true);
        setBounds(300,300,300,120);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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


运行结果:

 

五、列表框

代码演示:

package com.zibo.lession06;

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

public class TestJList extends JFrame {
    public TestJList() {
        Container container = getContentPane();

        //列表
        String[] strings = {"大哥","二哥","三哥","四哥"};
        JList<String> jList = new JList<>(strings);
        container.add(jList);

        setVisible(true);
        setBounds(300,300,500,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

备注代码:

        //可用此实现动态增减
        Vector<String> vector = new Vector<>();
        JList<String> jList = new JList<>(vector);
        vector.add("大哥");
        vector.add("二哥");
        vector.add("三哥");
        vector.add("四哥");
        vector.remove("三哥");

 

六、文本框

代码演示:

package com.zibo.lession06;

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

public class TestJTextField extends JFrame {
    public TestJTextField() {
        Container container = getContentPane();

        //文本框
        JTextField jTextField = new JTextField("这是文本框");
        container.add(jTextField);

        setVisible(true);
        setBounds(300,300,500,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

 

七、密码框

代码演示:

package com.zibo.lession06;

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

public class TestJPasswordField extends JFrame {
    public TestJPasswordField() {
        Container container = getContentPane();

        //文本框
        JPasswordField jPasswordField = new JPasswordField("这是密码框");
        jPasswordField.setEchoChar('*');
        container.add(jPasswordField);

        setVisible(true);
        setBounds(300,300,500,200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

 

八、文本域

代码演示:

package com.zibo.Lession05;

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

//带滚动条的面板
public class TestJScrollPane extends JFrame {
    public TestJScrollPane() {
        Container container = getContentPane();
        //文本域
        JTextArea jTextArea = new JTextArea(20, 50);
        jTextArea.setText("TestJScrollPane");
        //面板
        JScrollPane jScrollPane = new JScrollPane(jTextArea);
        container.add(jScrollPane);

        setVisible(true);
        setBounds(300,300,300,350);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

运行结果:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值