2021.4.7GUI编程-Swing学习

GUI编程

1.Swing

窗口,面板

package com.kuang.lesson04;

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

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

class JFrame1 extends JFrame {
    public void init(){
        //获得容器
        this.setBounds(100,100,200,300);
        this.setVisible(true);
        Container container = this.getContentPane();
        container.setBackground(Color.blue);
        //设置文字
        JLabel label = new JLabel("hello");
        this.add(label);
    }

}

1.2弹窗

package com.kuang.lesson04;

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

public class DialogDemo extends JFrame {
    public DialogDemo(){
        this.setVisible(true);
        this.setBounds(100,100,700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //放东西,容器
        Container container = this.getContentPane();
        //绝对布局
        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);
        Container container = this.getContentPane();
        container.setLayout(null);
        container.add(new JLabel("dialog"));
    }
}

1.3标签

lable

new JLabel("xxx")

图标icon

package com.kuang.lesson04;

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

public class IconDemo extends JFrame implements Icon {


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

    public void init(){
        IconDemo demo = new IconDemo(15, 15);
        //图标可以放在标签上,也可以放在按钮上
        JLabel label = new JLabel("incontest", demo, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public IconDemo(){}//无参构造
    public IconDemo(int width,int height){
        this.width=width;
        this.height=height;
    }

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



图片标签

package com.kuang.lesson04;

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

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

    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.add(label);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

1.4面板

JPanel

package com.kuang.lesson05;

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

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"));

        container.add(panel);
        setVisible(true);
        setSize(500,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

JScrollPanel滚动条

package com.kuang.lesson05;

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

public class JScrollDemo extends JFrame {
    public JScrollDemo() throws HeadlessException {
        Container container = getContentPane();
        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("welcome");
        //Scroll面板
        JScrollPane pane = new JScrollPane(textArea);
        container.add(pane);
        setBounds(100,100,400,400);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

1.5按钮

new JButton();
  • 单选按钮
//单选框
JRadioButton radioButton1 = new JRadioButton();
JRadioButton radioButton2 = new JRadioButton();
JRadioButton radioButton3 = new JRadioButton();
//由于单选框只能选择一个,分组,一个组只能选择一个
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
  • 复选按钮
JCheckBox checkBox1 = new JCheckBox();

1.6列表

  • 下拉框

    JComBox status = new JComboBox();
    
    status.addItem(null);
    status.addItem("正在热映");
    status.addItem("已下架");
    status.addItem("即将上映");
    container.add(status);
    
  • 列表框

    //生成列表的内容
    String[] contents = {"1","2","3"};
    JList jList = new JList(contents);
    
  • 应用场景

    • 下拉框—>选择地区,或者一些单个选项
    • 列表—>展示信息,一般是动态扩容

3.7文本框

  • 文本框

    JTextField textField = new JTextField("hello");
    
  • 密码框

    JPasswordField passwordField =new JPasswordField();
    passwordField.setEchoChar("*");
    
  • 文本域

JTextArea textArea = new JTextArea();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值