【Java GUI编程】003-Swing学习笔记(一):窗口、弹窗、标签、面板

目录

一、JFrame窗口

代码演示:

运行结果:

二、JDialog弹窗

代码演示:

运行结果:

三、Icon和ImageIcon标签

1、Lable

2、Icon

代码演示:

运行结果:

3、ImageIcon

代码演示:

运行结果:

文件位置图:

四、面板

1、普通面板JPanel

代码演示:

运行结果:

2、滚动面板JScrollPane

代码演示:

运行结果:


一、JFrame窗口

代码演示:

package com.zibo.lession04;

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

public class TestJFrame {
    public static void main(String[] args) {
        //创建窗口
        init();
    }
    //init 初始化
    public static void init(){
        //JFrame extends Frame
        JFrame jFrame = new JFrame("TestJFrame");
        jFrame.setBounds(300,300,500,500);
        jFrame.setVisible(true);
        JLabel jLabel = new JLabel("这是JLabel");
        //水平居中
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        jFrame.add(jLabel);
        Container container = jFrame.getContentPane();
        //设置背景颜色
        container.setBackground(Color.cyan);
        //关闭事件
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

运行结果:

 

二、JDialog弹窗

代码演示:

package com.zibo.lession04;

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

public class TestDialog {
    public static void main(String[] args) {
        init();
    }
    public static void init(){
        JFrame jFrame = new JFrame("TestDialog");
        JButton jButton = new JButton("显示dialog");
        jFrame.add(jButton);
        JDialog jDialog = new JDialog(jFrame, "这是弹窗的提示内容!");
        jDialog.setBounds(400,400,200,200);
        JLabel jLabel = new JLabel("jDialog里面的jLabel");
        jDialog.add(jLabel);
        jButton.addActionListener(e -> {
            jDialog.setVisible(true);
        } );
        jFrame.setBounds(300,300,500,500);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

运行结果:

 

三、Icon和ImageIcon标签

1、Lable

new Lable("XXX");

 

2、Icon

代码演示:

package com.zibo.lession04;

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

public class TestIcon extends JFrame implements Icon {

    private int width;
    private int height;

    public TestIcon() {}

    public TestIcon(int width, int height) throws HeadlessException {
        this.width = width;
        this.height = height;
    }

    public void init(){
        TestIcon icon = new TestIcon(width, height);
        //图标放在标签上,也可以放在按钮上
        JLabel jLabel = new JLabel("标签",icon,SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(jLabel);
        setBounds(300,300,500,500);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

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

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

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

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

运行结果:

 

3、ImageIcon

代码演示:

package com.zibo.lession04;

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

public class TestImageIcon extends JFrame {
    public TestImageIcon() throws HeadlessException {
        //获取图片地址
        JLabel label = new JLabel("ImageIcon");
        URL url = TestImageIcon.class.getResource("tx.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

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

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

    }

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

运行结果:

文件位置图:

 

四、面板

1、普通面板JPanel

代码演示:

package com.zibo.Lession05;

import com.zibo.lession01.TestPanel;

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

public class TestJPanel extends JFrame {
    public TestJPanel(){
        Container container = getContentPane();
        container.setLayout(new GridLayout(2,1,10,10));//行列间距
        JPanel jPanel1 = new JPanel(new GridLayout(1, 3));
        JPanel jPanel2 = new JPanel(new GridLayout(2, 3));
        JPanel jPanel3 = new JPanel(new GridLayout(3, 3));
        jPanel1.add(new JButton("1"));
        jPanel1.add(new JButton("2"));
        jPanel1.add(new JButton("3"));
        jPanel2.add(new JButton("1"));
        jPanel2.add(new JButton("2"));
        jPanel2.add(new JButton("3"));
        jPanel3.add(new JButton("1"));
        jPanel3.add(new JButton("2"));
        jPanel3.add(new JButton("3"));
        container.add(jPanel1);
        container.add(jPanel2);
        container.add(jPanel3);

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

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

运行结果:

 

2、滚动面板JScrollPane

代码演示:

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();
    }
}

运行结果:

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值