GUI编程之Swing

本文详细介绍了Java Swing中的JFrame窗口面板、JFrame与JDialog的使用,包括设置文字、图标、标签居中、布局管理等,并展示了如何创建和操作各种组件如JLabel、JButton、JComboBox和JTextArea。
摘要由CSDN通过智能技术生成
窗口 面板
package com.yeyue.lesson04;

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.cyan);

        //设置文字
        JLabel label =  new JLabel("欢迎来到哈哈哈哈哈");
        frame.add(label);

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

    public static void main(String[] args) {
        //建立一个窗口
        new JFrameDemo().init();
    }
}
标签居中
package com.yeyue.lesson04;

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

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

}

class MyJFrame2 extends JFrame{
    public void init(){
        this.setBounds(10,10,200,300);
        this.setVisible(true);

        //设置文字
        JLabel label =  new JLabel("欢迎来到");
        this.add(label);

        //让标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //获得一个容器
        Container container =  this.getContentPane();
        container.setBackground(Color.YELLOW);
    }
}
弹窗

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

package com.yeyue.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 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 Label("你好啊,我的朋友"));

    }
}
标签

label

new JLabel()
图标ICON
package com.yeyue.lesson04;

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

//图标,需要实现类,Frame继承
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);
    }


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

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

}
图片ICON
package com.yeyue.lesson04;

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("003.jpeg");

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

        Container container = getContentPane();
        container.add(label);
        setBounds(100,100,200,200);

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

    public static void main(String[] args) {
        new ImageIconDemo();
    }
}
面板
package com.yeyue.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 panel1 = 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));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.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(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

        this.setVisible(true);
        this.setSize(400,400);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JPanelDemo();
    }
}
JScroll
package com.yeyue.lesson05;

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

public class JScrollDemo extends JFrame {

    public JScrollDemo(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("你好啊,我的朋友");

        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        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();
    }
}
按钮
图片按钮
package com.yeyue.lesson05;

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

public class JBottonDemo01 extends JFrame {

    public JBottonDemo01(){
        Container container = this.getContentPane();

        URL url = JBottonDemo01.class.getResource("003.jpeg");
        ImageIcon icon = new ImageIcon(url);

        JButton button = new JButton();
        button.setIcon(icon);
        button.setToolTipText("图片按钮");

        container.add(button);

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

    }

    public static void main(String[] args) {
        new JBottonDemo01();
    }
}
单选按钮
package com.yeyue.lesson05;

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

public class JButtonDemo02 extends JFrame {

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

        //单选框
        JRadioButton radioButton1 = new JRadioButton("radioButton1");
        JRadioButton radioButton2 = new JRadioButton("radioButton2");
        JRadioButton radioButton3 = new JRadioButton("radioButton3");

        //由于单选框只能选一个,分组
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1,BorderLayout.CENTER);
        container.add(radioButton2,BorderLayout.NORTH);
        container.add(radioButton3,BorderLayout.SOUTH);


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

    }

    public static void main(String[] args) {
        new JButtonDemo02();
    }
}
复选按钮
package com.yeyue.lesson05;

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

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.CENTER);
        container.add(checkBox02,BorderLayout.NORTH);
        container.add(checkBox03,BorderLayout.SOUTH);


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

    }

    public static void main(String[] args) {
        new JButtonDemo03();
    }
}
列表
下拉框
package com.yeyue.lesson06;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;

public class TestComboboxDemo01 extends JFrame {

    public TestComboboxDemo01(){
        Container container = this.getContentPane();

        JComboBox status = new JComboBox();

        status.addItem("null");
        status.addItem("正在上映");
        status.addItem("已下架");
        status.addItem("即将上映");

        container.add(status);

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

    }


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


}
列表框
package com.yeyue.lesson06;

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

public class TestComboboxDemo02 extends JFrame {

    public TestComboboxDemo02() {
        Container container = this.getContentPane();

        //生成列表的内容
        //String[] contents = {"1","2","3"};

        Vector contents = new Vector();

        //列表中需要放入内容
        JList jlist = new JList(contents);

        contents.add("你好");
        contents.add("啊");
        contents.add("朋友");

        container.add(jlist);

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

    }


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


}
  • 应用场景
    • 选择地区,或者一些单个选项
    • 列表,显示信息,一般是动态扩容
文本框
  • 文本框

    package com.yeyue.lesson06;
    
    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.setSize(300,300);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
        }
        
        public static void main(String[] args) {
            new TestTextDemo01();
        }
        
    }
    
  • 密码框

    package com.yeyue.lesson06;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class TestTextDemo01 extends JFrame {
    
        public TestTextDemo01(){
            Container container = this.getContentPane();
    
            JPasswordField passwordField = new JPasswordField();
            passwordField.setEchoChar('*');
    
            container.add(passwordField);
    
            this.setVisible(true);
            this.setSize(300,300);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    
        }
    
        public static void main(String[] args) {
            new TestTextDemo01();
        }
    
    }
    
  • 文本域

package com.yeyue.lesson05;

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

public class JScrollDemo extends JFrame {

    public JScrollDemo(){
        Container container = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20,50);
        textArea.setText("你好啊,我的朋友");

        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值