GUI编程-Swing

GUI编程

3、Swing

3.1、窗口、面板

package com.zhang.lesson04;

import javax.swing.*;
import java.awt.*;
public class JFrameDome {
    // init();  初始化
    public void init(){
        // 顶级窗口
        JFrame jf = new JFrame("这是一个JFrame的窗口");
        jf.setVisible(true);
        jf.setBounds(100,100,200,200);
        jf.setBackground(Color.blue);
        //  文本居中
        JLabel jLabel = new JLabel("这个是顶级窗口", SwingConstants.CENTER);
        jf.add(jLabel);
        // 获得一个容器
        jf.getContentPane().setBackground(Color.blue);
        // 关闭事件
       jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        // 建立一个窗口
        new JFrameDome().init();     
    }
}

3.2、弹窗

JDialog,用来被弹出,默认就有关闭事件!

package com.zhang.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.setSize(700,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // JFrame 放东西,容器
        Container contentPane = this.getContentPane();
        // 绝对布局
        contentPane.setLayout(null);

         //按钮
        JButton jButton = new JButton("点击弹出一个对话框"); //  创建
        jButton.setBounds(30,30,200,50);
        // 点击这个按钮的时候,弹出一个弹窗
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 弹窗
                new MyDialogDemo();
            }
        });
        contentPane.add(jButton);

    }

    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.setBackground(Color.blue);
        JLabel jLabel = new JLabel("新的弹窗");
        jLabel.setBounds(50,50,50,50);
        container.add(jLabel);

    }
}

3.3、标签

label

new JLabel("xxx");

图标ICON

package com.zhang.lesson04;

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.height = height;
        this.width = width;
    }

    public void init(){
        IconDemo iconDemo = new IconDemo(15, 15);
        // 图标可以放在标签了,也可以放在按钮上!
        JLabel iconText = new JLabel("IconText", iconDemo, SwingConstants.CENTER);
        Container contentPane = getContentPane();
        contentPane.add(iconText);

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


    public static void main(String[] args) {
        new IconDemo().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;
    }
}

图片Icon

package com.zhang.lesson04;

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

public class ImageIcondemo extends JFrame{

    public ImageIcondemo(){
         // 获取图片地址
        JLabel jLabel = new JLabel("ImageIcon");
        URL url = ImageIcondemo.class.getResource("001.jpg");

        ImageIcon imageIcon = new ImageIcon(url);
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        Container contentPane = getContentPane();
        contentPane.add(jLabel);

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

        setBounds(10,10,20,20);
    }
    public static void main(String[] args) {
        new ImageIcondemo();
    }
}

3.4、面板

JPanel

package com.zhang.lesson05;

import javax.imageio.stream.ImageInputStream;
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));
        JPanel panel2 = new JPanel(new GridLayout(1, 2));
        JPanel panel3 = new JPanel(new GridLayout(2, 1));
        JPanel panel4 = new JPanel(new GridLayout(3, 2));

        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel3.add(new JButton("3"));
        panel3.add(new JButton("3"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));
        panel4.add(new JButton("4"));



        container.add(panel);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

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

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

JScrollPanel

package com.zhang.lesson05;

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

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = this.getContentPane();
        // 文本域
        JTextArea area = new JTextArea(20, 50);
        area.setText("一大段文字");
        // Scroll面板
        JScrollPane jScrollPane = new JScrollPane(area);
        container.add(jScrollPane);

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

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

3.5、按钮

图片按钮

package com.zhang.lesson05;

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

public class JButtonDemo01 extends JFrame {
    public JButtonDemo01(){
        Container contentPane = this.getContentPane();
        //将图片变为图标
        URL resource = JButtonDemo01.class.getResource("001.jpg");
        ImageIcon imageIcon = new ImageIcon(resource);

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

        contentPane.add(jButton);

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

    }

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

单选按钮

package com.zhang.lesson05;

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

public class JButtonDemo02 extends JFrame {
    public JButtonDemo02(){
        Container contentPane = this.getContentPane();
        //将图片变为图标
        URL resource = JButtonDemo01.class.getResource("001.jpg");
        ImageIcon imageIcon = new ImageIcon(resource);

        //单选框
        JRadioButton jRadioButton01 = new JRadioButton("JRadioButton01");
        JRadioButton jRadioButton02 = new JRadioButton("JRadioButton02");
        JRadioButton jRadioButton03 = new JRadioButton("JRadioButton03");

        //由于单选框只能选择一个,分组,一个组只能选择一个
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(jRadioButton01);
        buttonGroup.add(jRadioButton02);
        buttonGroup.add(jRadioButton03);

        contentPane.add(jRadioButton01,BorderLayout.CENTER);
        contentPane.add(jRadioButton02,BorderLayout.NORTH );
        contentPane.add(jRadioButton03,BorderLayout.SOUTH);


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

    }

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

复选按钮

package com.zhang.lesson05;

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

public class JButtonDemo03 extends JFrame {
    public JButtonDemo03(){
        Container contentPane = this.getContentPane();
        //将图片变为图标
        URL resource = JButtonDemo01.class.getResource("001.jpg");
        ImageIcon imageIcon = new ImageIcon(resource);

        //多选框
        JCheckBox jCheckBox1 = new JCheckBox("jCheckBox1");
        JCheckBox jCheckBox2 = new JCheckBox("jCheckBox2");
        JCheckBox jCheckBox3 = new JCheckBox("jCheckBox3");

        contentPane.add(jCheckBox1,BorderLayout.CENTER);
        contentPane.add(jCheckBox2,BorderLayout.NORTH);
        contentPane.add(jCheckBox3,BorderLayout.SOUTH);

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

    }

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

3.6、 列表

下拉框

package com.zhang.lesson05;

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

public class TestComboboxDemo01 extends JFrame {
    public TestComboboxDemo01(){
        Container contentPane = this.getContentPane();

        JComboBox JComboBox = new JComboBox();
        JComboBox.addItem(null);
        JComboBox.addItem("111");
        JComboBox.addItem("222");
        JComboBox.addItem("222");

        contentPane.add(JComboBox);

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

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

列表框

package com.zhang.lesson05;

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

public class TestComboboxDemo02 extends JFrame {
    public TestComboboxDemo02(){
        Container contentPane = this.getContentPane();
        // 生成列表内容
       // String[] contents = {"1","2","3"};

        Vector vector = new Vector();
        //列表中需要放入内容
        JList jList = new JList(vector);

        vector.add("zhansan");
        vector.add("zhansan2");
        vector.add("zhansan3");

        contentPane.add(jList);

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

    public static void main(String[] args) {
        new TestComboboxDemo02();
    }
}
  • 应用场景
  • 选择地区,或者单一选项
  • 列表,展示信息,一般都是动态扩容!

3.7、文本框

文本框

package com.zhang.lesson05;

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

public class TestTextDemo01 extends JFrame {
    public TestTextDemo01(){
        Container contentPane = this.getContentPane();

        JTextField jTextField = new JTextField("hello");
        JTextField jTextField2 = new JTextField("world",20);

        contentPane.add(jTextField,BorderLayout.NORTH);
        contentPane.add(jTextField2,BorderLayout.SOUTH);

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

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

密码框

package com.zhang.lesson05;

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

public class TestTextDemo02 extends JFrame {
    public TestTextDemo02(){
        Container contentPane = this.getContentPane();

        JPasswordField jPasswordField = new JPasswordField();
        jPasswordField.setEchoChar('*');
        contentPane.add(jPasswordField);

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

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

文本域

package com.zhang.lesson05;

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

public class JScrollDemo extends JFrame {
    public JScrollDemo(){
        Container container = this.getContentPane();
        // 文本域
        JTextArea area = new JTextArea(20, 50);
        area.setText("一大段文字");
        // Scroll面板
        JScrollPane jScrollPane = new JScrollPane(area);
        container.add(jScrollPane);

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值