Swing之Jframe窗体、 JDialog弹窗、标签、面板、按钮(图片按钮,单选框,多选框)、列表(下拉框,列表框)、文本框、密码框

Swing

Jframe窗体

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

public class JFrameDemo {
    //init() 初始化
    public void init(){
        JFrame frame = new JFrame("这是一个JFrame窗口");
        frame.setVisible(true);
        frame.setBounds(100, 100, 200, 200);
        //frame.setBackground(Color.pink);
        //设置一个容器
        frame.getContentPane().setBackground(Color.pink);
        //设置文字
        JLabel jLabel = new JLabel("阿涛来了");
        frame.add(jLabel);

        //让文本标签水平居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        //关闭事件
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}

JDialog弹窗

弹窗默认有关闭事件!

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗口
public class DialogDemo extends JFrame {
    public static void main(String[] args) {
        new DialogDemo();
    }
    public DialogDemo() {
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(600, 600);

        //放东西,容器
        Container container = this.getContentPane();

        //绝对布局
        container.setLayout(null);

        //按钮
        JButton button = new JButton("点击弹出一个对话框");
        button.setBounds(30, 30, 200, 60);

        //监听器
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹窗
                new MyDialog();
            }
        });
        container.add(button);
    }
}

//弹窗的窗口
class MyDialog extends JDialog{
    public MyDialog() {
        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"));
    }
}

标签

Icon标签:

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

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 init(){
        IconDemo iconDemo = new IconDemo(15, 15);
        //图标放在标签上,也可以放在按钮上
        JLabel label = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

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

ImageIcon标签:

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


public class ImageIconDemo extends JFrame {
    public ImageIconDemo() {
        //获取图片的地址
        JLabel label = new JLabel("ImageIcon");
        URL url = ImageIconDemo.class.getResource("tx.png");

        ImageIcon imageIcon = new ImageIcon(url);//命名不要冲突
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        Container container = getContentPane();
        container.add(label);

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

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

面板

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

public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        Container container = this.getContentPane();
        container.setLayout(new GridLayout(2,2,10,10));//后面两个参数表式间距

        JPanel jPanel = new JPanel(new GridLayout(1, 3));
        JPanel jPane2 = new JPanel(new GridLayout(1, 2));
        JPanel jPane3 = new JPanel(new GridLayout(2, 1));
        JPanel jPane4 = new JPanel(new GridLayout(2, 2));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPanel.add(new JButton("1"));
        jPane2.add(new JButton("2"));
        jPane2.add(new JButton("2"));
        jPane3.add(new JButton("3"));
        jPane3.add(new JButton("3"));
        jPane4.add(new JButton("4"));
        jPane4.add(new JButton("4"));
        jPane4.add(new JButton("4"));
        jPane4.add(new JButton("4"));

        container.add(jPanel);
        container.add(jPane2);
        container.add(jPane3);
        container.add(jPane4);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,500,500);
    }

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

按钮

图片按钮:

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

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        Container container = this.getContentPane();
        //将一个图片变为一个图标
        URL url = JButtonDemo01.class.getResource("tx.png");
        Icon icon = new ImageIcon(url);

        //把这个图标放在按钮上
        JButton jButton = new JButton();
        jButton.setIcon(icon);
        jButton.setToolTipText("图片按钮");

        container.add(jButton);
        this.setVisible(true);
        this.setBounds(100, 100, 500, 500);
        this.setDefaultCloseOperation(3);
    }

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

单选框:

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

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02(){
        Container container = this.getContentPane();

        //单选框
        JRadioButton button01 = new JRadioButton("JRadioButton01");
        JRadioButton button02 = new JRadioButton("JRadioButton02");
        JRadioButton button03 = new JRadioButton("JRadioButton03");

        //由于单选框只能选一个,分组
        ButtonGroup group = new ButtonGroup();
        group.add(button01);
        group.add(button02);
        group.add(button03);

        container.add(button01,BorderLayout.CENTER);
        container.add(button02,BorderLayout.NORTH);
        container.add(button03,BorderLayout.SOUTH);

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

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

多选框:

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

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03(){
        Container container = this.getContentPane();

        //多选框
        JCheckBox checkBox01 = new JCheckBox("checkBox01");
        JCheckBox checkBox02 = new JCheckBox("checkBox02");
        JCheckBox checkBox03 = new JCheckBox("checkBox03");

        container.add(checkBox01,BorderLayout.NORTH);
        container.add(checkBox02,BorderLayout.CENTER);
        container.add(checkBox03,BorderLayout.SOUTH);

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

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

列表

下拉框:

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

public class TestComboxDemo01 extends JFrame {
    public TestComboxDemo01(){

        Container container = this.getContentPane();
        JComboBox comboBox = new JComboBox();
        comboBox.addItem(null);
        comboBox.addItem("即将上映");
        comboBox.addItem("正在上映");
        comboBox.addItem("已下架");

        container.add(comboBox);

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

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

列表框:

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

public class TestComboxDemo02 extends JFrame {
    public TestComboxDemo02(){

        Container container = this.getContentPane();

        //生成列表的内容
        //String[] contents={"1","2","3"};
        Vector contents = new Vector();
        JList list = new JList(contents);
        contents.add("1");
        contents.add("2");
        contents.add("3");

        container.add(list);

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

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

文本框

文本框:

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01(){

        Container container = this.getContentPane();

        //文本框
        JTextField textField1 = new JTextField("hello");
        JTextField textField2 = new JTextField("world",20);

        container.add(textField1,BorderLayout.NORTH);
        container.add(textField2,BorderLayout.SOUTH);


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

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

密码框:

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

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02(){

        Container container = this.getContentPane();

        //密码框
        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');

        container.add(passwordField);

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

    public static void main(String[] args) {
        new TestTextDemo02();
    }
}
  • 1
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值